Class: FecResults::President

Inherits:
Object
  • Object
show all
Defined in:
lib/fec_results/president.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ President

Returns a new instance of President.



6
7
8
9
10
11
# File 'lib/fec_results/president.rb', line 6

def initialize(params={})
  params.each_pair do |k,v|
   instance_variable_set("@#{k}", v)
  end
  @url = FecResults::PRESIDENT_URLS[year.to_s]
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/fec_results/president.rb', line 4

def url
  @url
end

#yearObject (readonly)

Returns the value of attribute year.



4
5
6
# File 'lib/fec_results/president.rb', line 4

def year
  @year
end

Instance Method Details

#general_election_results(*args) ⇒ Object



31
32
33
# File 'lib/fec_results/president.rb', line 31

def general_election_results(*args)
  send("general_election_results_#{year}", *args)
end

#general_election_results_2000(options = {}) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/fec_results/president.rb', line 287

def general_election_results_2000(options={})
  results = []
  t = RemoteTable.new(url.first, sheet: 'Master (with Totals & Percents)', skip: 1, headers: false)
  rows = t.entries
  rows = rows.select{|r| r[0] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate[2].blank?
    c = {year: year}
    c[:date] = Date.parse("11/7/2000")
    c[:chamber] = "P"
    c[:state] = candidate[0]
    c[:party] = candidate[2] == 'Combined' ? "COMBINED TOTAL" : candidate[2]
    c[:incumbent] = false
    c[:fec_id] = nil
    c[:candidate_first] = candidate[1].split(', ')[1]
    c[:candidate_last] = candidate[1].split(', ')[0]
    c[:candidate_suffix] = candidate[1].split(', ').last if candidate[1].split(', ').size > 2
    c[:candidate_name] = candidate[1]
    c[:general_votes] = candidate[3].blank? ? candidate[5].to_i : candidate[3].to_i
    c[:general_pct] = candidate[4].to_f
    results << c
  end
  Result.create_from_results(results)
end

#general_election_results_2004(options = {}) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/fec_results/president.rb', line 237

def general_election_results_2004(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2004 PRES GENERAL RESULTS')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:date] = Date.parse(candidate["GENERAL ELECTION DATE"])
    c[:chamber] = "P"
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY'].blank? ? "COMBINED TOTAL" : candidate['PARTY']
    c[:incumbent] = candidate['LAST NAME,  FIRST'] == 'Bush, George W.' ? true : false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['LAST NAME,  FIRST']
    c[:candidate_last] = candidate['LAST NAME,  FIRST']
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:general_votes] = candidate['GENERAL RESULTS'].to_i
    c[:general_pct] = candidate['GENERAL %'].to_f*100.0
    results << c
  end
  Result.create_from_results(results)
end

#general_election_results_2008(options = {}) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/fec_results/president.rb', line 185

def general_election_results_2008(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2008 PRES GENERAL RESULTS')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:chamber] = "P"
    c[:date] = Date.parse(candidate['GENERAL ELECTION DATE'])
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY'] == 'Combined Parties:' ? "COMBINED TOTAL" : candidate['PARTY']
    c[:incumbent] = false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['FIRST NAME']
    c[:candidate_last] = candidate['LAST NAME']
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:general_votes] = candidate['GENERAL RESULTS'].to_i
    c[:general_pct] = candidate['GENERAL %'].to_f*100.0
    c[:general_winner] = nil
    results << c
  end
  Result.create_from_results(results)
end

#general_election_results_2012(options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fec_results/president.rb', line 135

def general_election_results_2012(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2012 Pres General Results')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:chamber] = "P"
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY'] == 'Combined Parties:' ? "COMBINED TOTAL" : candidate['PARTY']
    c[:incumbent] = candidate['LAST NAME,  FIRST'] == 'Obama, Barack' ? true : false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['LAST NAME,  FIRST']
    c[:candidate_last] = candidate['LAST NAME,  FIRST']
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:general_votes] = candidate['GENERAL RESULTS'].to_i
    c[:general_pct] = candidate['GENERAL %'].to_f*100.0
    c[:general_winner] = candidate['WINNER INDICATOR'] == "W" ? true : false
    results << c
  end
  Result.create_from_results(results)
end

main instance methods called with optional arguments to filter.



19
20
21
# File 'lib/fec_results/president.rb', line 19

def popular_vote_summary(*args)
  send("popular_vote_summary_#{year}", *args)
end


61
62
63
64
65
66
67
68
69
# File 'lib/fec_results/president.rb', line 61

def popular_vote_summary_2004(options={})
  results = []
  t = RemoteTable.new(url, sheet: 'Table 1. Pres Popular Vote', skip: 3)
  t.entries.each do |row|
    break if row['Candidate'] == 'Total:'
    results << OpenStruct.new(candidate: row['Candidate'], party: row['(Party Label)'], popular_votes: row['Popular Vote Total'].to_i, popular_vote_percent: row['Percent of Popular Vote'].to_f)
  end
  results
end


51
52
53
54
55
56
57
58
59
# File 'lib/fec_results/president.rb', line 51

def popular_vote_summary_2008(options={})
  results = []
  t = RemoteTable.new(FecResults::SUMMARY_URLS[year.to_s], sheet: 'Table 1. 2008 Pres Popular Vote', skip: 3)
  t.entries.each do |row|
    break if row['Candidate (Party Label)'] == 'Total:'
    results << OpenStruct.new(candidate: row['Candidate (Party Label)'], popular_votes: row['Popular Vote Total'].to_i, popular_vote_percent: row['Percent of Popular Vote'].to_f)
  end
  results
end

cycle-specific methods called by main methods above



41
42
43
44
45
46
47
48
49
# File 'lib/fec_results/president.rb', line 41

def popular_vote_summary_2012(options={})
  results = []
  t = RemoteTable.new(url, sheet: 'Table 1. 2012 Pres Popular Vote', skip: 3)
  t.entries.each do |row|
    break if row['Candidate (Party Label)'] == 'Total:'
    results << OpenStruct.new(candidate: row['Candidate (Party Label)'], popular_votes: row['Popular Vote Total'].to_i, popular_vote_percent: row['Percent of Popular Vote'].to_f)
  end
  results
end

#primary_election_results(*args) ⇒ Object



35
36
37
# File 'lib/fec_results/president.rb', line 35

def primary_election_results(*args)
  send("primary_election_results_#{year}", *args)
end

#primary_election_results_2000(options = {}) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/fec_results/president.rb', line 312

def primary_election_results_2000(options={})
  results = []
  t = RemoteTable.new(url.last, sheet: 'Primary Results by State')
  rows = t.entries
  rows = rows.select{|r| r['STATE'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['PARTY'].blank?
    next if candidate['CANDIDATE'] == 'Total Party Votes'
    c = {year: year}
    c[:date] = nil
    c[:chamber] = "P"
    c[:state] = candidate['STATE']
    c[:party] = candidate['PARTY']
    c[:incumbent] = false
    c[:fec_id] = nil
    c[:candidate_first] = candidate['CANDIDATE'].split(', ')[1]
    c[:candidate_last] = candidate['CANDIDATE'].split(', ')[0]
    c[:candidate_suffix] = candidate['CANDIDATE'].split(', ').last if candidate['CANDIDATE'].split(', ').size > 2
    c[:candidate_name] = candidate['CANDIDATE']
    c[:primary_votes] = candidate['# OF VOTES'].to_i
    c[:primary_pct] = candidate['PERCENT'].to_f
    results << c
  end
  Result.create_from_results(results)
end

#primary_election_results_2004(options = {}) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/fec_results/president.rb', line 262

def primary_election_results_2004(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2004 PRES PRIMARY RESULTS')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:date] = Date.parse(candidate["PRIMARY DATE"])
    c[:chamber] = "P"
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY']
    c[:incumbent] = candidate['LAST NAME,  FIRST'] == 'Bush, George W.' ? true : false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['LAST NAME,  FIRST'] #fixme
    c[:candidate_last] = candidate['LAST NAME,  FIRST'] #fixme
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:primary_votes] = candidate['PRIMARY RESULTS'].to_i
    c[:primary_pct] = candidate['PRIMARY %'].to_f*100.0
    results << c
  end
  Result.create_from_results(results)
end

#primary_election_results_2008(options = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/fec_results/president.rb', line 211

def primary_election_results_2008(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2008 Pres Primary Results')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:chamber] = "P"
    c[:date] = Date.parse(candidate['PRIMARY DATE'])
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY']
    c[:incumbent] = false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['FIRST NAME']
    c[:candidate_last] = candidate['LAST NAME']
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:general_votes] = candidate['PRIMARY RESULTS'].to_i
    c[:general_pct] = candidate['PRIMARY %'].to_f*100.0
    c[:general_winner] = nil
    results << c
  end
  Result.create_from_results(results)
end

#primary_election_results_2012(options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fec_results/president.rb', line 160

def primary_election_results_2012(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2012 Pres Primary Results')
  rows = t.entries
  rows = rows.select{|r| r['STATE ABBREVIATION'] == options[:state]} if options[:state]
  rows.each do |candidate|
    next if candidate['LAST NAME,  FIRST'].blank?
    c = {year: year}
    c[:chamber] = "P"
    c[:state] = candidate['STATE ABBREVIATION']
    c[:party] = candidate['PARTY']
    c[:incumbent] = candidate['LAST NAME,  FIRST'] == 'Obama, Barack' ? true : false
    c[:fec_id] = candidate['FEC ID']
    c[:candidate_first] = candidate['LAST NAME,  FIRST']
    c[:candidate_last] = candidate['LAST NAME,  FIRST']
    c[:candidate_suffix] = candidate['LAST NAME,  FIRST'].split(', ').last if candidate['LAST NAME,  FIRST'].split(', ').size > 2
    c[:candidate_name] = candidate['LAST NAME,  FIRST']
    c[:general_votes] = candidate['PRIMARY RESULTS'].to_i
    c[:general_pct] = candidate['PRIMARY %'].to_f*100.0
    c[:general_winner] = nil
    results << c
  end
  Result.create_from_results(results)
end

#primary_party_summaryObject



27
28
29
# File 'lib/fec_results/president.rb', line 27

def primary_party_summary
  send("primary_party_summary_#{year}")
end

#primary_party_summary_2000(options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


131
132
133
# File 'lib/fec_results/president.rb', line 131

def primary_party_summary_2000(options={})
  raise NotImplementedError.new("Data not available for #{year}")
end

#primary_party_summary_2004(options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/fec_results/president.rb', line 121

def primary_party_summary_2004(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2004 Pres Primary Party Summary', skip: 1, headers: false)
  t.entries.each do |row|
    break if row[0] == 'Total Primary Votes Cast:'
    results << OpenStruct.new(party: row[0], total_votes: row[1].to_i)
  end
  results
end

#primary_party_summary_2008(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/fec_results/president.rb', line 111

def primary_party_summary_2008(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2008 Pres Primary Party Summary', skip: 1, headers: false)
  t.entries.each do |row|
    break if row[0] == 'Total Primary Votes:'
    results << OpenStruct.new(party: row[0], total_votes: row[1].to_i)
  end
  results
end

#primary_party_summary_2012(options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/fec_results/president.rb', line 101

def primary_party_summary_2012(options={})
  results = []
  t = RemoteTable.new(url, sheet: '2012 Pres Primary Party Summary', skip: 1, headers: false)
  t.entries.each do |row|
    break if row[0] == 'Total Primary Votes:'
    results << OpenStruct.new(party: row[0], total_votes: row[1].to_i)
  end
  results
end


23
24
25
# File 'lib/fec_results/president.rb', line 23

def state_electoral_and_popular_vote_summary(*args)
  send("state_electoral_and_popular_vote_summary_#{year}", *args)
end


91
92
93
94
95
96
97
98
99
# File 'lib/fec_results/president.rb', line 91

def state_electoral_and_popular_vote_summary_2004(options={})
  results = []
  t = RemoteTable.new(url, sheet: 'Table 2. Pres Elec & Pop Vote', skip: 2)
  t.entries.each do |row|
    break if row[0] == 'Total:'
    results << OpenStruct.new(state: row['STATE'], democratic_electoral_votes: row['Electoral Vote Kerry (D)'].to_i, republican_electoral_votes: row['Electoral Vote Bush (R)'].to_i, :democratic_popular_votes => row['Popular Vote Kerry (D)'].to_i, :republican_popular_votes => row['Popular Vote Bush (R)'].to_i, :other_popular_votes => row['Popular Vote All Others'].to_i, total_votes: row['Popular Vote Total Vote'].to_i)
  end
  results
end


81
82
83
84
85
86
87
88
89
# File 'lib/fec_results/president.rb', line 81

def state_electoral_and_popular_vote_summary_2008(options={})
  results = []
  t = RemoteTable.new(FecResults::SUMMARY_URLS[year.to_s], sheet: 'Table 2. Electoral &  Pop Vote', skip: 4, headers: false)
  t.entries.each do |row|
    break if row[0] == 'Total:'
    results << OpenStruct.new(state: row[0], democratic_electoral_votes: row[1].to_i, republican_electoral_votes: row[2].to_i, :democratic_popular_votes => row[3].to_i, :republican_popular_votes => row[4].to_i, :other_popular_votes => row[5].to_i, total_votes: row[6].to_i)
  end
  results
end


71
72
73
74
75
76
77
78
79
# File 'lib/fec_results/president.rb', line 71

def state_electoral_and_popular_vote_summary_2012(options={})
  results = []
  t = RemoteTable.new(url, sheet: 'Table 2. Electoral &  Pop Vote', skip: 4, headers: false)
  t.entries.each do |row|
    break if row[0] == 'Total:'
    results << OpenStruct.new(state: row[0], democratic_electoral_votes: row[1].to_i, republican_electoral_votes: row[2].to_i, :democratic_popular_votes => row[3].to_i, :republican_popular_votes => row[4].to_i, :other_popular_votes => row[5].to_i, total_votes: row[6].to_i)
  end
  results
end

#to_sObject



13
14
15
# File 'lib/fec_results/president.rb', line 13

def to_s
  "#<FecResults::President:#{year.to_s}>"
end