Class: Onetime::Configuration
- Inherits:
-
Object
- Object
- Onetime::Configuration
- 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
-
#api_token ⇒ Object
Returns the value of attribute api_token.
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#customer ⇒ Object
Returns the value of attribute customer.
-
#default_headers ⇒ Object
Returns the value of attribute default_headers.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#max_retries ⇒ Object
Returns the value of attribute max_retries.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#transport ⇒ Object
Returns the value of attribute transport.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Instance Method Summary collapse
-
#anonymous? ⇒ Boolean
True when no credentials are configured.
-
#api_path_prefix ⇒ Object
The mount prefix for the configured API version, e.g.
-
#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
constructor
A new instance of Configuration.
- #validate! ⇒ Object
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_token ⇒ Object
Returns the value of attribute api_token.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def api_token @api_token end |
#api_version ⇒ Object
Returns the value of attribute api_version.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def api_version @api_version end |
#base_url ⇒ Object
Returns the value of attribute base_url.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def base_url @base_url end |
#customer ⇒ Object
Returns the value of attribute customer.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def customer @customer end |
#default_headers ⇒ Object
Returns the value of attribute default_headers.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def default_headers @default_headers end |
#logger ⇒ Object
Returns the value of attribute logger.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def logger @logger end |
#max_retries ⇒ Object
Returns the value of attribute max_retries.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def max_retries @max_retries end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def open_timeout @open_timeout end |
#timeout ⇒ Object
Returns the value of attribute timeout.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def timeout @timeout end |
#transport ⇒ Object
Returns the value of attribute transport.
50 51 52 |
# File 'lib/onetime/configuration.rb', line 50 def transport @transport end |
#user_agent ⇒ Object
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.
72 73 74 |
# File 'lib/onetime/configuration.rb', line 72 def anonymous? customer.to_s.empty? && api_token.to_s.empty? end |
#api_path_prefix ⇒ Object
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 |