mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-08 00:01:02 -05:00
Merge pull request #118 from vandrijevik/nested-to-hash
Improved StripeObject#to_hash
This commit is contained in:
commit
52792264ef
@ -95,7 +95,10 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
@values
|
@values.inject({}) do |acc, (key, value)|
|
||||||
|
acc[key] = value.respond_to?(:to_hash) ? value.to_hash : value
|
||||||
|
acc
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def each(&blk)
|
def each(&blk)
|
||||||
|
|||||||
@ -16,5 +16,12 @@ module Stripe
|
|||||||
assert_equal 'Stripe', m.name
|
assert_equal 'Stripe', m.name
|
||||||
assert_equal 'apikey', m.api_key
|
assert_equal 'apikey', m.api_key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "recursively call to_hash on its values" do
|
||||||
|
nested = Stripe::StripeObject.construct_from({ :id => 7, :foo => 'bar' })
|
||||||
|
obj = Stripe::StripeObject.construct_from({ :id => 1, :nested => nested })
|
||||||
|
expected_hash = { :id => 1, :nested => { :id => 7, :foo => 'bar' } }
|
||||||
|
assert_equal expected_hash, obj.to_hash
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user