Class: Ksef::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/ksef/configuration.rb

Overview

Immutable client configuration.

Frozen at construction so a single Client can be shared across threads (DESIGN.md §5.2); all mutable per-operation state lives in session and flow objects.

#inspect is redacted: credentials must never reach a log or an exception backtrace (DESIGN.md §4.5).

Constant Summary collapse

DEFAULT_TIMEOUT =
{ open: 10, read: 60 }.freeze
DEFAULT_ADAPTER =
:net_http

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: :test, auth: nil, logger: nil, timeout: DEFAULT_TIMEOUT, adapter: DEFAULT_ADAPTER, **options) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • env (Symbol, Ksef::Environments::Environment) (defaults to: :test)

    :test, :demo, :prod, or the result of Environments.custom

  • auth (Object, nil) (defaults to: nil)

    a credential object, e.g. Auth::Token

  • logger (Object, nil) (defaults to: nil)

    any object responding to #debug/#info/#warn. Deliberately duck-typed — logger is a bundled gem as of Ruby 4.0 and this gem does not require it (DESIGN.md §4.3).

  • timeout (Hash) (defaults to: DEFAULT_TIMEOUT)

    {open:, read:} in seconds

  • adapter (Symbol) (defaults to: DEFAULT_ADAPTER)

    Faraday adapter; kept swappable

  • options (Hash)

    accepts retry: (DESIGN.md §6.1) or retry_policy:, plus proxy: and user_agent:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ksef/configuration.rb', line 27

def initialize(env: :test, auth: nil, logger: nil, timeout: DEFAULT_TIMEOUT, adapter: DEFAULT_ADAPTER, **options)
  apply_options(options)

  @environment = Environments.fetch(env)
  @auth = auth
  @adapter = adapter
  @logger = validate_logger(logger)
  @timeout = normalize_timeout(timeout)

  validate_retry_policy!
  freeze
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def adapter
  @adapter
end

#authObject (readonly)

Returns the value of attribute auth.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def auth
  @auth
end

#environmentObject (readonly)

Returns the value of attribute environment.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def environment
  @environment
end

#loggerObject (readonly)

Returns the value of attribute logger.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def logger
  @logger
end

#proxyObject (readonly)

Returns the value of attribute proxy.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def proxy
  @proxy
end

#retry_policyObject (readonly)

Returns the value of attribute retry_policy.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def retry_policy
  @retry_policy
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def timeout
  @timeout
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



15
16
17
# File 'lib/ksef/configuration.rb', line 15

def user_agent
  @user_agent
end

Instance Method Details

#base_urlString

Returns the environment base URL, already including /v2.

Returns:

  • (String)

    the environment base URL, already including /v2



41
# File 'lib/ksef/configuration.rb', line 41

def base_url = environment.base_url

#inspectObject Also known as: to_s

Redacted — never expose auth (DESIGN.md §4.5).



52
53
54
55
# File 'lib/ksef/configuration.rb', line 52

def inspect
  "#<#{self.class.name} env=#{environment.name.inspect} base_url=#{base_url.inspect} " \
    "auth=#{auth ? "[REDACTED]" : "nil"} adapter=#{adapter.inspect} timeout=#{timeout.inspect}>"
end

#open_timeoutInteger

Returns seconds.

Returns:

  • (Integer)

    seconds



46
# File 'lib/ksef/configuration.rb', line 46

def open_timeout = timeout.fetch(:open)

#production?Boolean

Returns:

  • (Boolean)


43
# File 'lib/ksef/configuration.rb', line 43

def production? = environment.production?

#read_timeoutInteger

Returns seconds.

Returns:

  • (Integer)

    seconds



49
# File 'lib/ksef/configuration.rb', line 49

def read_timeout = timeout.fetch(:read)