fec_results_generator 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/fec_results_generator.gemspec +24 -0
- data/lib/fec_results_generator/version.rb +3 -0
- data/lib/fec_results_generator.rb +72 -0
- metadata +108 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Derek Willis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# FecResultsGenerator
|
2
|
+
|
3
|
+
A Ruby gem that takes Federal Election Commission election results as retrieved by the FecResults gem and generates JSON files in a directory structure, suitable for producting a static API. You can see an example of the data at the [FecResults site](http://openelections.github.io/fec_results/api/2012/summary/general_election_votes.json).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fec_results_generator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fec_results_generator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In `irb`, create an instance of the JSONGenerator class:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
g = FecResultsGenerator::JsonGenerator.new(:year => 2000)
|
25
|
+
g.congress # for congressional results
|
26
|
+
g.president # for presidential results
|
27
|
+
g.summary # for vote totals
|
28
|
+
```
|
29
|
+
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it ( http://github.com/<my-github-username>/fec_results_generator/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fec_results_generator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fec_results_generator"
|
8
|
+
spec.version = FecResultsGenerator::VERSION
|
9
|
+
spec.authors = ["Derek Willis"]
|
10
|
+
spec.email = ["dwillis@gmail.com"]
|
11
|
+
spec.summary = %q{Generates JSON files based on the FecResults gem.}
|
12
|
+
spec.description = %q{For use in building a static-site API.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_dependency "fec_results"
|
24
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require "fec_results_generator/version"
|
2
|
+
require "fec_results"
|
3
|
+
require "pp"
|
4
|
+
|
5
|
+
module FecResultsGenerator
|
6
|
+
class JsonGenerator
|
7
|
+
|
8
|
+
attr_reader :year
|
9
|
+
|
10
|
+
def initialize(params={})
|
11
|
+
params.each_pair do |k,v|
|
12
|
+
instance_variable_set("@#{k}", v)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def summary
|
17
|
+
fec_results = FecResults::Summary.new(:year => year)
|
18
|
+
create_dir(year, 'summary')
|
19
|
+
['party_labels', 'general_election_votes','general_election_votes_by_party','congressional_votes_by_election','chamber_votes_by_party'].each do |method|
|
20
|
+
begin
|
21
|
+
run(method, fec_results, 'summary')
|
22
|
+
rescue NotImplementedError
|
23
|
+
next
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def president
|
29
|
+
fec_results = FecResults::President.new(:year => year)
|
30
|
+
create_dir(year, 'president')
|
31
|
+
['popular_vote_summary', 'state_electoral_and_popular_vote_summary', 'primary_party_summary', 'general_election_results', 'primary_election_results'].each do |method|
|
32
|
+
begin
|
33
|
+
run(method, fec_results, 'president')
|
34
|
+
rescue NotImplementedError, NoMethodError
|
35
|
+
next
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def congress
|
41
|
+
fec_results = FecResults::Congress.new(:year => year)
|
42
|
+
create_dir(year, 'congress')
|
43
|
+
run_congress('results', fec_results, 'congress')
|
44
|
+
end
|
45
|
+
|
46
|
+
def run(method, fec_results, type)
|
47
|
+
data = generate_json(fec_results.send(method))
|
48
|
+
write_to_file("api/#{year}/#{type}","#{method}.json", data.map{|d| d['table'].to_json})
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_congress(method, fec_results, type)
|
52
|
+
data = generate_json(fec_results.send(method))
|
53
|
+
write_to_file("api/#{year}/#{type}","#{method}.json", data.map{|d| d.to_json})
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_json(results)
|
57
|
+
data = results.map{|r| JSON.parse(r.to_json)}
|
58
|
+
end
|
59
|
+
|
60
|
+
def write_to_file(dir, file, json)
|
61
|
+
File.open(File.join(dir, file), 'w') do |f|
|
62
|
+
PP.pp(json,f)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def create_dir(year, type)
|
67
|
+
FileUtils.mkdir_p "api/#{year}/#{type}/"
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fec_results_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Derek Willis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: fec_results
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: For use in building a static-site API.
|
63
|
+
email:
|
64
|
+
- dwillis@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- fec_results_generator.gemspec
|
75
|
+
- lib/fec_results_generator.rb
|
76
|
+
- lib/fec_results_generator/version.rb
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: -1909023395731726773
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -1909023395731726773
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.25
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Generates JSON files based on the FecResults gem.
|
108
|
+
test_files: []
|