class OpenFecApi::Client

Attributes

api_key[R]

Public Class Methods

new(api_key) click to toggle source
# File lib/open_fec_api/client.rb, line 11
def initialize(api_key)
  @api_key = api_key
end

Public Instance Methods

candidate(candidate_id, options = {}) click to toggle source

Candidate Endpoint

api.open.fec.gov/developers/#!/candidate/get_candidate_candidate_id

@param [String] candidate_id

@example

OpenFecApi::Client.new(:api_key => API_KEY).candidate("P00003392", {:page => 1, :per_page => 100})
# File lib/open_fec_api/client.rb, line 104
def candidate(candidate_id, options = {})
  get_response("/candidate/#{candidate_id}", options)
end
candidates(options = {}) click to toggle source

Candidates Endpoint

api.open.fec.gov/developers#!/candidate/get_candidates

@example

OpenFecApi::Client.new(:api_key => API_KEY).candidates({:page => 1, :per_page => 100})
# File lib/open_fec_api/client.rb, line 92
def candidates(options = {})
  get_response("/candidates", options)
end
committee(committee_id, options = {}) click to toggle source

Committee Endpoint

api.open.fec.gov/developers#!/committee/get_committees

@param [String] committee_id

@example

OpenFecApi::Client.new(:api_key => API_KEY).committee("C00571372", {:page => 1, :per_page => 100})
# File lib/open_fec_api/client.rb, line 151
def committee(committee_id, options = {})
  get_response("/committee/#{committee_id}", options)
end
committees(options = {}) click to toggle source

Committees Endpoint

api.open.fec.gov/developers#!/committee/get_committees

@example

OpenFecApi::Client.new(:api_key => API_KEY).committees({:page => 1, :per_page => 100})
# File lib/open_fec_api/client.rb, line 128
def committees(options = {})
  get_response("/committees", options)
end
configured?() click to toggle source
# File lib/open_fec_api/client.rb, line 15
def configured?
  !self.api_key.nil?
end
get_response(endpoint, options = {}) click to toggle source

@param [String] endpoint One of: [“candidates/”,“committees/”] @param [Hash] options

option options Array[string] :sort Provide a field to sort by. Use - for descending order.
option options Boolean :sort_hide_null Hide null values on sorted column(s).
option options String :year See records pertaining to a particular year.
option options Array[string] :office Governmental office candidate runs for: House (H), Senate (S) or President (P).
option options Array[string] :candidate_status One letter code explaining if the candidate is: present (C), future (F), not yet (N), or prior (P).
option options Array[string] :party Three letter code for the party under which a candidate ran for office.
option options Array[string] :state U.S. State candidate or territory where a candidate runs for office.
option options Array[integer] :cycle Two-year election cycle in which a candidate runs for office. Calculated from FEC form 2.
option options Array[string] :district Two digit district number.
option options Array[string] :incumbent_challenge One letter code explaining if the candidate is an incumbent (I), a challenger (C), or if the seat is open (O).
option options String :q Text to search all fields for.
option options String :name Candidate's name (full or partial).
option options Array[string] :candidate_id A unique identifier assigned to each candidate registered with the FEC. If a person runs for several offices, that person will have separate candidate IDs for each office.
option options Integer :page For paginating through results, starting at page 1.
option options Integer :per_page The number of results returned per page. Defaults to 20.
# File lib/open_fec_api/client.rb, line 45
def get_response(endpoint, options = {})
  endpoint_name = (endpoint.split("/") - [""]).first
  request_options = options.select{|k,v| request_params.include?(k.to_s)}

  # Parse/compile query params.

  query = {'api_key' => @api_key}
  request_options.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() click to toggle source
# 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