Class: TrumpcareTracker
- Inherits:
-
Object
show all
- Defined in:
- lib/trumpcare_tracker.rb,
lib/trumpcare_tracker/version.rb,
lib/trumpcare_tracker/rake_task.rb,
lib/trumpcare_tracker/reporters.rb,
lib/trumpcare_tracker/tweet_bot.rb
Overview
Track the Twitter mentions of Trumpcare by Democratic US Senators
Defined Under Namespace
Classes: RakeTask, Reporters, TweetBot
Constant Summary
collapse
- CONSUMER_KEY =
ENV['TCT_CONSUMER_KEY']
- CONSUMER_SECRET =
ENV['TCT_CONSUMER_SECRET']
- ACCESS_TOKEN =
ENV['TCT_ACCESS_TOKEN']
- ACCESS_TOKEN_SECRET =
ENV['TCT_ACCESS_TOKEN_SECRET']
- VERSION =
'0.1.9'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(user, screen_name, alt_screen_name = nil) ⇒ TrumpcareTracker
Returns a new instance of TrumpcareTracker.
32
33
34
35
36
|
# File 'lib/trumpcare_tracker.rb', line 32
def initialize(user, screen_name, alt_screen_name = nil)
@user = user
@screen_name = client.user screen_name
@alt_screen_name = client.user alt_screen_name if alt_screen_name
end
|
Instance Attribute Details
#alt_screen_name ⇒ Object
Returns the value of attribute alt_screen_name.
13
14
15
|
# File 'lib/trumpcare_tracker.rb', line 13
def alt_screen_name
@alt_screen_name
end
|
#screen_name ⇒ Object
Returns the value of attribute screen_name.
13
14
15
|
# File 'lib/trumpcare_tracker.rb', line 13
def screen_name
@screen_name
end
|
#user ⇒ Object
Returns the value of attribute user.
13
14
15
|
# File 'lib/trumpcare_tracker.rb', line 13
def user
@user
end
|
Class Method Details
.percentage(numerator, denominator) ⇒ Object
20
21
22
|
# File 'lib/trumpcare_tracker.rb', line 20
def self.percentage(numerator, denominator)
(ratio(numerator, denominator) * 100).round(2)
end
|
.ratio(numerator, denominator) ⇒ Object
15
16
17
18
|
# File 'lib/trumpcare_tracker.rb', line 15
def self.ratio(numerator, denominator)
return 0.0 if denominator.zero?
(numerator / denominator.to_f).round(4)
end
|
.russia_keyword_regex ⇒ Object
28
29
30
|
# File 'lib/trumpcare_tracker.rb', line 28
def self.russia_keyword_regex
/(russia|comey|sessions|mueller|fbi|flynn|obstruction of justice|collusion|putin|kremlin)/
end
|
.trumpcare_keyword_regex ⇒ Object
24
25
26
|
# File 'lib/trumpcare_tracker.rb', line 24
def self.trumpcare_keyword_regex
/(ahca|trumpcare|healthcare|health|care|drug|medication|medicaid|prescription|vaccine|obamacare|cbo|premiums|insurance|deductibles|aca|o-care|a.h.c.a|a.c.a|pre-existing conditions|hhs)/
end
|
Instance Method Details
#alt_screen_name_screen_name ⇒ Object
150
151
152
153
|
# File 'lib/trumpcare_tracker.rb', line 150
def alt_screen_name_screen_name
return '' unless alt_screen_name
alt_screen_name.screen_name
end
|
#audit ⇒ Object
99
100
101
|
# File 'lib/trumpcare_tracker.rb', line 99
def audit
puts to_s
end
|
#client ⇒ Object
Instantiate a Twitter Rest Client with API authorization
39
40
41
42
43
44
45
46
|
# File 'lib/trumpcare_tracker.rb', line 39
def client
@_client ||= Twitter::REST::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = ACCESS_TOKEN
config.access_token_secret = ACCESS_TOKEN_SECRET
end
end
|
#fetch_timeline(screen_name) ⇒ Object
Make two cursored API calls to fetch the 400 most recent tweets
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/trumpcare_tracker.rb', line 53
def fetch_timeline(screen_name)
return [] unless screen_name
timeline = client.user_timeline(screen_name, exclude_replies: true, count: 200)
timeline + client.user_timeline(
screen_name,
exclude_replies: true,
max_id: timeline.last.id - 1,
count: 200
)
end
|
Collect a user’s tweets within the last 7 days
65
66
67
68
69
70
|
# File 'lib/trumpcare_tracker.rb', line 65
def
@_recent_tweets ||= timeline.each_with_object([]) do |, memo|
= (Time.now.to_date - .created_at.to_date).to_i
memo << if <= 7
end
end
|
110
111
112
|
# File 'lib/trumpcare_tracker.rb', line 110
def
@_recent_tweets_count ||= .count
end
|
#reduce_by_keywords(regex) ⇒ Object
72
73
74
75
76
77
|
# File 'lib/trumpcare_tracker.rb', line 72
def reduce_by_keywords(regex)
.each_with_object([]) do |, memo|
= client.status(.id, tweet_mode: 'extended')
memo << if (, regex)
end
end
|
95
96
97
|
# File 'lib/trumpcare_tracker.rb', line 95
def
@_russia_tweets ||= reduce_by_keywords(self.class.russia_keyword_regex)
end
|
118
119
120
|
# File 'lib/trumpcare_tracker.rb', line 118
def
@_russia_tweets_count ||= .count
end
|
126
127
128
|
# File 'lib/trumpcare_tracker.rb', line 126
def
self.class.percentage(.count, .count)
end
|
#timeline ⇒ Object
48
49
50
|
# File 'lib/trumpcare_tracker.rb', line 48
def timeline
@_timeline ||= (fetch_timeline(screen_name) + fetch_timeline(alt_screen_name))
end
|
#to_h ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/trumpcare_tracker.rb', line 134
def to_h
{
senator: user,
official_user_name: screen_name.screen_name,
alt_user_name: alt_screen_name_screen_name,
tweets_in_last_seven_days: .count,
trumpcare_tweet_count: .count,
tct_percentage: ,
russia_tweet_count: .count,
rt_percentage: ,
tct_to_rt_ratio: ,
trumpcare_tweet_urls: .map { || .uri.to_s },
russia_tweet_urls: .map { || .uri.to_s }
}
end
|
#to_s ⇒ Object
103
104
105
106
107
108
|
# File 'lib/trumpcare_tracker.rb', line 103
def to_s
@_to_s ||= "@#{screen_name.screen_name}'s last 7 days\n#{} tweets\n"\
"#{} TrumpCare - #{}%\n"\
"#{} Russia - #{}%\n"\
"#{} TrumpCare tweets for every Russia tweet"
end
|
155
156
157
|
# File 'lib/trumpcare_tracker.rb', line 155
def (options = {})
client.update(".#{self}", options)
end
|
130
131
132
|
# File 'lib/trumpcare_tracker.rb', line 130
def
self.class.ratio(.count, .count)
end
|
91
92
93
|
# File 'lib/trumpcare_tracker.rb', line 91
def
@_trumpcare_tweets ||= reduce_by_keywords(self.class.trumpcare_keyword_regex)
end
|
114
115
116
|
# File 'lib/trumpcare_tracker.rb', line 114
def
@_trumpcare_tweets_count ||= .count
end
|
122
123
124
|
# File 'lib/trumpcare_tracker.rb', line 122
def
self.class.percentage(.count, .count)
end
|
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/trumpcare_tracker.rb', line 79
def (, regex)
full_text = .attrs[:full_text]
if full_text.downcase.match?(regex)
true
elsif .
= client.status(..id, tweet_mode: 'extended')
(, regex)
else
false
end
end
|