Skip to content

Commit

Permalink
Request all profile fields by default
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis committed Jul 4, 2024
1 parent 25a13e9 commit 9c06531
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion lib/threads/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
25 changes: 16 additions & 9 deletions spec/threads/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c06531

Please sign in to comment.