Skip to content

Instantly share code, notes, and snippets.

@ch1ago
Last active August 29, 2015 14:07
Show Gist options
  • Save ch1ago/1ee4f17473317ff635fc to your computer and use it in GitHub Desktop.
Save ch1ago/1ee4f17473317ff635fc to your computer and use it in GitHub Desktop.
require "net/http"
require "uri"
require 'date'
require "json"
require "colorize"
class Eleicoes
def initialize
@data = get_data
end
attr_reader :data
def hora
# DateTime.parse(data['dataHora']).to_s
data['dataHora']
end
def candidatos
@top_candidatos ||= data['candidatos'].sort_by { |c| c['votos']['quantidade'] }.reverse[0..2]
end
def cores
%i[green yellow blue]
end
private
def get_data
uri = URI("http://g1.globo.com/eleicoes_2014/apuracao/1-turno/br/presidente.json")
response = Net::HTTP.get_response(uri)
JSON(response.body)
end
end
while true
e = Eleicoes.new
puts e.hora
e.candidatos.each_with_index do |c, i|
votos = c['votos']['quantidade'].to_s.rjust(10, '0').scan(/.../).join('.')
pct = c['votos']['porcentagem']
titulo = "\n#{i+1} - #{pct}% - #{votos} votos - #{c['nome']} - #{c['partido']} #{c['numero']}\n"
print titulo.colorize(background: e.cores[i])
end
sleep 60
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment