Class: Onetime::Configuration

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

Overview

Immutable-ish configuration for a Client instance.

Authentication uses HTTP Basic, where the username slot carries the customer external id (extid) — the identifier that begins with "ur" and is shown (with a copy button) at the bottom of the user menu when signed in. The password slot carries your API token.

#validate! checks the extid's format, so a value of the wrong kind fails at construction rather than as an opaque 401.

Values fall back to environment variables:

ONETIME_BASE_URL        -> base_url
ONETIME_CUSTOMER_EXTID  -> customer
ONETIME_API_TOKEN       -> api_token

Constant Summary collapse

DEFAULT_API_VERSION =
:v2
SUPPORTED_VERSIONS =
%i[v1 v2].freeze
DEFAULT_TIMEOUT =

read timeout, seconds

30
DEFAULT_OPEN_TIMEOUT =

connect timeout, seconds

10
DEFAULT_MAX_RETRIES =

retries for idempotent requests

2
CUSTOMER_EXTID_PATTERN =

A customer extid is a short, opaque, case-insensitive identifier that always begins with "ur" — e.g. "ur1abc23def".

/\Aur[a-z0-9]+\z/i
CUSTOMER_EXTID_HINT =

Included in the rejection message: an invalid-format error is only actionable if it says where the valid value lives.

'Your customer extid is the "ur…" identifier at the bottom of the ' \
"user menu when you are signed in (there is a copy button next to it). " \
"Pass it as customer: or set ONETIME_CUSTOMER_EXTID."
APEX_HOSTS =

The apex domain and its www host serve the company website, not the API. Regional deployments each have their own host.

%w[onetimesecret.com www.onetimesecret.com].freeze
EXAMPLE_REGIONAL_HOSTS =

Known regional API hosts (per the API's published server list), surfaced in error messages. Self-hosted and custom domains are also valid base URLs.

%w[
  us.onetimesecret.com eu.onetimesecret.com uk.onetimesecret.com
  ca.onetimesecret.com nz.onetimesecret.com
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, api_version: nil, customer: nil, api_token: nil, timeout: nil, open_timeout: nil, max_retries: nil, user_agent: nil, logger: nil, transport: nil, default_headers: nil) ⇒ Configuration

Returns a new instance of Configuration.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/onetime/configuration.rb', line 54

def initialize(base_url: nil, api_version: nil, customer: nil,
               api_token: nil, timeout: nil, open_timeout: nil, max_retries: nil,
               user_agent: nil, logger: nil, transport: nil, default_headers: nil)
  @base_url        = base_url || ENV["ONETIME_BASE_URL"]
  @api_version     = normalize_version(api_version || DEFAULT_API_VERSION)
  @customer    = customer || ENV["ONETIME_CUSTOMER_EXTID"]
  @api_token       = api_token || ENV["ONETIME_API_TOKEN"]
  @timeout         = timeout || DEFAULT_TIMEOUT
  @open_timeout    = open_timeout || DEFAULT_OPEN_TIMEOUT
  @max_retries     = max_retries.nil? ? DEFAULT_MAX_RETRIES : max_retries
  @user_agent      = user_agent
  @logger          = logger
  @transport       = transport
  @default_headers = default_headers || {}
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def api_token
  @api_token
end

#api_versionObject

Returns the value of attribute api_version.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def api_version
  @api_version
end

#base_urlObject

Returns the value of attribute base_url.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def base_url
  @base_url
end

#customerObject

Returns the value of attribute customer.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def customer
  @customer
end

#default_headersObject

Returns the value of attribute default_headers.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def default_headers
  @default_headers
end

#loggerObject

Returns the value of attribute logger.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def logger
  @logger
end

#max_retriesObject

Returns the value of attribute max_retries.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def max_retries
  @max_retries
end

#open_timeoutObject

Returns the value of attribute open_timeout.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def open_timeout
  @open_timeout
end

#timeoutObject

Returns the value of attribute timeout.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def timeout
  @timeout
end

#transportObject

Returns the value of attribute transport.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def transport
  @transport
end

#user_agentObject

Returns the value of attribute user_agent.



50
51
52
# File 'lib/onetime/configuration.rb', line 50

def user_agent
  @user_agent
end

Instance Method Details

#anonymous?Boolean

True when no credentials are configured. Anonymous clients can still use public and /guest/* endpoints.

Returns:

  • (Boolean)


72
73
74
# File 'lib/onetime/configuration.rb', line 72

def anonymous?
  customer.to_s.empty? && api_token.to_s.empty?
end

#api_path_prefixObject

The mount prefix for the configured API version, e.g. "/api/v2".



77
78
79
# File 'lib/onetime/configuration.rb', line 77

def api_path_prefix
  "/api/#{api_version}"
end

#validate!Object



81
82
83
84
85
86
# File 'lib/onetime/configuration.rb', line 81

def validate!
  validate_api_version!
  validate_base_url!
  validate_credentials!
  self
end