Class: GetTweets
- Inherits:
-
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))
(File.expand_path(oauth_file))
= Tweets.new
end
|
Instance Attribute Details
#display ⇒ Object
Returns the value of attribute display.
16
17
18
|
# File 'lib/twitter_filter/get_tweets.rb', line 16
def display
@display
end
|
29
30
31
|
# File 'lib/twitter_filter/get_tweets.rb', line 29
def
@more_tweets ||= nil
end
|
25
26
27
|
# File 'lib/twitter_filter/get_tweets.rb', line 25
def
@tweets ||= Tweets.new
end
|
Instance Method Details
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/twitter_filter/get_tweets.rb', line 59
def (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_explanation ⇒ Object
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()
.display if display
if
self. = - 1
self. <<
p "Tweets count: " + .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_exception ⇒ Object
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. = 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 <= 0
end
(sleep 0.2 until <= 0) if num
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. = num if num
self.display = config[:display] || true
tsc = TweetStream::Client.new
tsc.track(term) do |status, client|
handle_tweet(status)
client.stop if <= 0
end
(sleep 0.2 until <= 0) if num
end
|