mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-08-24 00:00:51 -04:00
Refactor Stripe::Util.convert_to_stripe_object method
* Extract hash mapping into object_classes method/ivar * Use Hash#fetch instead of `if` and `||=`
This commit is contained in:
parent
616b169958
commit
da6736478e
@ -15,8 +15,8 @@ module Stripe
|
||||
end
|
||||
end
|
||||
|
||||
def self.convert_to_stripe_object(resp, api_key)
|
||||
types = {
|
||||
def self.object_classes
|
||||
@object_classes ||= {
|
||||
'charge' => Charge,
|
||||
'customer' => Customer,
|
||||
'invoiceitem' => InvoiceItem,
|
||||
@ -28,16 +28,15 @@ module Stripe
|
||||
'recipient' => Recipient,
|
||||
'list' => ListObject
|
||||
}
|
||||
end
|
||||
|
||||
def self.convert_to_stripe_object(resp, api_key)
|
||||
case resp
|
||||
when Array
|
||||
resp.map { |i| convert_to_stripe_object(i, api_key) }
|
||||
when Hash
|
||||
# Try converting to a known object class. If none available, fall back to generic APIResource
|
||||
if klass_name = resp[:object]
|
||||
klass = types[klass_name]
|
||||
end
|
||||
klass ||= StripeObject
|
||||
klass.construct_from(resp, api_key)
|
||||
object_classes.fetch(resp[:object]) { StripeObject }.construct_from(resp, api_key)
|
||||
else
|
||||
resp
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user