Class: FecResultsGenerator::JsonGenerator
- Inherits:
-
Object
- Object
- FecResultsGenerator::JsonGenerator
- Defined in:
- lib/fec_results_generator.rb
Instance Attribute Summary collapse
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Instance Method Summary collapse
- #congress ⇒ Object
- #create_dir(year, type) ⇒ Object
- #generate_json(results) ⇒ Object
-
#initialize(params = {}) ⇒ JsonGenerator
constructor
A new instance of JsonGenerator.
- #president ⇒ Object
- #run(method, fec_results, type) ⇒ Object
- #run_empty(method, type) ⇒ Object
- #run_results(method, fec_results, type) ⇒ Object
- #summary ⇒ Object
- #write_to_file(dir, file, json) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ JsonGenerator
Returns a new instance of JsonGenerator.
10 11 12 13 14 |
# File 'lib/fec_results_generator.rb', line 10 def initialize(params={}) params.each_pair do |k,v| instance_variable_set("@#{k}", v) end end |
Instance Attribute Details
#year ⇒ Object (readonly)
Returns the value of attribute year.
8 9 10 |
# File 'lib/fec_results_generator.rb', line 8 def year @year end |
Instance Method Details
#congress ⇒ Object
47 48 49 50 51 |
# 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) ⇒ Object
77 78 79 |
# File 'lib/fec_results_generator.rb', line 77 def create_dir(year, type) FileUtils.mkdir_p "api/#{year}/#{type}/" end |
#generate_json(results) ⇒ Object
63 64 65 |
# File 'lib/fec_results_generator.rb', line 63 def generate_json(results) data = results.map{|r| JSON.parse(r.to_json)} end |
#president ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
53 54 55 56 |
# 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) ⇒ Object
67 68 69 |
# 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) ⇒ Object
58 59 60 61 |
# 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 ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# 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) ⇒ Object
71 72 73 74 75 |
# 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 |