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).

See Also:

Constant Summary collapse

API_ENDPOINT =

Returns the default API base URL.

Returns:

  • (String)

    the default API base URL

'https://www.doctolib.fr'
USER_AGENT =

Returns the default User-Agent header.

Returns:

  • (String)

    the default User-Agent header

"TocDoc Ruby Gem #{TocDoc::VERSION}".freeze
MEDIA_TYPE =

Returns the default Accept / Content-Type media type.

Returns:

  • (String)

    the default Accept / Content-Type media type

'application/json'
PER_PAGE =

Returns the default number of results per page.

Returns:

  • (Integer)

    the default number of results per page

15
PAGINATION_DEPTH =

Returns the default number of +next_slot+ hops to follow.

Returns:

  • (Integer)

    the default number of +next_slot+ hops to follow

1
MAX_PER_PAGE =

Returns the hard upper limit for per_page.

Returns:

  • (Integer)

    the hard upper limit for per_page

15
MAX_RETRY =

Returns the default maximum number of retries.

Returns:

  • (Integer)

    the default maximum number of retries

3
CONNECT_TIMEOUT =

Returns the default TCP connect timeout in seconds.

Returns:

  • (Integer)

    the default TCP connect timeout in seconds

5
READ_TIMEOUT =

Returns the default read (response) timeout in seconds.

Returns:

  • (Integer)

    the default read (response) timeout in seconds

10

Class Method Summary collapse

Class Method Details

.api_endpointString

The base API endpoint URL.

Falls back to the TOCDOC_API_ENDPOINT environment variable, then API_ENDPOINT.

Returns:

  • (String)


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

Parameters:

  • logger (Logger, :stdout, nil) (defaults to: nil)

    the logger to inject; +nil+ omits logging

  • rate_limit (Numeric, Hash, nil) (defaults to: nil)

    rate-limit config (see resolve_rate_limit)

  • cache (Symbol, Hash, Object, nil) (defaults to: nil)

    cache config (see resolve_cache)

Returns:

  • (Faraday::RackBuilder)


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_timeoutInteger

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.

Returns:

  • (Integer)


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_optionsHash

Default Faraday connection options (empty by default).

Returns:

  • (Hash)


145
146
147
# File 'lib/toc_doc/core/default.rb', line 145

def connection_options
  @connection_options ||= {}
end

.default_media_typeString

The Accept / Content-Type media type.

Falls back to the TOCDOC_MEDIA_TYPE environment variable, then MEDIA_TYPE.

Returns:

  • (String)


85
86
87
# File 'lib/toc_doc/core/default.rb', line 85

def default_media_type
  ENV.fetch('TOCDOC_MEDIA_TYPE', MEDIA_TYPE)
end

.middlewareFaraday::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.

Returns:

  • (Faraday::RackBuilder)


121
122
123
# File 'lib/toc_doc/core/default.rb', line 121

def middleware
  @middleware ||= build_middleware
end

.optionsHash{Symbol => Object}

Returns a hash of all default configuration values, suitable for passing to Configurable#reset!.

Returns:

  • (Hash{Symbol => Object})


53
54
55
56
57
# File 'lib/toc_doc/core/default.rb', line 53

def options
  { 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_depthInteger

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.

Returns:

  • (Integer)


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_pageInteger

Number of results per page, clamped to MAX_PER_PAGE.

Falls back to the TOCDOC_PER_PAGE environment variable, then PER_PAGE.

Returns:

  • (Integer)


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_timeoutInteger

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.

Returns:

  • (Integer)


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_agentString

The User-Agent header sent with every request.

Falls back to the TOCDOC_USER_AGENT environment variable, then USER_AGENT.

Returns:

  • (String)


75
76
77
# File 'lib/toc_doc/core/default.rb', line 75

def user_agent
  ENV.fetch('TOCDOC_USER_AGENT', USER_AGENT)
end