class Ruboty::Adapters::Twitter

Public Instance Methods

run() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 16
def run
  Ruboty.logger.debug("#{self.class}##{__method__} started")
  abortable
  listen
  Ruboty.logger.debug("#{self.class}##{__method__} finished")
end
say(message) click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 23
def say(message)
  client.update(message[:body], in_reply_to_status_id: message[:original][:tweet].try(:id))
end

Private Instance Methods

abortable() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 77
def abortable
  Thread.abort_on_exception = true
end
client() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 57
def client
  ::Twitter::REST::Client.new do |config|
    config.consumer_key        = ENV["TWITTER_CONSUMER_KEY"]
    config.consumer_secret     = ENV["TWITTER_CONSUMER_SECRET"]
    config.access_token        = ENV["TWITTER_ACCESS_TOKEN"]
    config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
  end
end
enabled_to_auto_follow_back?() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 29
def enabled_to_auto_follow_back?
  ENV["TWITTER_AUTO_FOLLOW_BACK"] == "1"
end
listen() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 33
def listen
  stream.user do |message|
    case message
    when ::Twitter::Tweet
      retweeted = message.retweeted_status.is_a?(::Twitter::Tweet)
      tweet = retweeted ? message.retweeted_status : message
      Ruboty.logger.debug("#{tweet.user.screen_name} tweeted #{tweet.text.inspect}")
      robot.receive(
        body: tweet.text,
        from: tweet.user.screen_name,
        tweet: message
      )
    when ::Twitter::Streaming::Event
      if message.name == :follow
        Ruboty.logger.debug("#{message.source.screen_name} followed #{message.target.screen_name}")
        if enabled_to_auto_follow_back? && message.target.screen_name == robot.name
          Ruboty.logger.debug("Trying to follow back #{message.source.screen_name}")
          client.follow(message.source.screen_name)
        end
      end
    end
  end
end
stream() click to toggle source
# File lib/ruboty/adapters/twitter.rb, line 67
def stream
  ::Twitter::Streaming::Client.new do |config|
    config.consumer_key        = ENV["TWITTER_CONSUMER_KEY"]
    config.consumer_secret     = ENV["TWITTER_CONSUMER_SECRET"]
    config.access_token        = ENV["TWITTER_ACCESS_TOKEN"]
    config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"]
  end
end