Skip to content

Commit

Permalink
New bars
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Nov 9, 2016
1 parent 16fc014 commit 269aa74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
30 changes: 25 additions & 5 deletions lib/lita/handlers/onewheel_election_cnn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,33 @@ def election(response)
results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))

response.reply "United States 2016 Presidential Election, #{results['races'][0]['pctsrep']}% reporting."
votes = {'blue' => {}, 'red' => {}}
results['candidates'].each do |candidate|
candidate_str = "#{candidate['fname']} #{candidate['lname']}: "
candidate_str += "#{candidate['pctDecimal']}%, #{candidate['cvotes']} popular votes, #{candidate['evotes']} electoral votes."
candidate_str += " WINNER! " if candidate['winner']
Lita.logger.debug "Replying with #{candidate_str}"
response.reply candidate_str
if candidate['lname'] == 'Clinton'
votes['blue']['percentage'] = candidate['pctDecimal']
votes['blue']['popular'] = candidate['cvotes']
votes['blue']['electoral'] = candidate['evotes']
votes['blue']['winner'] = candidate['winner']
end

if candidate['lname'] == 'Trump'
votes['red']['percentage'] = candidate['pctDecimal']
votes['red']['popular'] = candidate['cvotes']
votes['red']['electoral'] = candidate['evotes']
votes['red']['winner'] = candidate['winner']
end
end

bluecount = (votes['blue']['percentage'].to_f / 2).to_i
redcount = (votes['red']['percentage'].to_f / 2).to_i

blueredstr = "\x0312"
bluecount.times { blueredstr += '█' }
blueredstr += "\x0300"
(50 - bluecount - redcount).times { blueredstr += '-' }
blueredstr += "\x0304"
redcount.times { blueredstr += '█' }
response.reply "Clinton #{votes['blue']['percentage']}% #{votes['blue']['popular']} |#{blueredstr}| Trump #{votes['blue']['percentage']}% #{votes['blue']['popular']}"
end

def election_by_state(response)
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 = '3.6.0'
spec.version = '4.0.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
6 changes: 2 additions & 4 deletions spec/lita/handlers/onewheel_election_cnn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
allow(RestClient).to receive(:get) { mock }
send_command 'election'
expect(replies[0]).to eq("United States 2016 Presidential Election, 1% reporting.")
expect(replies[1]).to eq("Hillary Clinton: 0.0%, 0 popular votes, 0 electoral votes.")
expect(replies[2]).to eq("Donald Trump: 0.0%, 0 popular votes, 19 electoral votes.")
expect(replies[1]).to eq("Clinton 0.0% 0 |\u000312\u000300--------------------------------------------------\u000304| Trump 0.0% 0")
end

it 'shows a winner' do
mock = File.open('spec/fixtures/winner.json').read
allow(RestClient).to receive(:get) { mock }
send_command 'election'
expect(replies[0]).to eq("United States 2016 Presidential Election, 1% reporting.")
expect(replies[1]).to eq("Hillary Clinton: 0.0%, 0 popular votes, 0 electoral votes. WINNER! ")
expect(replies[2]).to eq("Donald Trump: 0.0%, 0 popular votes, 0 electoral votes.")
expect(replies[1]).to eq("Clinton 0.0% 0 |\u000312\u000300--------------------------------------------------\u000304| Trump 0.0% 0")
end

it 'shows by state' do
Expand Down

0 comments on commit 269aa74

Please sign in to comment.