Class: OpenFec::Configuration
- Inherits:
-
Object
- Object
- OpenFec::Configuration
- Defined in:
- lib/open_fec/configuration.rb
Overview
Configuration for the OpenFEC API client.
Constant Summary collapse
- DEFAULT_BASE_URL =
'https://api.open.fec.gov/v1/'- DEFAULT_TIMEOUT =
30- DEFAULT_RETRIES =
3- DEFAULT_PER_PAGE =
20- DEFAULT_MAX_PAGES =
100
Instance Attribute Summary collapse
-
#adapter ⇒ Symbol
Faraday adapter (default: system default).
-
#api_key ⇒ String?
FEC API key (get one free at api.data.gov/signup).
-
#base_url ⇒ String
Base URL for the FEC API.
-
#logger ⇒ Logger?
Optional logger for HTTP requests.
-
#max_pages ⇒ Integer
Max pages to fetch during pagination.
-
#per_page ⇒ Integer
Default results per page.
-
#retries ⇒ Integer
Number of retries on 429/5xx.
-
#timeout ⇒ Integer
HTTP timeout in seconds.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #inspect ⇒ Object
-
#to_h ⇒ Hash
All configuration values.
-
#validate! ⇒ void
Validates that required configuration is present.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/open_fec/configuration.rb', line 36 def initialize @base_url = DEFAULT_BASE_URL @timeout = DEFAULT_TIMEOUT @retries = DEFAULT_RETRIES @per_page = DEFAULT_PER_PAGE @max_pages = DEFAULT_MAX_PAGES @logger = nil @adapter = Faraday.default_adapter @api_key = ENV.fetch('OPEN_FEC_API_KEY', nil) end |
Instance Attribute Details
#adapter ⇒ Symbol
Returns Faraday adapter (default: system default).
28 29 30 |
# File 'lib/open_fec/configuration.rb', line 28 def adapter @adapter end |
#api_key ⇒ String?
Returns FEC API key (get one free at api.data.gov/signup).
30 31 32 |
# File 'lib/open_fec/configuration.rb', line 30 def api_key @api_key end |
#base_url ⇒ String
Returns base URL for the FEC API.
20 21 22 |
# File 'lib/open_fec/configuration.rb', line 20 def base_url @base_url end |
#logger ⇒ Logger?
Returns optional logger for HTTP requests.
26 27 28 |
# File 'lib/open_fec/configuration.rb', line 26 def logger @logger end |
#max_pages ⇒ Integer
Returns max pages to fetch during pagination.
34 35 36 |
# File 'lib/open_fec/configuration.rb', line 34 def max_pages @max_pages end |
#per_page ⇒ Integer
Returns default results per page.
32 33 34 |
# File 'lib/open_fec/configuration.rb', line 32 def per_page @per_page end |
#retries ⇒ Integer
Returns number of retries on 429/5xx.
24 25 26 |
# File 'lib/open_fec/configuration.rb', line 24 def retries @retries end |
#timeout ⇒ Integer
Returns HTTP timeout in seconds.
22 23 24 |
# File 'lib/open_fec/configuration.rb', line 22 def timeout @timeout end |
Instance Method Details
#inspect ⇒ Object
72 73 74 |
# File 'lib/open_fec/configuration.rb', line 72 def inspect "#<#{self.class} base_url=#{base_url.inspect} timeout=#{timeout} retries=#{retries}>" end |
#to_h ⇒ Hash
Returns all configuration values.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/open_fec/configuration.rb', line 59 def to_h { base_url: base_url, timeout: timeout, retries: retries, per_page: per_page, max_pages: max_pages, logger: logger, adapter: adapter, api_key: api_key } end |
#validate! ⇒ void
This method returns an undefined value.
Validates that required configuration is present.
51 52 53 54 55 56 |
# File 'lib/open_fec/configuration.rb', line 51 def validate! return unless api_key.nil? || api_key.empty? raise ConfigurationError, 'api_key is required (get one free at https://api.data.gov/signup)' end |