Class: ChatSDK::X::ApiClient
- Inherits:
-
ApiClient::Base
- Object
- ApiClient::Base
- ChatSDK::X::ApiClient
- Defined in:
- lib/chat_sdk/x/api_client.rb
Constant Summary collapse
- BASE_URL =
"https://api.x.com"
Instance Method Summary collapse
- #create_tweet(text:, reply_to: nil, media_ids: nil) ⇒ Object
- #delete_tweet(tweet_id:) ⇒ Object
- #get_dm_events(participant_id:, cursor: nil, limit: 50) ⇒ Object
- #get_user(user_id) ⇒ Object
-
#initialize(access_token) ⇒ ApiClient
constructor
A new instance of ApiClient.
- #like_tweet(user_id:, tweet_id:) ⇒ Object
- #send_dm(participant_id:, text:) ⇒ Object
- #unlike_tweet(user_id:, tweet_id:) ⇒ Object
- #upload_media(io:, content_type:, total_bytes:) ⇒ Object
Constructor Details
#initialize(access_token) ⇒ ApiClient
Returns a new instance of ApiClient.
10 11 12 |
# File 'lib/chat_sdk/x/api_client.rb', line 10 def initialize(access_token) @access_token = access_token end |
Instance Method Details
#create_tweet(text:, reply_to: nil, media_ids: nil) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/chat_sdk/x/api_client.rb', line 14 def create_tweet(text:, reply_to: nil, media_ids: nil) body = {"text" => text} body["reply"] = {"in_reply_to_tweet_id" => reply_to} if reply_to body["media"] = {"media_ids" => media_ids} if media_ids&.any? request(:post, "/2/tweets", body) end |
#delete_tweet(tweet_id:) ⇒ Object
62 63 64 |
# File 'lib/chat_sdk/x/api_client.rb', line 62 def delete_tweet(tweet_id:) request(:delete, "/2/tweets/#{tweet_id}") end |
#get_dm_events(participant_id:, cursor: nil, limit: 50) ⇒ Object
70 71 72 73 74 |
# File 'lib/chat_sdk/x/api_client.rb', line 70 def get_dm_events(participant_id:, cursor: nil, limit: 50) query = "max_results=#{[limit, 100].min}&dm_event.fields=id,text,sender_id,created_at" query += "&pagination_token=#{cursor}" if cursor request(:get, "/2/dm_conversations/with/#{participant_id}/dm_events?#{query}") end |
#get_user(user_id) ⇒ Object
66 67 68 |
# File 'lib/chat_sdk/x/api_client.rb', line 66 def get_user(user_id) request(:get, "/2/users/#{user_id}?user.fields=name,username") end |
#like_tweet(user_id:, tweet_id:) ⇒ Object
54 55 56 |
# File 'lib/chat_sdk/x/api_client.rb', line 54 def like_tweet(user_id:, tweet_id:) request(:post, "/2/users/#{user_id}/likes", {"tweet_id" => tweet_id}) end |
#send_dm(participant_id:, text:) ⇒ Object
50 51 52 |
# File 'lib/chat_sdk/x/api_client.rb', line 50 def send_dm(participant_id:, text:) request(:post, "/2/dm_conversations/with/#{participant_id}/messages", {"text" => text}) end |
#unlike_tweet(user_id:, tweet_id:) ⇒ Object
58 59 60 |
# File 'lib/chat_sdk/x/api_client.rb', line 58 def unlike_tweet(user_id:, tweet_id:) request(:delete, "/2/users/#{user_id}/likes/#{tweet_id}") end |
#upload_media(io:, content_type:, total_bytes:) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chat_sdk/x/api_client.rb', line 21 def upload_media(io:, content_type:, total_bytes:) # INIT init_result = request(:post, "/2/media/upload", { command: "INIT", total_bytes: total_bytes, media_type: content_type }) media_id = init_result.dig("data", "id") || init_result["media_id_string"] # APPEND (single chunk, works for images <5MB) response = upload_connection.post("/2/media/upload") do |req| req.body = { command: "APPEND", media_id: media_id, segment_index: 0, media_data: Base64.strict_encode64(io.read) } end handle_response(response) # FINALIZE request(:post, "/2/media/upload", { command: "FINALIZE", media_id: media_id }) media_id end |