Class: Invoance::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/invoance/config.rb

Overview

Resolved, immutable SDK configuration.

All fields are optional at the client boundary — when omitted the SDK reads from environment variables:

* INVOANCE_API_KEY  — API key (required)
* INVOANCE_BASE_URL — API host (defaults to https://api.invoance.com)

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.invoance.com"
DEFAULT_TIMEOUT =
30
ENV_API_KEY =
"INVOANCE_API_KEY"
ENV_BASE_URL =
"INVOANCE_BASE_URL"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, api_version: "v1", timeout: DEFAULT_TIMEOUT, idempotency_key: nil, extra_headers: {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • api_key (String, nil) (defaults to: nil)
  • base_url (String, nil) (defaults to: nil)
  • api_version (String) (defaults to: "v1")
  • timeout (Numeric) (defaults to: DEFAULT_TIMEOUT)

    request timeout in seconds

  • idempotency_key (String, nil) (defaults to: nil)

    default idempotency key for mutating requests

  • extra_headers (Hash) (defaults to: {})

    merged into every request



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/invoance/config.rb', line 25

def initialize(api_key: nil, base_url: nil, api_version: "v1", timeout: DEFAULT_TIMEOUT,
               idempotency_key: nil, extra_headers: {})
  resolved_key = api_key || ENV[ENV_API_KEY] || ""
  if resolved_key.empty?
    raise ArgumentError,
          "api_key is required. Pass it explicitly or set the #{ENV_API_KEY} environment variable."
  end

  @api_key = resolved_key
  @base_url = (base_url || ENV[ENV_BASE_URL] || DEFAULT_BASE_URL).sub(%r{/+\z}, "")
  @api_version = (api_version || "v1").gsub(%r{\A/+|/+\z}, "")
  @timeout = timeout || DEFAULT_TIMEOUT
  @idempotency_key = idempotency_key
  @extra_headers = extra_headers || {}
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



17
18
19
# File 'lib/invoance/config.rb', line 17

def api_key
  @api_key
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



17
18
19
# File 'lib/invoance/config.rb', line 17

def api_version
  @api_version
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/invoance/config.rb', line 17

def base_url
  @base_url
end

#extra_headersObject (readonly)

Returns the value of attribute extra_headers.



17
18
19
# File 'lib/invoance/config.rb', line 17

def extra_headers
  @extra_headers
end

#idempotency_keyObject (readonly)

Returns the value of attribute idempotency_key.



17
18
19
# File 'lib/invoance/config.rb', line 17

def idempotency_key
  @idempotency_key
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/invoance/config.rb', line 17

def timeout
  @timeout
end