Class: Crystil::Config
- Inherits:
-
Object
- Object
- Crystil::Config
- Defined in:
- lib/crystil/config.rb,
sig/crystil/config.rbs
Overview
Thread-safe configuration for Crystil client
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
Returns the value of attribute api_key.
-
#api_url ⇒ String
readonly
Returns the value of attribute api_url.
-
#collector_url ⇒ String
readonly
Returns the value of attribute collector_url.
-
#timeout ⇒ Numeric
readonly
Returns the value of attribute timeout.
-
#version ⇒ String
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #attribution ⇒ Crystil::Attribution?
- #attribution=(value) ⇒ void
-
#initialize(api_key: nil, collector_url: nil, api_url: nil, timeout: nil) ⇒ Config
constructor
A new instance of Config.
- #new_transaction ⇒ String
-
#nonempty_env(name) ⇒ String?
Returns ENV unless it's missing or empty.
- #raise_if_irrelevant ⇒ Boolean
- #raise_if_irrelevant=(value) ⇒ void
- #secs_irrelevant_request_timeout ⇒ Numeric
- #secs_irrelevant_request_timeout=(value) ⇒ void
- #tx_uuid ⇒ String
- #tx_uuid=(value) ⇒ void
Constructor Details
#initialize(api_key: nil, collector_url: nil, api_url: nil, timeout: nil) ⇒ Config
Returns a new instance of Config.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/crystil/config.rb', line 10 def initialize(api_key: nil, collector_url: nil, api_url: nil, timeout: nil) @api_key = api_key # URL precedence: constructor arg > CRYSTIL_*_URL_BASE env var > hardcoded # prod default. Matches the JS SDK so an integration-test run pointed at # staging or a local backend only requires setting the env vars in # `.env` — no spec changes needed. @collector_url = collector_url || nonempty_env("CRYSTIL_COLLECTOR_URL_BASE") || "https://collector.crystil.com" @api_url = api_url || nonempty_env("CRYSTIL_API_URL_BASE") || "https://api.crystil.com" @timeout = timeout || 5 @version = Crystil::VERSION @attribution = Concurrent::AtomicReference.new(nil) @tx_uuid = Concurrent::AtomicReference.new(SecureRandom.uuid) @raise_if_irrelevant = Concurrent::AtomicReference.new(false) @secs_irrelevant_request_timeout = Concurrent::AtomicReference.new(5) end |
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/crystil/config.rb', line 8 def api_key @api_key end |
#api_url ⇒ String (readonly)
Returns the value of attribute api_url.
8 9 10 |
# File 'lib/crystil/config.rb', line 8 def api_url @api_url end |
#collector_url ⇒ String (readonly)
Returns the value of attribute collector_url.
8 9 10 |
# File 'lib/crystil/config.rb', line 8 def collector_url @collector_url end |
#timeout ⇒ Numeric (readonly)
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/crystil/config.rb', line 8 def timeout @timeout end |
#version ⇒ String (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/crystil/config.rb', line 8 def version @version end |
Instance Method Details
#attribution ⇒ Crystil::Attribution?
30 31 32 |
# File 'lib/crystil/config.rb', line 30 def attribution @attribution.get end |
#attribution=(value) ⇒ void
This method returns an undefined value.
34 35 36 |
# File 'lib/crystil/config.rb', line 34 def attribution=(value) @attribution.set(value) end |
#new_transaction ⇒ String
62 63 64 |
# File 'lib/crystil/config.rb', line 62 def new_transaction @tx_uuid.set(SecureRandom.uuid) end |
#nonempty_env(name) ⇒ String?
Returns ENV unless it's missing or empty. We can't use the JS-style
ENV[name] || default because Ruby treats empty strings as truthy — and
the .env.example ships with blank values for the URL keys, so naïve
truthiness would route every request to "".
72 73 74 75 76 77 |
# File 'lib/crystil/config.rb', line 72 def nonempty_env(name) value = ENV.fetch(name, nil) return nil if value.nil? || value.empty? value end |
#raise_if_irrelevant ⇒ Boolean
46 47 48 |
# File 'lib/crystil/config.rb', line 46 def raise_if_irrelevant @raise_if_irrelevant.get end |
#raise_if_irrelevant=(value) ⇒ void
This method returns an undefined value.
50 51 52 |
# File 'lib/crystil/config.rb', line 50 def raise_if_irrelevant=(value) @raise_if_irrelevant.set(value) end |
#secs_irrelevant_request_timeout ⇒ Numeric
54 55 56 |
# File 'lib/crystil/config.rb', line 54 def secs_irrelevant_request_timeout @secs_irrelevant_request_timeout.get end |
#secs_irrelevant_request_timeout=(value) ⇒ void
This method returns an undefined value.
58 59 60 |
# File 'lib/crystil/config.rb', line 58 def secs_irrelevant_request_timeout=(value) @secs_irrelevant_request_timeout.set(value) end |
#tx_uuid ⇒ String
38 39 40 |
# File 'lib/crystil/config.rb', line 38 def tx_uuid @tx_uuid.get end |
#tx_uuid=(value) ⇒ void
This method returns an undefined value.
42 43 44 |
# File 'lib/crystil/config.rb', line 42 def tx_uuid=(value) @tx_uuid.set(value) end |