Class: SimpleTweet::V2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_tweet/v2_client.rb

Overview

Twitte API v2を叩くクライアント

Constant Summary collapse

TW_API_ORIGIN =
"https://api.twitter.com"
TW_UPLOAD_ORIGIN =
"https://upload.twitter.com"
TW_MEDIA_UPLOAD_PATH =
"/1.1/media/upload.json"
TW_METADATA_CREATE_PATH =
"/1.1/media/metadata/create.json"
TW_TWEET_PATH =
"/2/tweets"
UA =
"SimpleTweet/#{SimpleTweet::VERSION}".freeze
APPEND_PER =
5 * (1 << 20)

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key:, consumer_secret:, access_token:, access_token_secret:, max_append_retry: 3) ⇒ Client



20
21
22
23
24
25
26
# File 'lib/simple_tweet/v2_client.rb', line 20

def initialize(consumer_key:, consumer_secret:, access_token:, access_token_secret:, max_append_retry: 3)
  @consumer_key_ = consumer_key
  @consumer_secret_ = consumer_secret
  @access_token_ = access_token
  @access_token_secret_ = access_token_secret
  @max_append_retry_ = max_append_retry
end

Instance Method Details

#tweet(message:, media_ids: []) ⇒ Object



29
30
31
32
33
34
# File 'lib/simple_tweet/v2_client.rb', line 29

def tweet(message:, media_ids: [])
  json = { text: message } # : ::Hash[::Symbol, (::String|::Hash[::Symbol, ::Array[::String]])]
  json[:media] = { media_ids: media_ids } unless media_ids.empty?
  header = { "User-Agent": UA, "content-type": "application/json" }
  access_token.post(TW_TWEET_PATH, json.to_json, header)
end

#tweet_with_media(message:, media_type:, media:, alt_text: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/simple_tweet/v2_client.rb', line 36

def tweet_with_media(message:, media_type:, media:, alt_text: nil)
  media_ids = upload_media(media_type: media_type, media: media)
  unless alt_text.nil?
    media_ids.each do |media_id|
      (media_id: media_id, alt_text: alt_text)
    end
  end
  tweet(message: message, media_ids: media_ids)
end