In StripeObject#to_hash, call #to_hash on values which respond to it

This commit is contained in:
Vladimir Andrijevik 2014-02-12 16:46:00 +01:00
parent 218178ea54
commit 310c69e90d
2 changed files with 11 additions and 1 deletions

View File

@ -95,7 +95,10 @@ module Stripe
end
def to_hash
@values
@values.inject({}) do |acc, (key, value)|
acc[key] = value.respond_to?(:to_hash) ? value.to_hash : value
acc
end
end
def each(&blk)

View File

@ -16,5 +16,12 @@ module Stripe
assert_equal m.name, 'Stripe'
assert_equal m.api_key, 'apikey'
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