Class: Threads::API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/threads/api/client.rb

Constant Summary collapse

PROFILE_FIELDS =
%w[id username threads_profile_picture_url threads_biography]
POST_FIELDS =
%w[
  id
  media_product_type
  media_type
  media_url
  permalink
  owner
  username
  text
  timestamp
  shortcode
  thumbnail_url
  children
  is_quote_post
]

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Client

Returns a new instance of Client.



24
25
26
# File 'lib/threads/api/client.rb', line 24

def initialize(access_token)
  @access_token = access_token
end

Instance Method Details



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/threads/api/client.rb', line 93

def create_carousel_item(type:, image_url: nil, video_url: nil)
  params = {access_token: @access_token, media_type: type, is_carousel_item: true}

  case type
  when "IMAGE"
    params[:image_url] = image_url || raise(ArgumentError, "The `:image_url` option is required when the item's type is \"IMAGE\"")
  when "VIDEO"
    params[:video_url] = video_url || raise(ArgumentError, "The `:video_url` option is required when the item's type is \"VIDEO\"")
  else
    raise ArgumentError, "Invalid item type: #{type}. Must be \"IMAGE\" or \"VIDEO\""
  end

  response = connection.post("me/threads", params)

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

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/threads/api/client.rb', line 110

def create_carousel_thread(children:, text: nil, reply_to_id: nil, reply_control: nil)
  params = {access_token: @access_token, media_type: "CAROUSEL", text: text}
  params[:children] = Array(children).join(",")
  params[:reply_to_id] = reply_to_id if reply_to_id
  params[:reply_control] = reply_control if reply_control

  raise ArgumentError, "At least one item must be present in the `:children` option" if params[:children].empty?

  response = connection.post("me/threads", params)

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

#create_thread(type: "TEXT", text: nil, image_url: nil, video_url: nil, reply_to_id: nil, reply_control: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/threads/api/client.rb', line 63

def create_thread(type: "TEXT", text: nil, image_url: nil, video_url: nil, reply_to_id: nil, reply_control: nil)
  params = {access_token: @access_token, media_type: type, text: text}
  params[:reply_to_id] = reply_to_id if reply_to_id
  params[:reply_control] = reply_control if reply_control

  case type
  when "IMAGE"
    params[:image_url] = image_url || raise(ArgumentError, "The `:image_url` option is required when the post's type is \"IMAGE\"")
  when "VIDEO"
    params[:video_url] = video_url || raise(ArgumentError, "The `:video_url` option is required when the post's type is \"VIDEO\"")
  when "TEXT"
    raise ArgumentError, "The `:text` option is required when the post's type is \"TEXT\"" if text.nil? || text.empty?
  else
    raise ArgumentError, "Invalid post type: #{type}. Must be one of: \"TEXT\", \"IMAGE\", or \"VIDEO\""
  end

  response = connection.post("me/threads", params)

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

#get_profile(user_id = "me", fields: PROFILE_FIELDS) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/threads/api/client.rb', line 28

def get_profile(user_id = "me", fields: PROFILE_FIELDS)
  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

#get_thread(thread_id, fields: POST_FIELDS) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/threads/api/client.rb', line 54

def get_thread(thread_id, fields: POST_FIELDS)
  params = {access_token: @access_token}
  params[:fields] = Array(fields).join(",") if fields

  response = connection.get(thread_id, params)

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

#get_thread_status(thread_id) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/threads/api/client.rb', line 84

def get_thread_status(thread_id)
  response = connection.get(thread_id, {
    access_token: @access_token,
    fields: "id,status,error_message"
  })

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

#list_threads(user_id: "me", **options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/threads/api/client.rb', line 37

def list_threads(user_id: "me", **options)
  params = options.slice(:since, :until, :before, :after, :limit).compact
  params[:access_token] = @access_token

  fields = if options.key?(:fields)
    Array(options[:fields]).join(",")
  else
    POST_FIELDS.join(",")
  end

  params[:fields] = fields unless fields.empty?

  response = connection.get("#{user_id}/threads", params)

  Threads::API::Thread::List.new(response.body)
end

#publish_thread(id) ⇒ Object Also known as: publish_carousel



123
124
125
126
127
# File 'lib/threads/api/client.rb', line 123

def publish_thread(id)
  response = connection.post("me/threads_publish", {access_token: @access_token, creation_id: id})

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