Class: OpenFec::Client
- Inherits:
-
Object
- Object
- OpenFec::Client
- Defined in:
- lib/open_fec/client.rb
Overview
HTTP client for the FEC API. Handles authentication, retries, and pagination.
Instance Attribute Summary collapse
- #config ⇒ OpenFec::Configuration readonly
Instance Method Summary collapse
-
#all(path, params = {}) ⇒ Array<Hash>
Fetch all results from an offset-paginated endpoint.
-
#all_seek(path, params = {}) ⇒ Array<Hash>
Fetch all results from a seek-paginated endpoint.
- #candidate_totals ⇒ OpenFec::Resources::CandidateTotals
- #candidates ⇒ OpenFec::Resources::Candidates
- #committees ⇒ OpenFec::Resources::Committees
- #connection ⇒ Faraday::Connection
- #contribution_aggregates ⇒ OpenFec::Resources::ContributionAggregates
- #contributions ⇒ OpenFec::Resources::Contributions
- #disbursements ⇒ OpenFec::Resources::Disbursements
- #elections ⇒ OpenFec::Resources::Elections
-
#get(path, params = {}) ⇒ OpenFec::Response
Make a GET request to the FEC API.
- #independent_expenditures ⇒ OpenFec::Resources::IndependentExpenditures
-
#initialize(config = OpenFec.configuration) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ String
-
#paginate(path, params = {}) {|OpenFec::Response| ... } ⇒ Object
Iterate through offset-paginated results, yielding each page.
-
#paginate_seek(path, params = {}) {|OpenFec::Response| ... } ⇒ Object
Iterate through seek/cursor-paginated results, yielding each page.
Constructor Details
#initialize(config = OpenFec.configuration) ⇒ Client
Returns a new instance of Client.
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
#config ⇒ OpenFec::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.
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.
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_totals ⇒ OpenFec::Resources::CandidateTotals
117 118 119 |
# File 'lib/open_fec/client.rb', line 117 def candidate_totals @candidate_totals ||= Resources::CandidateTotals.new(self) end |
#candidates ⇒ OpenFec::Resources::Candidates
112 113 114 |
# File 'lib/open_fec/client.rb', line 112 def candidates @candidates ||= Resources::Candidates.new(self) end |
#committees ⇒ OpenFec::Resources::Committees
122 123 124 |
# File 'lib/open_fec/client.rb', line 122 def committees @committees ||= Resources::Committees.new(self) end |
#connection ⇒ Faraday::Connection
24 25 26 |
# File 'lib/open_fec/client.rb', line 24 def connection @connection ||= build_connection end |
#contribution_aggregates ⇒ OpenFec::Resources::ContributionAggregates
132 133 134 |
# File 'lib/open_fec/client.rb', line 132 def contribution_aggregates @contribution_aggregates ||= Resources::ContributionAggregates.new(self) end |
#contributions ⇒ OpenFec::Resources::Contributions
127 128 129 |
# File 'lib/open_fec/client.rb', line 127 def contributions @contributions ||= Resources::Contributions.new(self) end |
#disbursements ⇒ OpenFec::Resources::Disbursements
137 138 139 |
# File 'lib/open_fec/client.rb', line 137 def disbursements @disbursements ||= Resources::Disbursements.new(self) end |
#elections ⇒ OpenFec::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.
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.}" rescue Faraday::ConnectionFailed => e raise ConnectionError, "Connection failed: #{e.}" end |
#independent_expenditures ⇒ OpenFec::Resources::IndependentExpenditures
147 148 149 |
# File 'lib/open_fec/client.rb', line 147 def independent_expenditures @independent_expenditures ||= Resources::IndependentExpenditures.new(self) end |
#inspect ⇒ 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.
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).
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 |