Class: TrumpcareTracker::Reporters

Inherits:
RakeTask
  • Object
show all
Defined in:
lib/trumpcare_tracker/reporters.rb

Overview

Rake tasks to generate audit reports in CSV format

require ‘trumpcare_tracker/reporters’ TrumpcareTracker::Reporters.new

$ rake tracker:export:senate_democrats $ rake tracker:export:house_democrats $ rake tracker:export:senate_republicans $ rake tracker:export:house_republicans $ rake tracker:export:all

$ rake tracker:homepage_scraper:senate_democrats $ rake tracker:homepage_scraper:house_democrats $ rake tracker:homepage_scraper:senate_republicans $ rake tracker:homepage_scraper:house_republicans $ rake tracker:homepage_scraper:all

Instance Method Summary collapse

Methods inherited from RakeTask

#audit_rep, #audit_tweets, #democrats, #handles, #house_democrats, #house_republicans, #mentions_mapper, #republicans, #senate_democrats, #senate_republicans

Constructor Details

#initializeReporters

Returns a new instance of Reporters.



25
26
27
28
# File 'lib/trumpcare_tracker/reporters.rb', line 25

def initialize
  export_task
  homepage_scraper_task
end

Instance Method Details

#all_tasksObject



119
120
121
122
# File 'lib/trumpcare_tracker/reporters.rb', line 119

def all_tasks
  desc 'Run for each caucus'
  task(all: [:senate_democrats, :house_democrats, :senate_republicans, :house_republicans])
end

#export(caucus) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/trumpcare_tracker/reporters.rb', line 50

def export(caucus)
  csv_tweet_report = CSV.generate do |csv|
    csv << %w[
      senator
      official_user_name
      alt_user_name
      tweets_in_last_seven_days
      trumpcare_tweets
      tct_percentage
      russia_tweets
      rt_percentage
      tct_to_rt_ratio
      trumpcare_tweet_urls
      russia_tweet_urls
    ]
    audit_tweets(send(caucus)) { |tracker| csv << tracker.to_h.values }
  end

  File.open("trumpcare_tweet_report_#{caucus}.csv", 'w') do |file|
    file.write csv_tweet_report
  end
end

#export_taskObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/trumpcare_tracker/reporters.rb', line 30

def export_task
  namespace(:tracker) do
    namespace(:export) do
      desc 'Output a report of Senate Democrats Trumpcare tweets'
      task(:senate_democrats) { export(:senate_democrats) }

      desc 'Output a report of House Democrats Trumpcare tweets'
      task(:house_democrats) { export(:house_democrats) }

      desc 'Output a report of Senate Republicans Trumpcare tweets'
      task(:senate_republicans) { export(:senate_republicans) }

      desc 'Output a report of House Republicans Trumpcare tweets'
      task(:house_republicans) { export(:house_republicans) }

      all_tasks
    end
  end
end

#homepage_scraper(caucus) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/trumpcare_tracker/reporters.rb', line 96

def homepage_scraper(caucus)
  csv_homepage_report = CSV.generate do |csv|
    csv << %w[senator url trumpcare_mentions russia_mentions trumpcare_to_russia_ratio]
    send(caucus).each do |rep|
      doc = Nokogiri::HTML(open(rep.url))
      trumpcare_mentions_count = mentions_mapper(doc, TrumpcareTracker.trumpcare_keyword_regex).count
      russia_mentions_count = mentions_mapper(doc, TrumpcareTracker.russia_keyword_regex).count
      csv << [
        rep.official_full,
        rep.url,
        trumpcare_mentions_count,
        russia_mentions_count,
        TrumpcareTracker.ratio(trumpcare_mentions_count, russia_mentions_count)
      ]
      puts "Scraped #{rep.official_full}'s homepage"
    end
  end

  File.open("trumpcare_homepage_report_#{caucus}.csv", 'w') do |file|
    file.write csv_homepage_report
  end
end

#homepage_scraper_taskObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/trumpcare_tracker/reporters.rb', line 75

def homepage_scraper_task
  namespace(:tracker) do
    desc 'Search Senators\' official homepage for TrumpCare and Russia Mentions'
    namespace(:homepage_scraper) do
      desc 'Search Senate Democrat\'s homepages'
      task(:senate_democrats) { homepage_scraper(:senate_democrats) }

      desc 'Search House Democrat\'s homepages'
      task(:house_democrats) { homepage_scraper(:house_democrats) }

      desc 'Search Senate Republican\'s homepages'
      task(:senate_republicans) { homepage_scraper(:senate_republicans) }

      desc 'Search House Republican\'s homepages'
      task(:house_republicans) { homepage_scraper(:house_republicans) }

      all_tasks
    end
  end
end