Merge pull request #118 from vandrijevik/nested-to-hash

Improved StripeObject#to_hash
This commit is contained in:
Amber Feng 2014-07-07 21:23:00 -07:00
commit 52792264ef
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 'Stripe', m.name
assert_equal 'apikey', m.api_key
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