class FecResultsGenerator::JsonGenerator
Attributes
year[R]
Public Class Methods
new(params={})
click to toggle source
# File lib/fec_results_generator.rb, line 10 def initialize(params={}) params.each_pair do |k,v| instance_variable_set("@#{k}", v) end end
Public Instance Methods
congress()
click to toggle source
# File lib/fec_results_generator.rb, line 47 def congress fec_results = FecResults::Congress.new(:year => year) create_dir(year, 'congress') run_results('results', fec_results, 'congress') end
create_dir(year, type)
click to toggle source
# File lib/fec_results_generator.rb, line 77 def create_dir(year, type) FileUtils.mkdir_p "api/#{year}/#{type}/" end
generate_json(results)
click to toggle source
# File lib/fec_results_generator.rb, line 63 def generate_json(results) data = results.map{|r| JSON.parse(r.to_json)} end
president()
click to toggle source
# File lib/fec_results_generator.rb, line 28 def president fec_results = FecResults::President.new(:year => year) create_dir(year, 'president') ['popular_vote_summary', 'state_electoral_and_popular_vote_summary', 'primary_party_summary'].each do |method| begin run(method, fec_results, 'president') rescue NotImplementedError, NoMethodError run_empty(method, 'president') end end ['general_election_results', 'primary_election_results'].each do |method| begin run_results(method, fec_results, 'president') rescue NotImplementedError, NoMethodError run_empty(method, 'president') end end end
run(method, fec_results, type)
click to toggle source
# File lib/fec_results_generator.rb, line 53 def run(method, fec_results, type) data = generate_json(fec_results.send(method)) write_to_file("api/#{year}/#{type}","#{method}.json", data.map{|d| d['table'].to_json}) end
run_empty(method, type)
click to toggle source
# File lib/fec_results_generator.rb, line 67 def run_empty(method, type) write_to_file("api/#{year}/#{type}","#{method}.json", []) end
run_results(method, fec_results, type)
click to toggle source
# File lib/fec_results_generator.rb, line 58 def run_results(method, fec_results, type) data = generate_json(fec_results.send(method)) write_to_file("api/#{year}/#{type}","#{method}.json", data.map{|d| d.to_json}) end
summary()
click to toggle source
# File lib/fec_results_generator.rb, line 16 def summary fec_results = FecResults::Summary.new(:year => year) create_dir(year, 'summary') ['party_labels', 'general_election_votes','general_election_votes_by_party','congressional_votes_by_election','chamber_votes_by_party'].each do |method| begin run(method, fec_results, 'summary') rescue NotImplementedError run_empty(method, 'summary') end end end
write_to_file(dir, file, json)
click to toggle source
# File lib/fec_results_generator.rb, line 71 def write_to_file(dir, file, json) File.open(File.join(dir, file), 'w') do |f| PP.pp(json,f) end end