Skip to content

Instantly share code, notes, and snippets.

@tallclouds
Forked from axelrivera/resultados.rb
Created November 7, 2012 04:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tallclouds/4029512 to your computer and use it in GitHub Desktop.
Save tallclouds/4029512 to your computer and use it in GitHub Desktop.
Resultados Elecciones de Puerto Rico en Ruby
#!/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
puts "\nRESULTADOS ELECCIONES DE PUERTO RICO"
puts "GOBERNACION"
puts "----------\n\n"
changed = true
previous_total_votes = 0
first_run = true
system('say Monitoring Results for Puerto Rico Gobernor Race')
loop do
response = HTTParty.get 'http://div1.ceepur.org/REYDI_NocheDelEvento/data/GOBERNADOR_ISLA.xml'
resultados = response['default']['option'].map do |r|
{ name: r['name']['es'], party: r['pe']['es'], votes: r['votes'].to_i }
end
total_votes = 0
resultados.each { |r| total_votes += r[:votes] }
if(total_votes - previous_total_votes > 0)
resultados.each do |r|
percentage = (r[:votes].to_f / total_votes.to_f) * 100
percentage_str = sprintf("%.2f", percentage)
votes = comma_numbers(r[:votes])
puts "#{Time.now.strftime("%B %d, %Y %H:%I:%S %p")}\n\n"
puts "#{r[:name]}"
puts "#{r[:party]}"
puts "Votes: #{comma_numbers(votes)} -- #{percentage_str}%"
puts "----------"
end
puts "Total Votes: #{comma_numbers(total_votes)}"
puts "\n\n++++++++++\n\n"
previous_total_votes = total_votes
if(first_run)
system('say These are the latest results')
else
system('say New Results Available')
end
end
sleep(5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment