Module: RunApi::Core::Auth

Defined in:
lib/runapi/core/auth.rb

Overview

API key resolution helpers.

Constant Summary collapse

ENV_VAR_NAME =
"RUNAPI_API_KEY"
MISSING_KEY_MESSAGE =
"API key is required. Pass api_key or set the RUNAPI_API_KEY environment variable."

Class Method Summary collapse

Class Method Details

.resolve_api_key(explicit = nil) ⇒ String

Resolve the API key from (in priority order):

1. the explicit argument
2. RunApi.api_key (global configuration)
3. the RUNAPI_API_KEY environment variable

All sources are trimmed; blank values are treated as missing. Raises RunApi::Core::AuthenticationError when no source yields a value.

Parameters:

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

    API key passed directly to a client constructor.

Returns:

  • (String)

    the resolved API key

Raises:



21
22
23
24
25
26
27
28
29
# File 'lib/runapi/core/auth.rb', line 21

def self.resolve_api_key(explicit = nil)
  resolved = normalize(explicit) ||
    normalize(RunApi.respond_to?(:api_key) ? RunApi.api_key : nil) ||
    normalize(ENV[ENV_VAR_NAME])

  raise AuthenticationError, MISSING_KEY_MESSAGE unless resolved

  resolved
end