Class: Api2Convert::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/api2convert/config.rb

Overview

Immutable client configuration.

Build via Config.create, which clamps every knob so a caller value can neither busy-loop the poll (interval floor) nor poll unbounded (timeout ceiling).

Constant Summary collapse

DEFAULT_BASE_URL =

Default API base URL — includes the /v2 path segment, no trailing slash.

"https://api.api2convert.com/v2"
MIN_POLL_INTERVAL =

Hard floor for the job-poll interval (seconds); prevents a busy-spin self-DDOS.

0.5
MAX_POLL_TIMEOUT =

Hard ceiling for the total job-poll timeout (4 hours); bounds an unbounded poll.

14_400

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url:, timeout:, max_retries:, poll_interval:, poll_max_interval:, poll_timeout:) ⇒ Config

The constructor does not clamp — use create (the single entry point the client uses) so a caller value can never busy-loop or poll unbounded.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/api2convert/config.rb', line 24

def initialize(api_key:, base_url:, timeout:, max_retries:,
               poll_interval:, poll_max_interval:, poll_timeout:)
  @api_key = api_key
  @base_url = base_url
  @timeout = timeout
  @max_retries = max_retries
  @poll_interval = poll_interval
  @poll_max_interval = poll_max_interval
  @poll_timeout = poll_timeout
  freeze
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def base_url
  @base_url
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def max_retries
  @max_retries
end

#poll_intervalObject (readonly)

Returns the value of attribute poll_interval.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def poll_interval
  @poll_interval
end

#poll_max_intervalObject (readonly)

Returns the value of attribute poll_max_interval.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def poll_max_interval
  @poll_max_interval
end

#poll_timeoutObject (readonly)

Returns the value of attribute poll_timeout.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def poll_timeout
  @poll_timeout
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



19
20
21
# File 'lib/api2convert/config.rb', line 19

def timeout
  @timeout
end

Class Method Details

.create(api_key, base_url: nil, timeout: nil, max_retries: nil, poll_interval: nil, poll_max_interval: nil, poll_timeout: nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api2convert/config.rb', line 36

def self.create(api_key, base_url: nil, timeout: nil, max_retries: nil,
                poll_interval: nil, poll_max_interval: nil, poll_timeout: nil)
  interval = [MIN_POLL_INTERVAL, (poll_interval.nil? ? 1.0 : poll_interval).to_f].max
  max_interval = [interval, (poll_max_interval.nil? ? 5.0 : poll_max_interval).to_f].max
  timeout_value = (poll_timeout.nil? ? 300 : poll_timeout).to_i.clamp(0, MAX_POLL_TIMEOUT)

  new(
    api_key: api_key,
    base_url: (base_url.nil? ? DEFAULT_BASE_URL : base_url).sub(%r{/+\z}, ""),
    timeout: [1, (timeout.nil? ? 30 : timeout).to_i].max,
    max_retries: [0, (max_retries.nil? ? 2 : max_retries).to_i].max,
    poll_interval: interval,
    poll_max_interval: max_interval,
    poll_timeout: timeout_value
  )
end

Instance Method Details

#inspectObject

Redacted representation — the API key is masked so it can never be printed in cleartext by p config, a logger, or an object dumped in a backtrace.



55
56
57
58
59
60
# File 'lib/api2convert/config.rb', line 55

def inspect
  "#<#{self.class.name} api_key=#{Support::Secret.mask(@api_key)} " \
    "base_url=#{@base_url.inspect} timeout=#{@timeout} max_retries=#{@max_retries} " \
    "poll_interval=#{@poll_interval} poll_max_interval=#{@poll_max_interval} " \
    "poll_timeout=#{@poll_timeout}>"
end

#to_sObject



62
63
64
# File 'lib/api2convert/config.rb', line 62

def to_s
  inspect
end