From 9c065312cfc683d879815a281fe30935612f0f94 Mon Sep 17 00:00:00 2001 From: David Celis Date: Thu, 4 Jul 2024 09:50:27 -0700 Subject: [PATCH] Request all profile fields by default --- README.md | 10 ++++++++++ lib/threads/api/client.rb | 3 ++- spec/threads/api/client_spec.rb | 25 ++++++++++++++++--------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 8755f56..4a2d78c 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,16 @@ thread = client.get_thread("18050206876707110", user_id: "7770386109746442") `Threads::API::Client#get_thread` accepts only the `user_id` and `fields` options. +## Reading profiles + +To get a user's profile: + +```ruby +profile = client.get_profile("7770386109746442") +``` + +`Threads::API::Client#get_profile` accepts a `fields` option, which is an Array (or comma-separated String) of fields to include in the response. By default, all documented fields are requested. See the [Threads API documentation](https://developers.facebook.com/docs/threads/threads-profiles#fields) for a list of available fields. + ## Posting to Threads Posting to Threads is, at the very least, a two-step process. Threads requires that you first create a container for the media you want to post, then explicitly publishing that container as a thread. However, more steps are involved if you want to post multiple media items in a single thread. diff --git a/lib/threads/api/client.rb b/lib/threads/api/client.rb index 26337ba..8cdd3f8 100644 --- a/lib/threads/api/client.rb +++ b/lib/threads/api/client.rb @@ -4,6 +4,7 @@ module Threads module API class Client + PROFILE_FIELDS = %w[id username threads_profile_picture_url threads_biography] POST_FIELDS = %w[ id media_product_type @@ -24,7 +25,7 @@ def initialize(access_token) @access_token = access_token end - def get_profile(user_id = "me", fields: nil) + def get_profile(user_id = "me", fields: PROFILE_FIELDS) params = {access_token: @access_token} params[:fields] = Array(fields).join(",") if fields diff --git a/spec/threads/api/client_spec.rb b/spec/threads/api/client_spec.rb index e51db0f..8c37840 100644 --- a/spec/threads/api/client_spec.rb +++ b/spec/threads/api/client_spec.rb @@ -7,50 +7,57 @@ describe "#get_profile" do let(:response_body) do - {id: "1234567890"}.to_json + { + id: "1234567890", + username: "davidcelis", + profile_picture_url: "https://scontent-sjc3-1.cdninstagram.com/link/to/profile/picture/on/threads/", + biography: "Cowboy coder." + }.to_json end let(:params) { {} } let!(:request) do stub_request(:get, "https://graph.threads.net/v1.0/me") - .with(query: params.merge(access_token: "ACCESS_TOKEN")) + .with(query: params.merge(access_token: "ACCESS_TOKEN", fields: Threads::API::Client::PROFILE_FIELDS.join(","))) .to_return(body: response_body, headers: {"Content-Type" => "application/json"}) end let(:profile) { client.get_profile(**params) } - it "returns a response object with the user's profile" do + it "returns a fully hydrated Profile by default" do expect(profile.id).to eq("1234567890") + expect(profile.username).to eq("davidcelis") + expect(profile.profile_picture_url).to eq("https://scontent-sjc3-1.cdninstagram.com/link/to/profile/picture/on/threads/") + expect(profile.biography).to eq("Cowboy coder.") end - context "when requesting all fields" do + context "when requesting specific fields" do let(:response_body) do { id: "1234567890", username: "davidcelis", - profile_picture_url: "https://scontent-sjc3-1.cdninstagram.com/link/to/profile/picture/on/threads/", biography: "Cowboy coder." }.to_json end let(:params) do { - fields: ["id", "username", "profile_picture_url", "biography"] + fields: ["id", "username", "biography"] } end let!(:request) do stub_request(:get, "https://graph.threads.net/v1.0/1234567890") - .with(query: {access_token: "ACCESS_TOKEN", fields: "id,username,profile_picture_url,biography"}) + .with(query: {access_token: "ACCESS_TOKEN", fields: "id,username,biography"}) .to_return(body: response_body, headers: {"Content-Type" => "application/json"}) end let(:profile) { client.get_profile("1234567890", **params) } - it "fully hydrates the Profile" do + it "returns a Profile with the specific fields" do expect(profile.id).to eq("1234567890") expect(profile.username).to eq("davidcelis") - expect(profile.profile_picture_url).to eq("https://scontent-sjc3-1.cdninstagram.com/link/to/profile/picture/on/threads/") expect(profile.biography).to eq("Cowboy coder.") + expect(profile.profile_picture_url).to be_nil end end end