mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-11-29 00:01:18 -05:00
As seen in #928, the `refresh` method doesn't work for an event class. This is because event has a field called `request`, and it ends up replacing the `request` method that it inherited from being an API resource, so when `refresh` tries to make a request, it fails because it tries to invoke it on the accessor added for the event's property. Here we give `request` a much more unique name so that it will never conflict with a property field again, and update all internal references to use the new name. We use `alias` to make the old name available for backwards compatibility reasons because its been around for so long that people are probably calling it. Fixes #928.
97 lines
3.0 KiB
Ruby
97 lines
3.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Stripe
|
|
module APIOperations
|
|
module Request
|
|
module ClassMethods
|
|
def execute_resource_request(method, url,
|
|
params = {}, opts = {})
|
|
params ||= {}
|
|
|
|
error_on_invalid_params(params)
|
|
warn_on_opts_in_params(params)
|
|
|
|
opts = Util.normalize_opts(opts)
|
|
error_on_non_string_user_opts(opts)
|
|
|
|
opts[:client] ||= StripeClient.active_client
|
|
|
|
headers = opts.clone
|
|
api_key = headers.delete(:api_key)
|
|
api_base = headers.delete(:api_base)
|
|
client = headers.delete(:client)
|
|
# Assume all remaining opts must be headers
|
|
|
|
resp, opts[:api_key] = client.execute_request(
|
|
method, url,
|
|
api_base: api_base, api_key: api_key,
|
|
headers: headers, params: params
|
|
)
|
|
|
|
# Hash#select returns an array before 1.9
|
|
opts_to_persist = {}
|
|
opts.each do |k, v|
|
|
opts_to_persist[k] = v if Util::OPTS_PERSISTABLE.include?(k)
|
|
end
|
|
|
|
[resp, opts_to_persist]
|
|
end
|
|
|
|
# This method used to be called `request`, but it's such a short name
|
|
# that it eventually conflicted with the name of a field on an API
|
|
# resource (specifically, `Event#request`), so it was renamed to
|
|
# something more unique.
|
|
#
|
|
# The former name had been around for just about forever though, and
|
|
# although all internal uses have been renamed, I've left this alias in
|
|
# place for backwards compatibility. Consider removing it on the next
|
|
# major.
|
|
alias request execute_resource_request
|
|
|
|
private def error_on_non_string_user_opts(opts)
|
|
Util::OPTS_USER_SPECIFIED.each do |opt|
|
|
next unless opts.key?(opt)
|
|
|
|
val = 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
|
|
|
|
private def error_on_invalid_params(params)
|
|
return if params.nil? || params.is_a?(Hash)
|
|
|
|
raise ArgumentError,
|
|
"request params should be either a Hash or nil " \
|
|
"(was a #{params.class})"
|
|
end
|
|
|
|
private def warn_on_opts_in_params(params)
|
|
Util::OPTS_USER_SPECIFIED.each do |opt|
|
|
if params.key?(opt)
|
|
warn("WARNING: '#{opt}' should be in opts instead of params.")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.included(base)
|
|
base.extend(ClassMethods)
|
|
end
|
|
|
|
protected def execute_resource_request(method, url,
|
|
params = {}, opts = {})
|
|
opts = @opts.merge(Util.normalize_opts(opts))
|
|
self.class.execute_resource_request(method, url, params, opts)
|
|
end
|
|
|
|
# See notes on `alias` above.
|
|
alias request execute_resource_request
|
|
end
|
|
end
|
|
end
|