Class: Brutalismbot::Twitter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/brutalismbot/twitter/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
# File 'lib/brutalismbot/twitter/client.rb', line 13

def initialize(client:nil)
  @client = client || ::Twitter::REST::Client.new do |config|
    config.access_token        = ENV["TWITTER_ACCESS_TOKEN"]
    config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
    config.consumer_key        = ENV["TWITTER_CONSUMER_KEY"]
    config.consumer_secret     = ENV["TWITTER_CONSUMER_SECRET"]
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/brutalismbot/twitter/client.rb', line 11

def client
  @client
end

Instance Method Details

#push(post, dryrun: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brutalismbot/twitter/client.rb', line 22

def push(post, dryrun:nil)
  opts = {}
  slices_for(post).each_with_index.map do |slice, index|
    status, media = slice
    Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}twitter://@brutalismbot")
    begin
      res = @client.update_with_media(status, media, opts)
      opts[:in_reply_to_status_id] = res.id
    rescue ::Twitter::Error::BadRequest => err
      if err.message =~ /Image file size must be <= \d+ bytes/
        Brutalismbot.logger.warn("IMAGE TOO LARGE - RETRYING WITH PREVIEWS")
        res = push_preview(post, opts, index)
        opts[:in_reply_to_status_id] = res.id
      end
    end unless dryrun

    res&.id
  end
end