open_fec_api 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CREDITS.md +1 -0
- data/README.md +24 -18
- data/lib/open_fec_api.rb +2 -0
- data/lib/open_fec_api/client.rb +47 -12
- data/lib/open_fec_api/endpoint.rb +28 -0
- data/lib/open_fec_api/response.rb +1 -0
- data/lib/open_fec_api/responses/candidates_response.rb +4 -0
- data/lib/open_fec_api/responses/committees_response.rb +4 -0
- data/lib/open_fec_api/version.rb +1 -1
- data/{reponse_examples/candidates.rb → mock_responses/mock_candidates_response.rb} +0 -0
- data/mock_responses/mock_committees_response.rb +20 -0
- data/{reponse_examples/over_rate_limit.rb → mock_responses/over_rate_limit_response.rb} +0 -0
- data/open_fec_api.gemspec +3 -3
- data/spec/client_spec.rb +33 -1
- metadata +13 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 124251aad3027ab11a34736940eadde4bc61ed1a
|
4
|
+
data.tar.gz: efd3d0bbad721efb73c40f33862ee35b964c03bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b30b9116a9d873685d781228d73c292d43014b397063061c210e4a0f6581aaeb22d6956571348e3eb95d09553f89b17a3859960503583ccde2fe53a1805764d5
|
7
|
+
data.tar.gz: 18e3e371fe7c2fb84039196cb6bda871cb22398f84c1f104d19a7771a0f37954fe8beed943405656eb484f84a99d47e3d514c23146d73ab5f682fd16a48655cb
|
data/CREDITS.md
CHANGED
data/README.md
CHANGED
@@ -2,43 +2,44 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/open_fec_api.svg)](http://badge.fury.io/rb/open_fec_api)
|
4
4
|
|
5
|
-
A ruby
|
5
|
+
A ruby interface to the [Open Federal Elections Commission (FEC) API](https://api.open.fec.gov/). Returns data about election candidates and committees. Includes options to customize API requests.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
```` sh
|
10
|
+
gem install open_fec_api
|
11
|
+
````
|
12
12
|
|
13
|
-
|
13
|
+
## Prerequisites
|
14
14
|
|
15
|
-
|
16
|
-
client = OpenFecApi::Client.new("api_key_123")
|
17
|
-
````
|
15
|
+
Obtain an [API key](https://api.data.gov/signup/).
|
18
16
|
|
19
17
|
## Usage
|
20
18
|
|
21
|
-
Make a request.
|
22
|
-
|
23
19
|
```` rb
|
24
|
-
|
20
|
+
client = OpenFecApi::Client.new("api_key_123")
|
21
|
+
candidates_response = client.candidates
|
22
|
+
committees_response = client.committees
|
25
23
|
````
|
24
|
+
### Configuration Options
|
26
25
|
|
27
|
-
Request
|
26
|
+
Request subsequent pages and avoid rate-limits.
|
28
27
|
|
29
28
|
```` rb
|
30
29
|
options = {:page => 1, :per_page => 100}
|
31
30
|
response = client.candidates(options)
|
31
|
+
|
32
32
|
while response.page < response.pages do
|
33
33
|
options.merge!({:page => response.page + 1})
|
34
34
|
response = client.candidates(options)
|
35
35
|
end
|
36
36
|
````
|
37
37
|
|
38
|
-
|
38
|
+
Specify endpoint-specific parameters.
|
39
39
|
|
40
40
|
```` rb
|
41
41
|
options = {:party => "DEM"}
|
42
|
+
|
42
43
|
response = client.candidates(options)
|
43
44
|
````
|
44
45
|
|
@@ -50,19 +51,24 @@ Browse existing issues or create a new issue to communicate bugs, desired featur
|
|
50
51
|
|
51
52
|
After forking the repo and pushing your changes, create a pull request referencing the applicable issue(s).
|
52
53
|
|
53
|
-
###
|
54
|
+
### Developing
|
55
|
+
|
56
|
+
```` sh
|
57
|
+
git clone git@github.com:data-creative/open-fec-api-ruby.git
|
58
|
+
cd open-fec-api-ruby
|
59
|
+
````
|
54
60
|
|
55
|
-
|
61
|
+
Run `bin/setup` to install dependencies.
|
56
62
|
|
57
|
-
|
63
|
+
Obtain an API key and assign it to an environment variable called `OPEN_FEC_API_KEY`.
|
58
64
|
|
59
65
|
### Testing
|
60
66
|
|
61
67
|
Run `bundle exec rake` or `bundle exec rspec spec/` to run the tests.
|
62
68
|
|
63
|
-
|
69
|
+
Run `bin/console` for an interactive prompt to facilitate experiment.
|
64
70
|
|
65
|
-
|
71
|
+
Run `bundle exec rake install` to install the packaged gem onto your local machine for testing in other local projects.
|
66
72
|
|
67
73
|
### Releasing
|
68
74
|
|
data/lib/open_fec_api.rb
CHANGED
data/lib/open_fec_api/client.rb
CHANGED
@@ -2,10 +2,12 @@ require 'httparty'
|
|
2
2
|
|
3
3
|
module OpenFecApi
|
4
4
|
class Client
|
5
|
-
attr_reader :api_key
|
6
5
|
include HTTParty
|
6
|
+
|
7
7
|
base_uri 'https://api.open.fec.gov/v1'
|
8
8
|
|
9
|
+
attr_reader :api_key
|
10
|
+
|
9
11
|
def initialize(api_key)
|
10
12
|
@api_key = api_key
|
11
13
|
end
|
@@ -14,10 +16,15 @@ module OpenFecApi
|
|
14
16
|
!self.api_key.nil?
|
15
17
|
end
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
def request_params
|
20
|
+
[
|
21
|
+
"page", "per_page", "year", "designation", "committee_type", "organization_type",
|
22
|
+
"cycle", "party", "min_first_file_date", "candidate_id", "state", "committee_id",
|
23
|
+
"name", "q", "max_first_file_date", "sort", "sort_hide_null", "sort_nulls_large"
|
24
|
+
]
|
25
|
+
end # NOTE: these params don't apply to all available endpoints ...
|
26
|
+
|
27
|
+
# @param [String] endpoint One of: ["candidates/","committees/"]
|
21
28
|
# @param [Hash] options
|
22
29
|
# option options Array[string] :sort Provide a field to sort by. Use - for descending order.
|
23
30
|
# option options Boolean :sort_hide_null Hide null values on sorted column(s).
|
@@ -35,17 +42,45 @@ module OpenFecApi
|
|
35
42
|
# option options Integer :page For paginating through results, starting at page 1.
|
36
43
|
# option options Integer :per_page The number of results returned per page. Defaults to 20.
|
37
44
|
#
|
38
|
-
|
39
|
-
|
40
|
-
def candidates(options = {})
|
41
|
-
query = {'api_key' => @api_key}
|
42
|
-
request_params = ["sort", "sort_hide_null", "year", "office", "candidate_status", "party", "state", "cycle", "district", "incumbent_challenge", "name", "candidate_id", "page", "per_page"]
|
45
|
+
def get_response(endpoint, options = {})
|
46
|
+
endpoint_name = endpoint.gsub("/","")
|
43
47
|
request_options = options.select{|k,v| request_params.include?(k.to_s)}
|
48
|
+
|
49
|
+
# Parse/compile query params.
|
50
|
+
|
51
|
+
query = {'api_key' => @api_key}
|
44
52
|
request_options.each do |k,v|
|
45
53
|
query.merge!({k.to_s => v})
|
46
54
|
end
|
47
|
-
|
48
|
-
|
55
|
+
|
56
|
+
# Make a request.
|
57
|
+
|
58
|
+
response = self.class.get(endpoint, query: query)
|
59
|
+
|
60
|
+
# Return the proper response.
|
61
|
+
|
62
|
+
response_class_name = endpoint_name.capitalize.concat("Response")
|
63
|
+
return OpenFecApi.const_get(response_class_name).new(response) # response_class_name.constantize.new(response)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Candidates Endpoint
|
67
|
+
#
|
68
|
+
# https://api.open.fec.gov/developers#!/candidate/get_candidates
|
69
|
+
#
|
70
|
+
# @example
|
71
|
+
# OpenFecApi::Client.new(:api_key => API_KEY).candidates(:page => 1, :per_page => 100)
|
72
|
+
def candidates(options = {})
|
73
|
+
get_response("/candidates", options)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Committees Endpoint
|
77
|
+
#
|
78
|
+
# https://api.open.fec.gov/developers#!/committee/get_committees
|
79
|
+
#
|
80
|
+
# @example
|
81
|
+
# OpenFecApi::Client.new(:api_key => API_KEY).committees(:page => 1, :per_page => 100)
|
82
|
+
def committees(options = {})
|
83
|
+
get_response("/committees", options)
|
49
84
|
end
|
50
85
|
end
|
51
86
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
=begin
|
2
|
+
module OpenFecApi
|
3
|
+
class Endpoint
|
4
|
+
ENDPOINTS = [
|
5
|
+
{
|
6
|
+
:path => "/candidates",
|
7
|
+
:request_params => [
|
8
|
+
"page", "per_page", "year", "designation", "committee_type", "organization_type", "cycle", "party", "min_first_file_date", "candidate_id", "state", "committee_id", "name", "q", "max_first_file_date", "sort", "sort_hide_null", "sort_nulls_large"
|
9
|
+
# TODO: {:name => "page"}, {:name => "per_page"}
|
10
|
+
]
|
11
|
+
},
|
12
|
+
{
|
13
|
+
:path => "/committees",
|
14
|
+
:request_params => [
|
15
|
+
"page", "per_page", "year", "designation", "committee_type", "organization_type", "cycle", "party", "min_first_file_date", "candidate_id", "state", "committee_id", "name", "q", "max_first_file_date", "sort", "sort_hide_null", "sort_nulls_large"
|
16
|
+
# TODO: {:name => "page"}, {:name => "per_page"}
|
17
|
+
]
|
18
|
+
}
|
19
|
+
]
|
20
|
+
|
21
|
+
attr_accessor :request_params
|
22
|
+
|
23
|
+
def initialize(options = {})
|
24
|
+
@request_params = options[:request_params]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
=end
|
data/lib/open_fec_api/version.rb
CHANGED
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
{"pagination"=>{"page"=>1, "pages"=>1, "count"=>1, "per_page"=>20},
|
2
|
+
"api_version"=>"1.0",
|
3
|
+
"results"=>
|
4
|
+
[{"last_file_date"=>"2014-06-10",
|
5
|
+
"treasurer_name"=>"RANDI M WILLIS",
|
6
|
+
"organization_type_full"=>nil,
|
7
|
+
"party"=>nil,
|
8
|
+
"designation_full"=>"Unauthorized",
|
9
|
+
"name"=>"10 ^ 9+",
|
10
|
+
"committee_type_full"=>"Super PAC (Independent Expenditure-Only)",
|
11
|
+
"committee_id"=>"C00563023",
|
12
|
+
"first_file_date"=>"2014-05-16",
|
13
|
+
"committee_type"=>"O",
|
14
|
+
"cycles"=>[2014],
|
15
|
+
"expire_date"=>nil,
|
16
|
+
"organization_type"=>nil,
|
17
|
+
"state"=>"CA",
|
18
|
+
"party_full"=>nil,
|
19
|
+
"designation"=>"U",
|
20
|
+
"candidate_ids"=>[]}]}
|
File without changes
|
data/open_fec_api.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = OpenFecApi::VERSION
|
9
9
|
spec.authors = ["MJ Rossetti (@s2t2)"]
|
10
10
|
spec.email = ["s2t2mail@gmail.com"]
|
11
|
-
spec.summary = %q{A ruby
|
12
|
-
spec.description = %q{A ruby
|
13
|
-
spec.homepage = "https://github.com/
|
11
|
+
spec.summary = %q{A ruby interface to the Open Federal Elections Commission (FEC) API.}
|
12
|
+
spec.description = %q{A ruby interface to the Open Federal Elections Commission (FEC) API. Returns data about election candidates and committees. Includes options to customize API requests.}
|
13
|
+
spec.homepage = "https://github.com/data-creative/open-fec-api-ruby/"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
data/spec/client_spec.rb
CHANGED
@@ -2,6 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module OpenFecApi
|
4
4
|
RSpec.describe Client do
|
5
|
+
|
6
|
+
#
|
7
|
+
# Candidates Endpoint
|
8
|
+
#
|
9
|
+
|
5
10
|
describe '#candidates' do
|
6
11
|
context 'when configured' do
|
7
12
|
before do
|
@@ -39,7 +44,7 @@ module OpenFecApi
|
|
39
44
|
options = {:party => "DEM"}
|
40
45
|
response = request_and_print(options)
|
41
46
|
expect(response.results.map{|c| c["party"]}.uniq).to eql(["DEM"])
|
42
|
-
end
|
47
|
+
end #todo: maybe return mock response instead
|
43
48
|
|
44
49
|
it "prevents unrecognized params from being requested" do
|
45
50
|
unrecognized_params = {:hair_color => "brown"}
|
@@ -49,5 +54,32 @@ module OpenFecApi
|
|
49
54
|
end
|
50
55
|
end
|
51
56
|
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Committees Endpoint
|
60
|
+
#
|
61
|
+
|
62
|
+
describe '#committees' do
|
63
|
+
context 'when configured' do
|
64
|
+
let(:committee_id){"C00563023"}
|
65
|
+
|
66
|
+
before do
|
67
|
+
@client = OpenFecApi::Client.new(ENV["OPEN_FEC_API_KEY"])
|
68
|
+
end
|
69
|
+
|
70
|
+
def request_and_print(options)
|
71
|
+
response = @client.committees(options)
|
72
|
+
pp response.summary
|
73
|
+
return response
|
74
|
+
end #todo: maybe return mock response instead
|
75
|
+
|
76
|
+
it "accepts accepts endpoint-specific options like Committee ID" do
|
77
|
+
options = {:committee_id => committee_id}
|
78
|
+
response = request_and_print(options)
|
79
|
+
committe_treasurer_name = response.results.map{|c| c["treasurer_name"]}.uniq
|
80
|
+
expect(committe_treasurer_name).to eql(["RANDI M WILLIS"])
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
52
84
|
end
|
53
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_fec_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MJ Rossetti (@s2t2)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,8 +94,9 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.13'
|
97
|
-
description: A ruby
|
98
|
-
|
97
|
+
description: A ruby interface to the Open Federal Elections Commission (FEC) API.
|
98
|
+
Returns data about election candidates and committees. Includes options to customize
|
99
|
+
API requests.
|
99
100
|
email:
|
100
101
|
- s2t2mail@gmail.com
|
101
102
|
executables:
|
@@ -116,14 +117,18 @@ files:
|
|
116
117
|
- bin/setup
|
117
118
|
- lib/open_fec_api.rb
|
118
119
|
- lib/open_fec_api/client.rb
|
120
|
+
- lib/open_fec_api/endpoint.rb
|
119
121
|
- lib/open_fec_api/response.rb
|
122
|
+
- lib/open_fec_api/responses/candidates_response.rb
|
123
|
+
- lib/open_fec_api/responses/committees_response.rb
|
120
124
|
- lib/open_fec_api/version.rb
|
125
|
+
- mock_responses/mock_candidates_response.rb
|
126
|
+
- mock_responses/mock_committees_response.rb
|
127
|
+
- mock_responses/over_rate_limit_response.rb
|
121
128
|
- open_fec_api.gemspec
|
122
|
-
- reponse_examples/candidates.rb
|
123
|
-
- reponse_examples/over_rate_limit.rb
|
124
129
|
- spec/client_spec.rb
|
125
130
|
- spec/spec_helper.rb
|
126
|
-
homepage: https://github.com/
|
131
|
+
homepage: https://github.com/data-creative/open-fec-api-ruby/
|
127
132
|
licenses:
|
128
133
|
- MIT
|
129
134
|
metadata: {}
|
@@ -146,7 +151,7 @@ rubyforge_project:
|
|
146
151
|
rubygems_version: 2.4.5
|
147
152
|
signing_key:
|
148
153
|
specification_version: 4
|
149
|
-
summary: A ruby
|
154
|
+
summary: A ruby interface to the Open Federal Elections Commission (FEC) API.
|
150
155
|
test_files:
|
151
156
|
- spec/client_spec.rb
|
152
157
|
- spec/spec_helper.rb
|