Class: Equipoise::Configuration

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

Constant Summary collapse

DEFAULT_BASE_URL =
"https://app.equipoi.se/api/v1"
DEFAULT_OPEN_TIMEOUT =
5
DEFAULT_READ_TIMEOUT =
30
DEFAULT_MAX_RETRIES =
2
DEFAULT_BATCH_SIZE =
500
READ_TIMEOUT_BOUNDS =

Hard limits — every producer-tunable transport knob is clamped into these ranges so a misconfigured producer can never punish the receiver:

read_timeout — floor keeps the producer patient enough that one slow
batch doesn't become two concurrent deliveries (a low timeout is how a
retry double-submits); ceiling stops a producer worker hanging forever.
max_retries  — ceiling means a misconfig can't retry-storm the receiver.
batch_size   — ceiling is the receiver's own per-batch cap (it rejects
anything larger); floor of 1 keeps slicing sane.

Out-of-range values are clamped, not raised (see #clamp_to).

(5..120).freeze
MAX_RETRIES_BOUNDS =
(0..5).freeze
BATCH_SIZE_BOUNDS =
(1..500).freeze
LOOPBACK_HOSTS =
%w[localhost 127.0.0.1 ::1 [::1]].freeze
PRODUCTION_HOST =

"app.equipoi.se"

URI.parse(DEFAULT_BASE_URL).host

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV) ⇒ Configuration

Returns a new instance of Configuration.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/equipoise/configuration.rb', line 70

def initialize(env: ENV)
  @env                 = env
  @api_key             = nil
  @base_url            = nil
  @source              = nil
  @open_timeout        = DEFAULT_OPEN_TIMEOUT
  @read_timeout        = DEFAULT_READ_TIMEOUT
  @max_retries         = DEFAULT_MAX_RETRIES
  @batch_size          = DEFAULT_BATCH_SIZE
  @retry_backoff_base  = 0.5
  @max_retry_backoff   = 30
  @allow_insecure_http = false
  @enabled             = true
  @logger              = nil
  @user_agent          = "equipoise-ruby/#{Equipoise::VERSION}"
end

Instance Attribute Details

#allow_insecure_httpObject

Returns the value of attribute allow_insecure_http.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def allow_insecure_http
  @allow_insecure_http
end

#api_keyObject

Explicit assignment wins; otherwise read ENV each call (rotation-aware).



96
97
98
# File 'lib/equipoise/configuration.rb', line 96

def api_key
  resolve(@api_key, "EQUIPOISE_API_KEY")
end

#base_urlObject



100
101
102
# File 'lib/equipoise/configuration.rb', line 100

def base_url
  resolve(@base_url, "EQUIPOISE_API_URL") || DEFAULT_BASE_URL
end

#batch_sizeObject

Clamped knobs — custom writers (below) hold every override inside its *_BOUNDS so the gem stays a good citizen no matter what a producer sets.



63
64
65
# File 'lib/equipoise/configuration.rb', line 63

def batch_size
  @batch_size
end

#enabledObject

Returns the value of attribute enabled.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def enabled
  @enabled
end

#loggerObject

Returns the value of attribute logger.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def logger
  @logger
end

#max_retriesObject

Clamped knobs — custom writers (below) hold every override inside its *_BOUNDS so the gem stays a good citizen no matter what a producer sets.



63
64
65
# File 'lib/equipoise/configuration.rb', line 63

def max_retries
  @max_retries
end

#max_retry_backoffObject

Returns the value of attribute max_retry_backoff.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def max_retry_backoff
  @max_retry_backoff
end

#open_timeoutObject

Returns the value of attribute open_timeout.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def open_timeout
  @open_timeout
end

#read_timeoutObject

Clamped knobs — custom writers (below) hold every override inside its *_BOUNDS so the gem stays a good citizen no matter what a producer sets.



63
64
65
# File 'lib/equipoise/configuration.rb', line 63

def read_timeout
  @read_timeout
end

#retry_backoff_baseObject

Returns the value of attribute retry_backoff_base.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def retry_backoff_base
  @retry_backoff_base
end

#sourceObject



104
105
106
# File 'lib/equipoise/configuration.rb', line 104

def source
  resolve(@source, "EQUIPOISE_SOURCE")
end

#user_agentObject

Returns the value of attribute user_agent.



54
55
56
# File 'lib/equipoise/configuration.rb', line 54

def user_agent
  @user_agent
end

Instance Method Details

#bounded_batch_size(requested = nil) ⇒ Object

The effective per-request chunk size: an explicit batch_sync(chunk_size:) wins, else the configured batch_size — but either way clamped to the receiver's hard cap so the gem never posts an oversized (and rejected) batch.



130
131
132
# File 'lib/equipoise/configuration.rb', line 130

def bounded_batch_size(requested = nil)
  clamp_to(:batch_size, (requested || batch_size).to_i, BATCH_SIZE_BOUNDS)
end

#disabled?Boolean

Test-mode kill switch. When disabled the client short-circuits to a no-op before any network call OR validation, so a stray EQUIPOISE_API_KEY in a producer's test env can never make a live call.

Returns:

  • (Boolean)


90
91
92
# File 'lib/equipoise/configuration.rb', line 90

def disabled?
  !enabled
end

#validate!Object

Raises:



134
135
136
137
138
139
140
141
# File 'lib/equipoise/configuration.rb', line 134

def validate!
  raise ConfigurationError, "api_key is required (set EQUIPOISE_API_KEY or Equipoise.configure)" if api_key.to_s.empty?
  raise ConfigurationError, "base_url is required" if base_url.to_s.empty?

  ensure_secure_transport!
  ensure_environment_match!
  true
end