Class: Assinafy::Configuration
- Inherits:
-
Object
- Object
- Assinafy::Configuration
- Defined in:
- lib/assinafy/configuration.rb,
sig/assinafy.rbs
Overview
SDK configuration values. Client snapshots these values when it builds its connection; construct a new client after changing a configuration.
Constant Summary collapse
- DEFAULT_BASE_URL =
Default base URL (production v1 API).
'https://api.assinafy.com.br/v1'- DEFAULT_TIMEOUT =
Default Faraday open/read timeout, in seconds.
30
Instance Attribute Summary collapse
-
#account_id ⇒ String?
Default workspace ID.
-
#api_key ⇒ String?
Sent as
X-Api-Key. -
#base_url ⇒ String
API base URL (trailing slash stripped).
- #logger ⇒ Logger?
-
#timeout ⇒ Integer
Faraday timeout in seconds.
-
#token ⇒ String?
Legacy bearer token (used when
api_keyis nil). -
#webhook_secret ⇒ String?
Secret for Support::WebhookVerifier.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Configuration
Build a Configuration from a Hash with string or symbol keys.
Instance Method Summary collapse
-
#auth_headers ⇒ Hash{String=>String}
Return the HTTP headers used to authenticate requests, preferring
X-Api-Key(the documented mechanism) over a bearer token. -
#initialize(api_key: nil, token: nil, account_id: nil, base_url: DEFAULT_BASE_URL, webhook_secret: nil, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ Configuration
constructor
Build a configuration directly from keyword arguments.
Constructor Details
#initialize(api_key: nil, token: nil, account_id: nil, base_url: DEFAULT_BASE_URL, webhook_secret: nil, timeout: DEFAULT_TIMEOUT, logger: nil) ⇒ Configuration
Build a configuration directly from keyword arguments. Prefer passing
api_key (the documented X-Api-Key mechanism); token is the legacy
bearer fallback. base_url has its trailing slash stripped on assignment.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/assinafy/configuration.rb', line 68 def initialize(api_key: nil, token: nil, account_id: nil, base_url: DEFAULT_BASE_URL, webhook_secret: nil, timeout: DEFAULT_TIMEOUT, logger: nil) @api_key = api_key @token = token @account_id = account_id raise ValidationError.new('Base URL is required') unless base_url.is_a?(String) @base_url = base_url.strip.sub(%r{/+\z}, '') raise ValidationError.new('Base URL is required') if @base_url.empty? @webhook_secret = webhook_secret @timeout = normalize_timeout(timeout) @logger = logger end |
Instance Attribute Details
#account_id ⇒ String?
Returns default workspace ID.
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
#api_key ⇒ String?
Returns sent as X-Api-Key.
42 43 44 |
# File 'lib/assinafy/configuration.rb', line 42 def api_key @api_key end |
#base_url ⇒ String
Returns API base URL (trailing slash stripped).
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
#logger ⇒ Logger?
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
#timeout ⇒ Integer
Returns Faraday timeout in seconds.
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
#token ⇒ String?
Returns legacy bearer token (used when api_key is nil).
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
#webhook_secret ⇒ String?
Returns secret for Support::WebhookVerifier.
42 |
# File 'lib/assinafy/configuration.rb', line 42 attr_accessor :api_key, :token, :account_id, :base_url, :webhook_secret, :timeout, :logger |
Class Method Details
.from_hash(hash) ⇒ Configuration
Build a Assinafy::Configuration from a Hash with string or symbol keys.
Accepts both 'token' and 'access_token' for backwards compatibility.
Missing keys fall back to defaults (base_url => DEFAULT_BASE_URL,
timeout => DEFAULT_TIMEOUT); numeric strings are accepted, while
invalid and non-positive values raise ValidationError.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/assinafy/configuration.rb', line 104 def self.from_hash(hash) raise ValidationError.new('Configuration must be a Hash') unless hash.is_a?(Hash) h = hash.transform_keys(&:to_s) new( api_key: h['api_key'], token: h['token'] || h['access_token'], account_id: h['account_id'], base_url: h.key?('base_url') ? h['base_url'] : DEFAULT_BASE_URL, webhook_secret: h['webhook_secret'], timeout: h.key?('timeout') ? h['timeout'] : DEFAULT_TIMEOUT, logger: h['logger'] ) end |
Instance Method Details
#auth_headers ⇒ Hash{String=>String}
Return the HTTP headers used to authenticate requests, preferring
X-Api-Key (the documented mechanism) over a bearer token. When api_key
is set it wins; otherwise a non-nil token produces an Authorization: Bearer header; with neither credential set an empty Hash is returned.
138 139 140 141 142 143 144 145 |
# File 'lib/assinafy/configuration.rb', line 138 def auth_headers key = api_key.to_s.strip bearer = token.to_s.strip return { 'X-Api-Key' => key } unless key.empty? return { 'Authorization' => "Bearer #{bearer}" } unless bearer.empty? {} end |