Class: OpenFecApi::Client
- Inherits:
-
Object
- Object
- OpenFecApi::Client
- Includes:
- HTTParty
- Defined in:
- lib/open_fec_api/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#candidate(candidate_id, options = {}) ⇒ Object
Candidate Endpoint.
-
#candidates(options = {}) ⇒ Object
Candidates Endpoint.
-
#committee(committee_id, options = {}) ⇒ Object
Committee Endpoint.
-
#committees(options = {}) ⇒ Object
Committees Endpoint.
- #configured? ⇒ Boolean
- #get_response(endpoint, options = {}) ⇒ Object
-
#initialize(api_key) ⇒ Client
constructor
A new instance of Client.
- #request_params ⇒ Object
Constructor Details
#initialize(api_key) ⇒ Client
Returns a new instance of Client.
11 12 13 |
# File 'lib/open_fec_api/client.rb', line 11 def initialize(api_key) @api_key = api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/open_fec_api/client.rb', line 9 def api_key @api_key end |
Instance Method Details
#candidate(candidate_id, options = {}) ⇒ Object
Candidate Endpoint
api.open.fec.gov/developers/#!/candidate/get_candidate_candidate_id
104 105 106 |
# File 'lib/open_fec_api/client.rb', line 104 def candidate(candidate_id, = {}) get_response("/candidate/#{candidate_id}", ) end |
#candidates(options = {}) ⇒ Object
Candidates Endpoint
92 93 94 |
# File 'lib/open_fec_api/client.rb', line 92 def candidates( = {}) get_response("/candidates", ) end |
#committee(committee_id, options = {}) ⇒ Object
Committee Endpoint
151 152 153 |
# File 'lib/open_fec_api/client.rb', line 151 def committee(committee_id, = {}) get_response("/committee/#{committee_id}", ) end |
#committees(options = {}) ⇒ Object
Committees Endpoint
128 129 130 |
# File 'lib/open_fec_api/client.rb', line 128 def committees( = {}) get_response("/committees", ) end |
#configured? ⇒ Boolean
15 16 17 |
# File 'lib/open_fec_api/client.rb', line 15 def configured? !self.api_key.nil? end |
#get_response(endpoint, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/open_fec_api/client.rb', line 45 def get_response(endpoint, = {}) endpoint_name = (endpoint.split("/") - [""]).first = .select{|k,v| request_params.include?(k.to_s)} # Parse/compile query params. query = {'api_key' => @api_key} .each do |k,v| query.merge!({k.to_s => v}) end # Make a request. response = self.class.get(endpoint, query: query) # Return the proper response. response_class_name = endpoint_name.capitalize.concat("Response") return OpenFecApi.const_get(response_class_name).new(response) # response_class_name.constantize.new(response) end |
#request_params ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/open_fec_api/client.rb', line 19 def request_params [ "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" ] end |