Class: GetTweets

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_filter/get_tweets.rb

Defined Under Namespace

Classes: InvalidOauthFileException, NoOauthFileException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_file = nil) ⇒ GetTweets

Returns a new instance of GetTweets.



19
20
21
22
23
# File 'lib/twitter_filter/get_tweets.rb', line 19

def initialize(oauth_file = nil)
  raise_no_oauth_file_exception unless oauth_file && File.exists?(File.expand_path(oauth_file))
  configure_tweetstream(File.expand_path(oauth_file))
  tweets = Tweets.new
end

Instance Attribute Details

#displayObject

Returns the value of attribute display.



16
17
18
# File 'lib/twitter_filter/get_tweets.rb', line 16

def display
  @display
end

#more_tweetsObject



29
30
31
# File 'lib/twitter_filter/get_tweets.rb', line 29

def more_tweets
  @more_tweets ||= nil
end

#tweetsObject



25
26
27
# File 'lib/twitter_filter/get_tweets.rb', line 25

def tweets
  @tweets ||= Tweets.new
end

Instance Method Details

#configure_tweetstream(oauth_file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/twitter_filter/get_tweets.rb', line 59

def configure_tweetstream(oauth_file)
  TweetStream.configure do |c|
    settings = get_settings(oauth_file)
    ['consumer_key', 'consumer_secret', 'oauth_token', 'oauth_token_secret'].each do |twitk|
      raise_invalid_oauth_file_exception("#{oauth_file} must have a valid #{twitk} value") unless settings[twitk]
      c.public_send(twitk + '=', settings[twitk])
    end
    c.public_send('auth_method=', :oauth)
  end
end

#display_oauth_file_explanationObject



33
34
35
36
37
38
39
40
41
# File 'lib/twitter_filter/get_tweets.rb', line 33

def display_oauth_file_explanation
  puts "You must call #{self.class.name}.new() with the location of your OAuth credentials YAML file"
  puts "The file should look like this (but with valid values):\n"
  puts "---"
  puts "consumer_key: Id254ZAzQJtF7ltUerYPw"
  puts "consumer_secret: fSuNNvlairlesqGDnasde58zib3hApHMTUnZ0J94jmw"
  puts "oauth_token: 938572174-ByY7Plbe1J4Dp5JSwZjmPowBwOr2VVjLA2Juxeqp"
  puts "oauth_token_secret: PrRwQVOgO2Sg0r4IOJPwQq95IqHieZeEnemxkiHKw"
end

#get_settings(oauth_file) ⇒ Object



53
54
55
56
57
# File 'lib/twitter_filter/get_tweets.rb', line 53

def get_settings(oauth_file)
  YAML.load_file oauth_file
rescue
  raise_invalid_oauth_file_exception("Cannot read file as valid YAML file")
end

#handle_tweet(tweet) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/twitter_filter/get_tweets.rb', line 70

def handle_tweet(tweet)
  tweet.display if display
  if more_tweets
    self.more_tweets = more_tweets - 1
    self.tweets << tweet
    p "Tweets count: " + tweets.size.to_s
  end
rescue Exception => e
  puts "***Error saving tweet***: #{e.message}\n"
end

#raise_invalid_oauth_file_exception(msg) ⇒ Object



48
49
50
51
# File 'lib/twitter_filter/get_tweets.rb', line 48

def raise_invalid_oauth_file_exception(msg)
  display_oauth_file_explanation
  raise InvalidOauthFileException, msg
end

#raise_no_oauth_file_exceptionObject



43
44
45
46
# File 'lib/twitter_filter/get_tweets.rb', line 43

def raise_no_oauth_file_exception
  display_oauth_file_explanation
  raise NoOauthFileException
end

#sample(num = nil, config = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/twitter_filter/get_tweets.rb', line 81

def sample(num = nil, config = {})
  self.more_tweets = num if num
  self.display = (config[:display] && config[:display] == false) ? false : true
  tsc = TweetStream::Client.new
  tsc.sample do |status, client|
    handle_tweet(status)
    client.stop if more_tweets <= 0
  end
  (sleep 0.2 until more_tweets <= 0) if num
  tweets
end

#track_term(term, num = nil, config = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/twitter_filter/get_tweets.rb', line 93

def track_term(term, num = nil, config = {})
  self.more_tweets = num if num
  self.display = config[:display] || true
  tsc = TweetStream::Client.new
  tsc.track(term) do |status, client|
    handle_tweet(status)
    client.stop if more_tweets <= 0
  end
  (sleep 0.2 until more_tweets <= 0) if num
  tweets
end