Skip to content

Commit

Permalink
Refactor run and logging update.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Nov 9, 2016
1 parent 087eb19 commit db82e3f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 70 deletions.
137 changes: 68 additions & 69 deletions lib/lita/handlers/onewheel_election_cnn.rb
Expand Up @@ -28,40 +28,17 @@ def election(response)
response.reply "\x0300United States 2016 Presidential Election, #{results['races'][0]['pctsrep']}% reporting."
votes = {'blue' => {}, 'red' => {}}
results['candidates'].each do |candidate|
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
votes = get_reds_and_blues(candidate, votes)
end

blueredstr = get_blueredstr(votes)
response.reply "\x0300Clinton #{votes['blue']['percentage']}% #{votes['blue']['popular']} |#{blueredstr}\x0300| Trump #{votes['red']['percentage']}% #{votes['red']['popular']}"
reply = "\x0300Clinton #{votes['blue']['percentage']}% #{votes['blue']['popular']} |#{blueredstr}\x0300| Trump #{votes['red']['percentage']}% #{votes['red']['popular']}"
Lita.logger.debug reply
response.reply reply

ansielection(response, results)
end

def get_blueredstr(votes)
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 += '█' }
blueredstr
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'))
Expand All @@ -75,27 +52,77 @@ def election_by_state(response)
response.reply state_reply
Lita.logger.debug "Replying with #{state_reply}"
race['candidates'].each do |candidate|
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
votes = get_reds_and_blues(candidate, votes)
end

blueredstr = get_blueredstr(votes)
response.reply "\x0300Clinton #{(votes['blue']['winner'] == true)? ' WINNER! ' : '' }#{votes['blue']['percentage']}% #{votes['blue']['popular']} |#{blueredstr}\x0300| Trump #{votes['red']['percentage']}% #{votes['red']['popular']}"
reply = "\x0300Clinton #{(votes['blue']['winner'] == true) ? ' WINNER! ' : '' }#{votes['blue']['percentage']}% #{votes['blue']['popular']} |#{blueredstr}\x0300| Trump #{votes['red']['percentage']}% #{votes['red']['popular']}"
Lita.logger.debug reply
response.reply reply
end
end
end

def ansielection(response, results)
reds = 0
blues = 0
results['candidates'].each do |candidate|
if candidate['lname'] == 'Clinton'
blues = candidate['evotes']
end
if candidate['lname'] == 'Trump'
reds = candidate['evotes']
end
end

reply = ''
extras = 54 - (blues / 10) - (reds / 10)
reply += "\x0312"
(blues / 10).times { reply += '█' }
reply += "\x0300"
extras.times { reply += '-'}
reply += "\x0304"
(reds / 10).times { reply += '█' }

reply.insert((reply.length / 2) + 3, "👽")

reply = "\x0300Clinton #{blues} |" + reply + "\x0300| Trump #{reds}"

Lita.logger.debug reply
response.reply reply
end

def get_reds_and_blues(candidate, votes)
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

votes
end

def get_blueredstr(votes)
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 += '█' }
blueredstr
end

def stateness(gimme)
states = {"AK" => "Alaska",
"AL" => "Alabama",
Expand Down Expand Up @@ -163,34 +190,6 @@ def stateness(gimme)
end
end

def ansielection(response, results)
reds = 0
blues = 0
results['candidates'].each do |candidate|
if candidate['lname'] == 'Clinton'
blues = candidate['evotes']
end
if candidate['lname'] == 'Trump'
reds = candidate['evotes']
end
end

reply = ''
extras = 54 - (blues / 10) - (reds / 10)
reply += "\x0312"
(blues / 10).times { reply += '█' }
reply += "\x0300"
extras.times { reply += '-'}
reply += "\x0304"
(reds / 10).times { reply += '█' }

reply.insert((reply.length / 2) + 3, "👽")

reply = "\x0300Clinton #{blues} |" + reply + "\x0300| Trump #{reds}"

response.reply reply
end

Lita.register_handler(self)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lita-onewheel-election-cnn.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'lita-onewheel-election-cnn'
spec.version = '5.1.0'
spec.version = '5.1.1'
spec.authors = ['Andrew Kreps']
spec.email = ['andrew.kreps@gmail.com']
spec.description = %q{Lita interface to CNN's 2016 Presidential Election JSON.}
Expand Down

0 comments on commit db82e3f

Please sign in to comment.