Module: Stripe::RequestOptions

Defined in:
lib/stripe/request_options.rb

Overview

RequestOptions is a class that encapsulates configurable options for requests made to the Stripe API. It is used by the APIRequestor to set per-request options.

For internal use only. Does not provide a stable API and may be broken with future non-major changes.

Constant Summary collapse

OPTS_USER_SPECIFIED =

Options that a user is allowed to specify.

Set[
  :api_key,
  :idempotency_key,
  :stripe_account,
  :stripe_context,
  :stripe_version
].freeze
OPTS_COPYABLE =

Options that should be copyable from one StripeObject to another including options that may be internal.

(
  OPTS_USER_SPECIFIED + Set[:api_base]
).freeze
OPTS_PERSISTABLE =

Options that should be persisted between API requests.

(
  OPTS_USER_SPECIFIED - Set[:idempotency_key, :stripe_context]
).freeze

Class Method Summary collapse

Class Method Details

.combine_opts(object_opts, req_opts) ⇒ Object

Merges requestor options hash on a StripeObject with a per-request options hash, giving precedence to the per-request options. Returns the merged request options. Expects two hashes.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stripe/request_options.rb', line 60

def self.combine_opts(object_opts, req_opts)
  merged_opts = {
    api_key: req_opts[:api_key] || object_opts[:api_key],
    idempotency_key: req_opts[:idempotency_key],
    stripe_account: req_opts[:stripe_account] || object_opts[:stripe_account],
    stripe_context: req_opts[:stripe_context] || object_opts[:stripe_context],
    stripe_version: req_opts[:stripe_version] || object_opts[:stripe_version],
  }

  # Remove nil values from headers
  merged_opts.delete_if { |_, v| v.nil? }

  merged_opts
end

.copyable(req_opts) ⇒ Object

Get options that are copyable from StripeObject to StripeObject



122
123
124
125
126
# File 'lib/stripe/request_options.rb', line 122

def self.copyable(req_opts)
  req_opts.select do |k, _v|
    RequestOptions::OPTS_COPYABLE.include?(k)
  end
end

.error_on_non_string_user_opts(normalized_opts) ⇒ Object

Validates a normalized opts hash.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stripe/request_options.rb', line 95

def self.error_on_non_string_user_opts(normalized_opts)
  OPTS_USER_SPECIFIED.each do |opt|
    next unless normalized_opts.key?(opt)

    val = normalized_opts[opt]
    next if val.nil?
    next if val.is_a?(String)

    raise ArgumentError,
          "request option '#{opt}' should be a string value " \
          "(was a #{val.class})"
  end
end

.extract_opts_from_hash(opts) ⇒ Object

Extracts options from a user-provided hash, returning a new request options hash containing the recognized request options and a ‘headers` entry for the remaining options.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/stripe/request_options.rb', line 77

def self.extract_opts_from_hash(opts)
  req_opts = {}
  normalized_opts = Util.normalize_opts(opts.clone)

  RequestOptions.error_on_non_string_user_opts(normalized_opts)

  OPTS_USER_SPECIFIED.each do |opt|
    req_opts[opt] = normalized_opts[opt] if normalized_opts.key?(opt)
    normalized_opts.delete(opt)
  end

  # Remaining user-provided opts should be treated as headers
  req_opts[:headers] = Util.normalize_headers(normalized_opts) if normalized_opts.any?

  req_opts
end

.merge_config_and_opts(config, req_opts) ⇒ Object

Merges requestor options on a StripeConfiguration object with a per-request options hash, giving precedence to the per-request options. Expects StripeConfiguration and hash.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/stripe/request_options.rb', line 35

def self.merge_config_and_opts(config, req_opts)
  # Raise an error if config is not a StripeConfiguration object
  unless config.is_a?(StripeConfiguration)
    raise ArgumentError, "config must be a Stripe::StripeConfiguration object"
  end

  merged_opts = {
    api_key: req_opts[:api_key] || config.api_key,
    idempotency_key: req_opts[:idempotency_key],
    stripe_account: req_opts[:stripe_account] || config.,
    stripe_context: req_opts[:stripe_context] || config.stripe_context,
    stripe_version: req_opts[:stripe_version] || config.api_version,
    headers: req_opts[:headers] || {},
  }

  # Remove nil values from headers
  merged_opts.delete_if { |_, v| v.nil? }

  merged_opts
end

.persistable(req_opts) ⇒ Object

Get options that persist between requests



110
111
112
113
114
115
116
117
118
119
# File 'lib/stripe/request_options.rb', line 110

def self.persistable(req_opts)
  opts_to_persist = {}

  # Hash#select returns an array before 1.9
  req_opts.each do |k, v|
    opts_to_persist[k] = v if RequestOptions::OPTS_PERSISTABLE.include?(k)
  end

  opts_to_persist
end