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



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

#candidates(options = {}) ⇒ Object

Examples:

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


72
73
74
# File 'lib/open_fec_api/client.rb', line 72

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

#committees(options = {}) ⇒ Object

Examples:

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


82
83
84
# File 'lib/open_fec_api/client.rb', line 82

def committees(options = {})
  get_response("/committees", options)
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, options = {})
  endpoint_name = endpoint.gsub("/","")
  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