extract method 'normalize_id' from 'StripeObject#initialize' & 'ListObject#retrieve'

This commit is contained in:
Andy Cohen 2015-07-27 15:20:42 -05:00
parent 31ccf7c6d3
commit 97c9249ab1
3 changed files with 12 additions and 15 deletions

View File

@ -16,12 +16,7 @@ module Stripe
end
def retrieve(id, opts={})
if id.kind_of?(Hash) # overloaded id
retrieve_params = id
id = retrieve_params.delete(:id)
else
retrieve_params = {}
end
id, retrieve_params = Util.normalize_id(id)
response, opts = request(:get,"#{url}/#{CGI.escape(id)}", retrieve_params, opts)
Util.convert_to_stripe_object(response, opts)
end

View File

@ -10,15 +10,7 @@ module Stripe
end
def initialize(id=nil, opts={})
# parameter overloading!
if id.kind_of?(Hash)
@retrieve_params = id.dup
@retrieve_params.delete(:id)
id = id[:id]
else
@retrieve_params = {}
end
id, @retrieve_params = Util.normalize_id(id)
@opts = opts
@values = {}
# This really belongs in APIResource, but not putting it there allows us

View File

@ -120,6 +120,16 @@ module Stripe
result
end
def self.normalize_id(id)
if id.kind_of?(Hash) # overloaded id
params_hash = id.dup
id = params_hash.delete(:id)
else
params_hash = {}
end
[id, params_hash]
end
# The secondary opts argument can either be a string or hash
# Turn this value into an api_key and a set of headers
def self.normalize_opts(opts)