Class: Dinie::Internal::RequestOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/dinie/runtime/request_options.rb

Overview

Normalized per-call options, the trailing ‘request_options:` Hash every public method accepts (architecture §12, RB5). Validates types up front and freezes; the actual header merge against the client defaults (where a `nil` value removes a default) and the timeout/retry wiring happen in the transport (story 003).

Examples:

Dinie::Internal::RequestOptions.coerce(timeout: 5, headers: { "x-trace" => "abc" })

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timeout: nil, idempotency_key: nil, headers: nil, max_retries: nil) ⇒ RequestOptions

Returns a new instance of RequestOptions.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil)

    seconds; must be a non-negative Numeric

  • idempotency_key (String, nil) (defaults to: nil)
  • headers (Hash, nil) (defaults to: nil)

    values must be String or nil

  • max_retries (Integer, nil) (defaults to: nil)

    must be a non-negative Integer

Raises:

  • (ArgumentError)

    on an invalid value type



39
40
41
42
43
44
45
# File 'lib/dinie/runtime/request_options.rb', line 39

def initialize(timeout: nil, idempotency_key: nil, headers: nil, max_retries: nil)
  @timeout = validate_timeout(timeout)
  @idempotency_key = validate_string(idempotency_key, :idempotency_key)
  @headers = validate_headers(headers)
  @max_retries = validate_max_retries(max_retries)
  freeze
end

Instance Attribute Details

#headersHash{String => String, nil}? (readonly)

Returns per-call header overrides (a ‘nil` value removes a default).

Returns:

  • (Hash{String => String, nil}, nil)

    per-call header overrides (a ‘nil` value removes a default)



18
19
20
# File 'lib/dinie/runtime/request_options.rb', line 18

def headers
  @headers
end

#idempotency_keyString? (readonly)

Returns explicit idempotency key (overrides the auto-generated one).

Returns:

  • (String, nil)

    explicit idempotency key (overrides the auto-generated one)



16
17
18
# File 'lib/dinie/runtime/request_options.rb', line 16

def idempotency_key
  @idempotency_key
end

#max_retriesInteger? (readonly)

Returns per-call retry budget override.

Returns:

  • (Integer, nil)

    per-call retry budget override



20
21
22
# File 'lib/dinie/runtime/request_options.rb', line 20

def max_retries
  @max_retries
end

#timeoutNumeric? (readonly)

Returns per-call timeout, in seconds.

Returns:

  • (Numeric, nil)

    per-call timeout, in seconds



14
15
16
# File 'lib/dinie/runtime/request_options.rb', line 14

def timeout
  @timeout
end

Class Method Details

.coerce(value) ⇒ RequestOptions

Coerce a value into a Dinie::Internal::RequestOptions: pass an instance through, or build one from a Hash (string or symbol keys; ‘nil` → empty).

Parameters:

Returns:

Raises:

  • (ArgumentError)

    on an unknown key or an invalid value type



28
29
30
31
32
# File 'lib/dinie/runtime/request_options.rb', line 28

def self.coerce(value)
  return value if value.is_a?(self)

  new(**(value || {}).transform_keys(&:to_sym))
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


55
56
57
# File 'lib/dinie/runtime/request_options.rb', line 55

def ==(other)
  other.is_a?(RequestOptions) && other.to_h == to_h
end

#hashInteger

Returns:

  • (Integer)


61
62
63
# File 'lib/dinie/runtime/request_options.rb', line 61

def hash
  to_h.hash
end

#to_hHash{Symbol => Object} Also known as: to_hash

Returns the normalized options.

Returns:

  • (Hash{Symbol => Object})

    the normalized options



48
49
50
# File 'lib/dinie/runtime/request_options.rb', line 48

def to_h
  { timeout: timeout, idempotency_key: idempotency_key, headers: headers, max_retries: max_retries }
end