Class: OpenFec::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/open_fec/client.rb

Overview

HTTP client for the FEC API. Handles authentication, retries, and pagination.

Examples:

Direct usage

client = OpenFec.client
response = client.get('candidates/search/', name: 'Pelosi')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = OpenFec.configuration) ⇒ Client

Returns a new instance of Client.

Parameters:

Raises:



18
19
20
21
# File 'lib/open_fec/client.rb', line 18

def initialize(config = OpenFec.configuration)
  @config = config
  config.validate!
end

Instance Attribute Details

#configOpenFec::Configuration (readonly)



14
15
16
# File 'lib/open_fec/client.rb', line 14

def config
  @config
end

Instance Method Details

#all(path, params = {}) ⇒ Array<Hash>

Fetch all results from an offset-paginated endpoint.

Parameters:

  • path (String)

    API endpoint path

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

    query parameters

Returns:

  • (Array<Hash>)

    all result records



92
93
94
95
96
# File 'lib/open_fec/client.rb', line 92

def all(path, params = {})
  records = []
  paginate(path, params) { |response| records.concat(response.results) }
  records
end

#all_seek(path, params = {}) ⇒ Array<Hash>

Fetch all results from a seek-paginated endpoint.

Parameters:

  • path (String)

    API endpoint path

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

    query parameters

Returns:

  • (Array<Hash>)

    all result records



103
104
105
106
107
# File 'lib/open_fec/client.rb', line 103

def all_seek(path, params = {})
  records = []
  paginate_seek(path, params) { |response| records.concat(response.results) }
  records
end

#candidate_totalsOpenFec::Resources::CandidateTotals



117
118
119
# File 'lib/open_fec/client.rb', line 117

def candidate_totals
  @candidate_totals ||= Resources::CandidateTotals.new(self)
end

#candidatesOpenFec::Resources::Candidates



112
113
114
# File 'lib/open_fec/client.rb', line 112

def candidates
  @candidates ||= Resources::Candidates.new(self)
end

#committeesOpenFec::Resources::Committees



122
123
124
# File 'lib/open_fec/client.rb', line 122

def committees
  @committees ||= Resources::Committees.new(self)
end

#connectionFaraday::Connection

Returns:

  • (Faraday::Connection)


24
25
26
# File 'lib/open_fec/client.rb', line 24

def connection
  @connection ||= build_connection
end

#contribution_aggregatesOpenFec::Resources::ContributionAggregates



132
133
134
# File 'lib/open_fec/client.rb', line 132

def contribution_aggregates
  @contribution_aggregates ||= Resources::ContributionAggregates.new(self)
end

#contributionsOpenFec::Resources::Contributions



127
128
129
# File 'lib/open_fec/client.rb', line 127

def contributions
  @contributions ||= Resources::Contributions.new(self)
end

#disbursementsOpenFec::Resources::Disbursements



137
138
139
# File 'lib/open_fec/client.rb', line 137

def disbursements
  @disbursements ||= Resources::Disbursements.new(self)
end

#electionsOpenFec::Resources::Elections



142
143
144
# File 'lib/open_fec/client.rb', line 142

def elections
  @elections ||= Resources::Elections.new(self)
end

#get(path, params = {}) ⇒ OpenFec::Response

Make a GET request to the FEC API.

Parameters:

  • path (String)

    API endpoint path (e.g. “candidates/search/”)

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

    query parameters

Returns:

Raises:



34
35
36
37
38
39
40
41
# File 'lib/open_fec/client.rb', line 34

def get(path, params = {})
  response = connection.get(path, with_api_key(compact_params(params)))
  handle_response(response)
rescue Faraday::TimeoutError => e
  raise ConnectionError, "Request timed out: #{e.message}"
rescue Faraday::ConnectionFailed => e
  raise ConnectionError, "Connection failed: #{e.message}"
end

#independent_expendituresOpenFec::Resources::IndependentExpenditures



147
148
149
# File 'lib/open_fec/client.rb', line 147

def independent_expenditures
  @independent_expenditures ||= Resources::IndependentExpenditures.new(self)
end

#inspectString

Returns:

  • (String)


152
153
154
# File 'lib/open_fec/client.rb', line 152

def inspect
  "#<#{self.class} base_url=#{config.base_url.inspect} timeout=#{config.timeout}>"
end

#paginate(path, params = {}) {|OpenFec::Response| ... } ⇒ Object

Iterate through offset-paginated results, yielding each page. Used by most FEC API endpoints.

Parameters:

  • path (String)

    API endpoint path

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

    query parameters

Yields:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/open_fec/client.rb', line 49

def paginate(path, params = {})
  page = 1
  iterations = 0
  loop do
    iterations += 1
    response = get(path, params.merge(page: page, per_page: config.per_page))
    yield response
    break if response.empty?
    break unless response.next_page?

    page = response.next_page_number
    break if page.nil?
    break if iterations >= config.max_pages
  end
end

#paginate_seek(path, params = {}) {|OpenFec::Response| ... } ⇒ Object

Iterate through seek/cursor-paginated results, yielding each page. Used by itemized transaction endpoints (Schedule A, Schedule B).

Parameters:

  • path (String)

    API endpoint path

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

    query parameters

Yields:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/open_fec/client.rb', line 71

def paginate_seek(path, params = {})
  iterations = 0
  cursor_params = {}
  loop do
    iterations += 1
    response = get(path, params.merge(per_page: config.per_page, **cursor_params))
    yield response
    break if response.empty?
    break unless response.next_page?

    cursor_params = response.next_page_params || {}
    break if cursor_params.empty?
    break if iterations >= config.max_pages
  end
end