-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reading Threads media posts
- Loading branch information
1 parent
0a69592
commit 826b3c2
Showing
5 changed files
with
397 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require_relative "thread" | ||
|
||
module Threads | ||
module API | ||
class Client | ||
def initialize(access_token) | ||
@access_token = access_token | ||
end | ||
|
||
def threads(user_id: "me", **options) | ||
params = options.slice(:since, :until, :before, :after, :limit).compact | ||
params[:access_token] = @access_token | ||
|
||
fields = Array(options[:fields]).join(",") | ||
params[:fields] = fields unless fields.empty? | ||
|
||
response = connection.get("#{user_id}/threads", params) | ||
|
||
Threads::API::Thread::List.new(response.body) | ||
end | ||
|
||
def thread(thread_id, user_id: "me", fields: nil) | ||
params = {access_token: @access_token} | ||
params[:fields] = Array(fields).join(",") if fields | ||
|
||
response = connection.get("#{user_id}/threads/#{thread_id}", params) | ||
|
||
Threads::API::Thread.new(response.body) | ||
end | ||
|
||
private | ||
|
||
def connection | ||
@connection ||= Faraday.new(url: "https://graph.threads.net/v1.0/") do |f| | ||
f.request :url_encoded | ||
|
||
f.response :json | ||
f.response :raise_error | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "time" | ||
|
||
module Threads | ||
module API | ||
class Thread | ||
class List | ||
attr_reader :threads, :before, :after | ||
|
||
def initialize(json) | ||
@threads = json["data"].map { |t| Threads::API::Thread.new(t) } | ||
|
||
@before = json.dig("paging", "cursors", "before") | ||
@after = json.dig("paging", "cursors", "after") | ||
end | ||
end | ||
|
||
attr_reader :id, :type, :media_url, :permalink, :user_id, :username, :text, :timestamp, :created_at, :shortcode, :video_thumbnail_url, :children | ||
|
||
def initialize(json) | ||
@id = json["id"] | ||
@type = json["media_type"] | ||
@permalink = json["permalink"] | ||
@shortcode = json["shortcode"] | ||
@text = json["text"] | ||
@media_url = json["media_url"] | ||
@video_thumbnail_url = json["thumbnail_url"] | ||
@user_id = json.dig("owner", "id") | ||
@username = json["username"] | ||
@is_quote_post = json["is_quote_post"] | ||
|
||
@timestamp = json["timestamp"] | ||
@created_at = Time.iso8601(@timestamp) if @timestamp | ||
|
||
children = Array(json["children"]) | ||
@children = children.map { |c| Thread.new(c) } if children.any? | ||
end | ||
|
||
def quote_post? | ||
@is_quote_post | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.