Module: TocDoc::Default
- Defined in:
- lib/toc_doc/core/default.rb
Overview
Provides sensible default values for every configurable option.
Each value can be overridden per-environment via the corresponding TOCDOC_*
environment variable (see individual methods).
Constant Summary collapse
- API_ENDPOINT =
Returns the default API base URL.
'https://www.doctolib.fr'- USER_AGENT =
Returns the default User-Agent header.
"TocDoc Ruby Gem #{TocDoc::VERSION}".freeze
- MEDIA_TYPE =
Returns the default Accept / Content-Type media type.
'application/json'- PER_PAGE =
Returns the default number of results per page.
15- PAGINATION_DEPTH =
Returns the default number of +next_slot+ hops to follow.
1- MAX_PER_PAGE =
Returns the hard upper limit for per_page.
15- MAX_RETRY =
Returns the default maximum number of retries.
3- CONNECT_TIMEOUT =
Returns the default TCP connect timeout in seconds.
5- READ_TIMEOUT =
Returns the default read (response) timeout in seconds.
10
Class Method Summary collapse
-
.api_endpoint ⇒ String
The base API endpoint URL.
-
.build_middleware(logger: nil, rate_limit: nil, cache: nil) ⇒ Faraday::RackBuilder
Builds a Faraday middleware stack, optionally injecting a logger, rate limiter, and response cache.
-
.connect_timeout ⇒ Integer
The TCP connect timeout in seconds.
-
.connection_options ⇒ Hash
Default Faraday connection options (empty by default).
-
.default_media_type ⇒ String
The Accept / Content-Type media type.
-
.middleware ⇒ Faraday::RackBuilder
The default (memoized) Faraday middleware stack, built without a logger.
-
.options ⇒ Hash{Symbol => Object}
Returns a hash of all default configuration values, suitable for passing to Configurable#reset!.
-
.pagination_depth ⇒ Integer
Number of +next_slot+ hops to follow automatically.
-
.per_page ⇒ Integer
Number of results per page, clamped to MAX_PER_PAGE.
-
.read_timeout ⇒ Integer
The read (response) timeout in seconds.
-
.reset!
Clears all memoized values so the next call to Default.middleware and Default.connection_options rebuilds them from scratch.
-
.user_agent ⇒ String
The User-Agent header sent with every request.
Class Method Details
.api_endpoint ⇒ String
The base API endpoint URL.
Falls back to the TOCDOC_API_ENDPOINT environment variable, then
API_ENDPOINT.
65 66 67 |
# File 'lib/toc_doc/core/default.rb', line 65 def api_endpoint ENV.fetch('TOCDOC_API_ENDPOINT', API_ENDPOINT) end |
.build_middleware(logger: nil, rate_limit: nil, cache: nil) ⇒ Faraday::RackBuilder
Builds a Faraday middleware stack, optionally injecting a logger, rate limiter, and response cache.
Stack order (outermost first): RaiseError > [Cache] > [Logging] > Retry > [RateLimiter] > JSON > Adapter
135 136 137 138 139 140 |
# File 'lib/toc_doc/core/default.rb', line 135 def build_middleware(logger: nil, rate_limit: nil, cache: nil) logger_obj = resolve_logger(logger) bucket = resolve_rate_limit(rate_limit) cache_config = resolve_cache(cache) Faraday::RackBuilder.new { |b| apply_middleware(b, logger_obj, bucket, cache_config) } end |
.connect_timeout ⇒ Integer
The TCP connect timeout in seconds.
Falls back to the TOCDOC_CONNECT_TIMEOUT environment variable, then
CONNECT_TIMEOUT. Invalid ENV values fall back to CONNECT_TIMEOUT.
155 156 157 158 159 |
# File 'lib/toc_doc/core/default.rb', line 155 def connect_timeout Integer(ENV.fetch('TOCDOC_CONNECT_TIMEOUT', CONNECT_TIMEOUT), 10) rescue ArgumentError CONNECT_TIMEOUT end |
.connection_options ⇒ Hash
Default Faraday connection options (empty by default).
145 146 147 |
# File 'lib/toc_doc/core/default.rb', line 145 def @connection_options ||= {} end |
.default_media_type ⇒ String
The Accept / Content-Type media type.
Falls back to the TOCDOC_MEDIA_TYPE environment variable, then
MEDIA_TYPE.
85 86 87 |
# File 'lib/toc_doc/core/default.rb', line 85 def default_media_type ENV.fetch('TOCDOC_MEDIA_TYPE', MEDIA_TYPE) end |
.middleware ⇒ Faraday::RackBuilder
The default (memoized) Faraday middleware stack, built without a logger.
Stack order (outermost first): RaiseError, retry, JSON parsing, adapter. RaiseError is outermost so it wraps retry and maps the final response or re-raised transport exception into a typed Error.
121 122 123 |
# File 'lib/toc_doc/core/default.rb', line 121 def middleware @middleware ||= build_middleware end |
.options ⇒ Hash{Symbol => Object}
Returns a hash of all default configuration values, suitable for passing to Configurable#reset!.
53 54 55 56 57 |
# File 'lib/toc_doc/core/default.rb', line 53 def { api_endpoint:, user_agent:, default_media_type:, per_page:, middleware:, connection_options:, connect_timeout:, read_timeout:, logger: nil, pagination_depth:, rate_limit: nil, cache: nil } end |
.pagination_depth ⇒ Integer
Number of +next_slot+ hops to follow automatically.
Falls back to the TOCDOC_PAGINATION_DEPTH environment variable, then
PAGINATION_DEPTH. Negative ENV values are clamped to 0.
107 108 109 110 111 112 |
# File 'lib/toc_doc/core/default.rb', line 107 def pagination_depth depth = Integer(ENV.fetch('TOCDOC_PAGINATION_DEPTH', PAGINATION_DEPTH), 10) [depth, 0].max rescue ArgumentError PAGINATION_DEPTH end |
.per_page ⇒ Integer
Number of results per page, clamped to MAX_PER_PAGE.
Falls back to the TOCDOC_PER_PAGE environment variable, then
PER_PAGE.
95 96 97 98 99 |
# File 'lib/toc_doc/core/default.rb', line 95 def per_page [Integer(ENV.fetch('TOCDOC_PER_PAGE', PER_PAGE), 10), MAX_PER_PAGE].min rescue ArgumentError PER_PAGE end |
.read_timeout ⇒ Integer
The read (response) timeout in seconds.
Falls back to the TOCDOC_READ_TIMEOUT environment variable, then
READ_TIMEOUT. Invalid ENV values fall back to READ_TIMEOUT.
167 168 169 170 171 |
# File 'lib/toc_doc/core/default.rb', line 167 def read_timeout Integer(ENV.fetch('TOCDOC_READ_TIMEOUT', READ_TIMEOUT), 10) rescue ArgumentError READ_TIMEOUT end |
.reset!
This method returns an undefined value.
Clears all memoized values so the next call to middleware and connection_options rebuilds them from scratch.
Called by Configurable#reset! to ensure each reset produces a fresh middleware stack rather than reusing a stale memoized instance.
180 181 182 183 |
# File 'lib/toc_doc/core/default.rb', line 180 def reset! @middleware = nil @connection_options = nil end |
.user_agent ⇒ String
The User-Agent header sent with every request.
Falls back to the TOCDOC_USER_AGENT environment variable, then
USER_AGENT.
75 76 77 |
# File 'lib/toc_doc/core/default.rb', line 75 def user_agent ENV.fetch('TOCDOC_USER_AGENT', USER_AGENT) end |