class Sensor::Actuator::TwitterRetrieval

Public Class Methods

new(time_range) click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 6
def initialize(time_range)
  @time_range = time_range
end

Public Instance Methods

acquire() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 10
def acquire
  map = {}
  map.tap do |map|
    map[:twitter] = {}
    map[:twitter][:followers] = client.user.followers_count
    map[:twitter][:tweets] = tweet_count
    map[:twitter][:mentions] = mention_count
  end
end

Protected Instance Methods

account() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 46
def account
  ENV['TWITTER_ACCOUNT']
end
client() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 50
def client
  if !@client
    @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
  @client
end
format_date(date) click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 21
def format_date(date)
  date.strftime("%Y-%m-%d")
end
mention_count() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 40
def mention_count
  search = client.search("@#{account} -from:#{account}",
    search_options)
  search.entries.count
end
search_options() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 25
def search_options
  {
    count: 100,
    since: format_date(@time_range.start_date),
    until: format_date(@time_range.end_date)
  }
end
tweet_count() click to toggle source
# File lib/sensor/actuator/twitter_retrieval.rb, line 33
def tweet_count
  search = client.search("from:#{account}",
    search_options
  )
  search.entries.count
end