class Socialinvestigator::CLI::TwitterCli

Public Instance Methods

config() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 103
def config
  config = Socialinvestigator::Config.config.twitter_config || {}
  print "App key            : "
  config[:twitter_app_key] = $stdin.gets.strip
  print "App Secret         : "
  config[:twitter_app_secret] = $stdin.gets.strip
  print "Access token       : "
  config[:twitter_access_token] = $stdin.gets.strip
  print "Access token secret: "
  config[:twitter_access_token_secret] = $stdin.gets.strip
  Socialinvestigator::Config.config.twitter_config = config
  puts "Saved."
end
filter( terms ) click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 82
def filter( terms )
  streaming_client.filter(track: terms) do |object|
    puts "@#{object.user.user_name}:#{object.text}" if object.is_a?(Twitter::Tweet)
  end
end
followers( screenname ) click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 59
def followers( screenname )
  client.followers( screenname ).each do |u|
    printf( "@%-15s %-20s %s\n", u.user_name, u.name, u.description )
  end
end
home_timeline() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 25
def home_timeline
  client.home_timeline.each do |tweet|
    puts "@#{tweet.user.user_name}:#{tweet.text}"
  end
end
limits() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 46
def limits
  resp = client.get( "/1.1/application/rate_limit_status.json" )
  current_time = Time.now.to_i
  template = "   %-40s %5d remaining, resets in %3d seconds\n"
  resp.body[:resources].each do |category,resources|
    puts category.to_s
    resources.each do |resource,info|
      printf template, resource.to_s, info[:remaining], (info[:reset] - current_time)
    end
  end
end
listen() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 89
def listen
  streaming_client.user do |object|
    case object
    when Twitter::Tweet
      puts "Tweet:@#{object.user.user_name}:#{object.text}"
    when Twitter::DirectMessage
      puts "DM:@#{object.sender.user_name}:#{object.text}"
    when Twitter::Streaming::StallWarning
      warn "Falling behind!"
    end
  end
end
lookup( url ) click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 13
def lookup( url )
  puts agent.lookup_url( url )
end
mentions() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 39
def mentions
  client.mentions.each do |tweet|
    puts "@#{tweet.user.user_name}:#{tweet.text}"
  end
end
retweets() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 32
def retweets
  client.retweets_of_me.each do |tweet|
    puts "@#{tweet.user.user_name}:#{tweet.text}"
  end
end
user( username ) click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 8
def user( username )
  agent.print_user_info client.user( username )
end
user_timeline() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 18
def user_timeline
  client.user_timeline.each do |tweet|
    puts "@#{tweet.user.user_name}:#{tweet.text}"
  end
end

Private Instance Methods

agent() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 118
def agent
  @agent ||= Socialinvestigator::Client::Twitter.new
end
client() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 122
def client
  tc = Socialinvestigator::Config.config.twitter_config
  if tc.nil? || tc[:twitter_app_key].nil?
    puts "Twitter config not found, try running:"
    puts "socialinvestigator twitter config"
    exit
  end
  @client ||= ::Twitter::REST::Client.new do |config|
    config.consumer_key        = tc[:twitter_app_key]
    config.consumer_secret     = tc[:twitter_app_secret]
    config.access_token        = tc[:twitter_access_token]
    config.access_token_secret = tc[:twitter_access_token_secret]
  end
end
streaming_client() click to toggle source
# File lib/socialinvestigator/cli/twitter.rb, line 137
def streaming_client
  tc = Socialinvestigator::Config.config.twitter_config
  if tc.nil?
    puts "Twitter config not found, try running:"
    puts "socialinvestigator twitter config"
    exit
  end
  @streaming_client ||= ::Twitter::Streaming::Client.new do |config|
    config.consumer_key        = tc[:twitter_app_key]
    config.consumer_secret     = tc[:twitter_app_secret]
    config.access_token        = tc[:twitter_access_token]
    config.access_token_secret = tc[:twitter_access_token_secret]
  end
end