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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6a8af8d99c9b3e4a5fce6d0e34dd94f3af1775d
4
- data.tar.gz: 5f057b2396cdcbc32338cbbf79de9bb3917f30d9
3
+ metadata.gz: 124251aad3027ab11a34736940eadde4bc61ed1a
4
+ data.tar.gz: efd3d0bbad721efb73c40f33862ee35b964c03bd
5
5
  SHA512:
6
- metadata.gz: fb251bdd6f007da64cae9b0b87166e43eb904e0ab19668b6f4eacb63844a62a312b1462c623a7839ed0e932aff8455d55978adea72e494fd5e97eebb61a423f2
7
- data.tar.gz: c9ded79cc265e40f04672aad96b18f33f64540777a1c8d6b18518714fd0feb1bd7031bf3356f9236ac43749efbd5e7eb827e69f724acdb8b007b413c75dd6fd0
6
+ metadata.gz: b30b9116a9d873685d781228d73c292d43014b397063061c210e4a0f6581aaeb22d6956571348e3eb95d09553f89b17a3859960503583ccde2fe53a1805764d5
7
+ data.tar.gz: 18e3e371fe7c2fb84039196cb6bda871cb22398f84c1f104d19a7771a0f37954fe8beed943405656eb484f84a99d47e3d514c23146d73ab5f682fd16a48655cb
data/CREDITS.md CHANGED
@@ -12,3 +12,4 @@
12
12
  + https://api.open.fec.gov/
13
13
  + http://plumbing.pipelinedeals.com/wrapping-rest-apis-with-ruby/
14
14
  + http://stackoverflow.com/questions/2680523/dry-ruby-initialization-with-hash-argument
15
+ + http://github.com/chriscondon/
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 wrapper for 18F's [Open FEC (Federal Elections Commission) API](https://api.open.fec.gov/).
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
- Add `gem 'open_fec_api'` to your application's *Gemfile* and run `bundle install`, or install manually with `gem install open_fec_api`.
10
-
11
- ## Configuration
9
+ ```` sh
10
+ gem install open_fec_api
11
+ ````
12
12
 
13
- Configure a client with your [API key](https://api.data.gov/signup/) before making requests.
13
+ ## Prerequisites
14
14
 
15
- ```` rb
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
- response = client.candidates
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 different pages by setting the `:page` request parameter. Avoid rate-limits by increasing the `:per_page` request parameter to 100.
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
- Make requests using endpoint-specific parameters.
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
- ### Installation
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
- Check out the repo with `git clone git@github.com:debate-watch/open-fec-api-ruby.git`, and `cd open-fec-api-ruby`.
61
+ Run `bin/setup` to install dependencies.
56
62
 
57
- After checking out the repo, run `bin/setup` to install dependencies.
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
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
69
+ Run `bin/console` for an interactive prompt to facilitate experiment.
64
70
 
65
- To install this gem onto your local machine, run `bundle exec rake install`.
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
 
@@ -1,6 +1,8 @@
1
1
  require "open_fec_api/version"
2
2
  require "open_fec_api/client"
3
3
  require "open_fec_api/response"
4
+ require "open_fec_api/responses/candidates_response"
5
+ require "open_fec_api/responses/committees_response"
4
6
 
5
7
  module OpenFecApi
6
8
  =begin
@@ -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
- # Candidates Endpoint
18
- #
19
- # https://api.open.fec.gov/developers#!/candidate/get_candidates
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
- # @example
39
- # OpenFecApi::Client.new(:api_key => API_KEY).candidates(:page => 1, :per_page => 100)
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
- response = self.class.get("/candidates", query: query)
48
- return Response.new(response)
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
@@ -2,6 +2,7 @@ module OpenFecApi
2
2
  class Response
3
3
  attr_reader :request, :headers, :api_version, :pagination, :results
4
4
 
5
+ # @param [HTTParty::Response] response
5
6
  def initialize(response)
6
7
  @request = response.request
7
8
  @headers = response.headers
@@ -0,0 +1,4 @@
1
+ module OpenFecApi
2
+ class CandidatesResponse < Response
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module OpenFecApi
2
+ class CommitteesResponse < Response
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenFecApi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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"=>[]}]}
@@ -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 wrapper for 18F's Open FEC (Federal Elections Commission) API - https://api.open.fec.gov/.}
12
- spec.description = %q{A ruby wrapper for 18F's Open FEC (Federal Elections Commission) API - https://api.open.fec.gov/.}
13
- spec.homepage = "https://github.com/debate-watch/open_fec_api/"
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")
@@ -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.2
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-07-25 00:00:00.000000000 Z
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 wrapper for 18F's Open FEC (Federal Elections Commission) API
98
- - https://api.open.fec.gov/.
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/debate-watch/open_fec_api/
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 wrapper for 18F's Open FEC (Federal Elections Commission) API - https://api.open.fec.gov/.
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