From 54efdf5405d1ad0da410ec72d5fc404ae630e5d0 Mon Sep 17 00:00:00 2001 From: Andy Cohen Date: Mon, 27 Jul 2015 15:20:42 -0500 Subject: [PATCH] extract method 'normalize_id' from 'StripeObject#initialize' & 'ListObject#retrieve' --- lib/stripe/list_object.rb | 7 +------ lib/stripe/stripe_object.rb | 10 +--------- lib/stripe/util.rb | 10 ++++++++++ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/stripe/list_object.rb b/lib/stripe/list_object.rb index 9620667c..2da1852c 100644 --- a/lib/stripe/list_object.rb +++ b/lib/stripe/list_object.rb @@ -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 diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 0e581d46..88209a4f 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -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 diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 2aa618a2..5a129bd5 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -133,5 +133,15 @@ module Stripe raise TypeError.new('normalize_opts expects a string or a hash') end 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 end end