Skip to content

Commit

Permalink
Added state searching.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Nov 8, 2016
1 parent 8201bcb commit 09d6861
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
91 changes: 90 additions & 1 deletion lib/lita/handlers/onewheel_election_cnn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
module Lita
module Handlers
class OnewheelElectionCnn < Handler
route /^e[rl]ection/i,
route /^e[rl]ection$/i,
:election,
command: true,
help: {'election' => 'Display the current election results.'}

route /^e[rl]ection\s+(.*)$/i,
:election_by_state,
command: true,
help: {'election AB' => 'Display the current election results in Alabama.'}

def election(response)
Lita.logger.debug 'get_source started'
results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))
Expand All @@ -19,6 +24,90 @@ def election(response)
response.reply candidate_str
end
end

def election_by_state(response)
Lita.logger.debug 'get_source started'
results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))

state = stateness(response.matches[0][0])

results['races'].each do |race|
if race['state'].downcase == state.downcase
race['candidates'].each do |candidate|
candidate_str = "#{state} - #{candidate['fname']} #{candidate['lname']}: "
candidate_str += "WINNER! " if candidate['winner']
candidate_str += "#{candidate['pctDecimal']}%, #{candidate['vpct']}% of precincts reporting."
response.reply candidate_str
end
end
end
end

def stateness(gimme)
states = {"AK" => "Alaska",
"AL" => "Alabama",
"AR" => "Arkansas",
"AS" => "American Samoa",
"AZ" => "Arizona",
"CA" => "California",
"CO" => "Colorado",
"CT" => "Connecticut",
"DC" => "District of Columbia",
"DE" => "Delaware",
"FL" => "Florida",
"GA" => "Georgia",
"GU" => "Guam",
"HI" => "Hawaii",
"IA" => "Iowa",
"ID" => "Idaho",
"IL" => "Illinois",
"IN" => "Indiana",
"KS" => "Kansas",
"KY" => "Kentucky",
"LA" => "Louisiana",
"MA" => "Massachusetts",
"MD" => "Maryland",
"ME" => "Maine",
"MI" => "Michigan",
"MN" => "Minnesota",
"MO" => "Missouri",
"MS" => "Mississippi",
"MT" => "Montana",
"NC" => "North Carolina",
"ND" => "North Dakota",
"NE" => "Nebraska",
"NH" => "New Hampshire",
"NJ" => "New Jersey",
"NM" => "New Mexico",
"NV" => "Nevada",
"NY" => "New York",
"OH" => "Ohio",
"OK" => "Oklahoma",
"OR" => "Oregon",
"PA" => "Pennsylvania",
"PR" => "Puerto Rico",
"RI" => "Rhode Island",
"SC" => "South Carolina",
"SD" => "South Dakota",
"TN" => "Tennessee",
"TX" => "Texas",
"UT" => "Utah",
"VA" => "Virginia",
"VI" => "Virgin Islands",
"VT" => "Vermont",
"WA" => "Washington",
"WI" => "Wisconsin",
"WV" => "West Virginia",
"WY" => "Wyoming"}

search_state = gimme.upcase
if search_state.length == 2
states[search_state.upcase]
else
search_state
end
end

Lita.register_handler(self)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lita-onewheel-election-cnn.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'lita-onewheel-election-cnn'
spec.version = '0.0.0'
spec.version = '0.1.0'
spec.authors = ['Andrew Kreps']
spec.email = ['andrew.kreps@gmail.com']
spec.description = %q{Lita interface to CNN's 2016 Presidential Election JSON.}
Expand Down
24 changes: 24 additions & 0 deletions spec/lita/handlers/onewheel_election_cnn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,28 @@
expect(replies[0]).to eq("Hillary Clinton: WINNER! 0.0%, 0% of precincts reporting.")
expect(replies[1]).to eq("Donald Trump: 0.0%, 0% of precincts reporting.")
end

it 'shows by state' do
mock = File.open('spec/fixtures/election.json').read
allow(RestClient).to receive(:get) { mock }
send_command 'election va'
expect(replies[0]).to eq("Virginia - Hillary Clinton: 0.0%, 0% of precincts reporting.")
expect(replies[1]).to eq("Virginia - Donald Trump: 0.0%, 0% of precincts reporting.")
end

it 'shows by STATE' do
mock = File.open('spec/fixtures/election.json').read
allow(RestClient).to receive(:get) { mock }
send_command 'election VA'
expect(replies[0]).to eq("Virginia - Hillary Clinton: 0.0%, 0% of precincts reporting.")
expect(replies[1]).to eq("Virginia - Donald Trump: 0.0%, 0% of precincts reporting.")
end

it 'shows by full state name downcase' do
mock = File.open('spec/fixtures/election.json').read
allow(RestClient).to receive(:get) { mock }
send_command 'election new york'
expect(replies[0]).to eq("NEW YORK - Hillary Clinton: 0.0%, 0% of precincts reporting.")
expect(replies[1]).to eq("NEW YORK - Donald Trump: 0.0%, 0% of precincts reporting.")
end
end

0 comments on commit 09d6861

Please sign in to comment.