Class: TrumpcareTracker::RakeTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/trumpcare_tracker/rake_task.rb

Overview

Base rake task class

Direct Known Subclasses

Reporters, TweetBot

Instance Method Summary collapse

Instance Method Details

#audit_rep(i, rep, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/trumpcare_tracker/rake_task.rb', line 59

def audit_rep(i, rep, &block)
  puts "Sending requests to Twitter API for #{rep.official_full}"
  rep_handles = handles.detect do |handle|
    handle['official'].downcase.strip == rep.twitter.downcase.strip
  end

  alt_screen_name = if rep_handles
    rep_handles['personal/campaign']
  end

  tracker = TrumpcareTracker.new(rep.official_full, rep.twitter, alt_screen_name)
  start_time = Time.now
  tracker.audit
  duration = (Time.now - start_time).round(2)
  puts tracker.to_s
  block.call(tracker, rep) if block_given?

  puts "#{i + 1} down. Out of #{tracker.timeline.length} tweets checked,\n"\
    "#{tracker.recent_tweets.length} were made in the last 7 days.\n"\
    "The oldest tweet analyzed was created #{tracker.recent_tweets.last&.created_at&.strftime("%H:%M:%S %-m/%-d/%y")}\n"\
    "#{tracker.requests} requests took #{duration} seconds."
rescue Twitter::Error::TooManyRequests
  puts 'Rate limit exceeded, waiting 5 minutes'
  sleep(300)
  audit_rep(i, rep, &block)
rescue Twitter::Error => e
  puts e.message
  audit_rep(i, rep, &block)
end

#audit_tweets(reps, &block) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/trumpcare_tracker/rake_task.rb', line 89

def audit_tweets(reps, &block)
  reps.each_with_index do |rep, i|
    next if rep.twitter.nil?
    audit_rep(i, rep, &block)
  end
ensure
  `say "beep beep beep beep beep"`
end

#democrats(chamber = '') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/trumpcare_tracker/rake_task.rb', line 12

def democrats(chamber = '')
  chamber = chamber.to_s

  democrats = PYR.reps do |r|
    r.democrat = true
    r.chamber  = chamber unless chamber.empty?
  end

  independents = PYR.reps do |r|
    r.independent = true
    r.chamber     = chamber unless chamber.empty?
  end

  democrats.objects.to_a + independents.objects.to_a
end

#handlesObject



55
56
57
# File 'lib/trumpcare_tracker/rake_task.rb', line 55

def handles
  @_handles ||= CSV.read(File.expand_path('../twitter_handles.csv', __FILE__), headers: true)
end

#house_democratsObject



32
33
34
# File 'lib/trumpcare_tracker/rake_task.rb', line 32

def house_democrats
  democrats('lower')
end

#house_republicansObject



51
52
53
# File 'lib/trumpcare_tracker/rake_task.rb', line 51

def house_republicans
  republicans('lower')
end

#mentions_mapper(doc, regex) ⇒ Object



98
99
100
# File 'lib/trumpcare_tracker/rake_task.rb', line 98

def mentions_mapper(doc, regex)
  doc.text.split("\n").select { |string| string.downcase.match?(regex) }
end

#republicans(chamber = '') ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/trumpcare_tracker/rake_task.rb', line 36

def republicans(chamber = '')
  chamber = chamber.to_s

  republicans = PYR.reps do |r|
    r.republican = true
    r.chamber = chamber unless chamber.empty?
  end

  republicans.objects
end

#senate_democratsObject



28
29
30
# File 'lib/trumpcare_tracker/rake_task.rb', line 28

def senate_democrats
  democrats('upper')
end

#senate_republicansObject



47
48
49
# File 'lib/trumpcare_tracker/rake_task.rb', line 47

def senate_republicans
  republicans('upper')
end