Skip to content

Commit

Permalink
Add support for reading user profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis committed Jun 20, 2024
1 parent 44ff5e9 commit 2411de3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/threads/api/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative "profile"
require_relative "thread"

module Threads
Expand All @@ -7,6 +8,15 @@ def initialize(access_token)
@access_token = access_token
end

def get_profile(user_id = "me", fields: nil)
params = {access_token: @access_token}
params[:fields] = Array(fields).join(",") if fields

response = connection.get(user_id, params)

Threads::API::Profile.new(response.body)
end

def list_threads(user_id: "me", **options)
params = options.slice(:since, :until, :before, :after, :limit).compact
params[:access_token] = @access_token
Expand Down
18 changes: 18 additions & 0 deletions lib/threads/api/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "time"

module Threads
module API
class Profile
attr_reader :id, :username, :profile_picture_url, :biography

def initialize(json)
@id = json["id"]
@username = json["username"]
@profile_picture_url = json["profile_picture_url"]
@biography = json["biography"]
end

alias_method :bio, :biography
end
end
end
50 changes: 50 additions & 0 deletions spec/threads/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,56 @@
RSpec.describe Threads::API::Client do
let(:client) { described_class.new("ACCESS_TOKEN") }

describe "#get_profile" do
let(:response_body) do
{id: "1234567890"}.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"))
.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
expect(profile.id).to eq("1234567890")
end

context "when requesting all 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"]
}
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"})
.to_return(body: response_body, headers: {"Content-Type" => "application/json"})
end

let(:profile) { client.get_profile("1234567890", **params) }

it "fully hydrates the Profile" 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
end
end

describe "#list_threads" do
let(:before_cursor) { "QVFIUkFyUFVVczIwWjVNaDVieUxHbW9vWFVqNkh0MHU0cFZARVHRTR3ZADSUxnaTdTdXl2eXBqUG4yX0RLVTF3TUszWW1nXzVJcmU5bnd2QmV2ZAVVDNVFXcFRB" }
let(:after_cursor) { "QVFIUkZA4QzVhQW1XdTFibU9lRUF2YUR1bEVRQkhVZAWRCX2d3TThUMGVoQ3ZAwT1E4bElEa0JzNGJqV2ZAtUE00U0dMTnhZAdXpBUWN3OUdVSF9aSGZAhYXlGSDFR" }
Expand Down

0 comments on commit 2411de3

Please sign in to comment.