diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index 71e937b0..dfed9828 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -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) diff --git a/test/stripe/stripe_object_test.rb b/test/stripe/stripe_object_test.rb index c9eb2dc0..bb62aa95 100644 --- a/test/stripe/stripe_object_test.rb +++ b/test/stripe/stripe_object_test.rb @@ -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