Class: OpenFecApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/open_fec_api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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

Examples:

OpenFecApi::Client.new(:api_key => API_KEY).candidate("P00003392", {:page => 1, :per_page => 100})

Parameters:

  • candidate_id (String)


104
105
106
# File 'lib/open_fec_api/client.rb', line 104

def candidate(candidate_id, options = {})
  get_response("/candidate/#{candidate_id}", options)
end

#candidates(options = {}) ⇒ Object

Examples:

OpenFecApi::Client.new(:api_key => API_KEY).candidates({:page => 1, :per_page => 100})


92
93
94
# File 'lib/open_fec_api/client.rb', line 92

def candidates(options = {})
  get_response("/candidates", options)
end

#committee(committee_id, options = {}) ⇒ Object

Examples:

OpenFecApi::Client.new(:api_key => API_KEY).committee("C00571372", {:page => 1, :per_page => 100})

Parameters:

  • committee_id (String)


151
152
153
# File 'lib/open_fec_api/client.rb', line 151

def committee(committee_id, options = {})
  get_response("/committee/#{committee_id}", options)
end

#committees(options = {}) ⇒ Object

Examples:

OpenFecApi::Client.new(:api_key => API_KEY).committees({:page => 1, :per_page => 100})


128
129
130
# File 'lib/open_fec_api/client.rb', line 128

def committees(options = {})
  get_response("/committees", options)
end

#configured?Boolean

Returns:

  • (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

Parameters:

  • endpoint (String)

    One of: [“candidates/”,“committees/”]

  • options (Hash) (defaults to: {})

    option options Array :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 :office Governmental office candidate runs for: House (H), Senate (S) or President (P). option options Array :candidate_status One letter code explaining if the candidate is: present ©, future (F), not yet (N), or prior (P). option options Array :party Three letter code for the party under which a candidate ran for office. option options Array :state U.S. State candidate or territory where a candidate runs for office. option options Array :cycle Two-year election cycle in which a candidate runs for office. Calculated from FEC form 2. option options Array :district Two digit district number. option options Array :incumbent_challenge One letter code explaining if the candidate is an incumbent (I), a challenger ©, 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 :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.



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, 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_paramsObject



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