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
# File 'lib/brutalismbot/twitter/client.rb', line 22

def push(post, dryrun:nil)
  Brutalismbot.logger.info("PUSH #{"DRYRUN " if dryrun}twitter://@brutalismbot")
  method = post.url.nil? ? :push_text : :push_image
  send(method, post, dryrun: dryrun)
end

#push_image(post, dryrun: nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/brutalismbot/twitter/client.rb', line 33

def push_image(post, dryrun:nil)
  status = post.to_twitter
  uri    = URI.parse(post.url)
  Brutalismbot.logger.info("GET #{uri}")
  uri.open do |media|
    @client.update_with_media(status, media)
  end unless dryrun
end

#push_text(post, dryrun: nil) ⇒ Object



28
29
30
31
# File 'lib/brutalismbot/twitter/client.rb', line 28

def push_text(post, dryrun:nil)
  status = post.to_twitter
  @client.update(status) unless dryrun
end