Created
November 7, 2012 01:44
-
-
Save axelrivera/4029044 to your computer and use it in GitHub Desktop.
Resultados Elecciones de Puerto Rico en Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# You should run `gem install httparty` first | |
require 'httparty' | |
def comma_numbers(number, delimiter = ',') | |
number.to_s.reverse.gsub(%r{([0-9]{3}(?=([0-9])))}, "\\1#{delimiter}").reverse | |
end | |
trap("INT") { puts "Shutting down."; exit } | |
puts "\nRESULTADOS ELECCIONES DE PUERTO RICO" | |
puts "GOBERNACION" | |
puts "----------\n\n" | |
loop do | |
response = HTTParty.get 'http://div1.ceepur.org/REYDI_NocheDelEvento/data/GOBERNADOR_ISLA.xml' | |
next if response.nil? | |
resultados = response['default']['option'].map do |r| | |
{ name: r['name']['es'], party: r['pe']['es'], votes: r['votes'].to_i } | |
end | |
puts "#{Time.now.strftime("%B %d, %Y %H:%I:%S %p")}\n\n" | |
total_votes = 0 | |
resultados.each { |r| total_votes += r[:votes] } | |
resultados.each do |r| | |
percentage = (r[:votes].to_f / total_votes.to_f) * 100 | |
percentage_str = sprintf("%.2f", percentage) | |
puts "#{r[:name]}" | |
puts "#{r[:party]}" | |
puts "Votes: #{comma_numbers(r[:votes])} -- #{percentage_str}%" | |
puts "----------" | |
end | |
puts "Total Votes: #{comma_numbers(total_votes)}" | |
puts "\n\n++++++++++\n\n" | |
sleep(5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment