From 46d992cf0dea9c76c7f2d67d51c761d967f6c5ac Mon Sep 17 00:00:00 2001 From: Amos47 Date: Tue, 15 Nov 2016 19:26:01 -0500 Subject: [PATCH] allow to_s to also pretty_generate embedded StripeObjects --- lib/stripe/stripe_object.rb | 2 +- test/stripe/stripe_object_test.rb | 34 ++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/stripe/stripe_object.rb b/lib/stripe/stripe_object.rb index d42aa6d0..afa01447 100644 --- a/lib/stripe/stripe_object.rb +++ b/lib/stripe/stripe_object.rb @@ -43,7 +43,7 @@ module Stripe end def to_s(*args) - JSON.pretty_generate(@values) + JSON.pretty_generate(to_hash) end def inspect diff --git a/test/stripe/stripe_object_test.rb b/test/stripe/stripe_object_test.rb index 5e211dc1..ba6725b6 100644 --- a/test/stripe/stripe_object_test.rb +++ b/test/stripe/stripe_object_test.rb @@ -96,7 +96,7 @@ module Stripe obj.update_attributes(:foo => 'bar') assert_equal true, obj.send(:metaclass).method_defined?(:foo) end - + should "warn that #refresh_from is deprecated" do old_stderr = $stderr $stderr = StringIO.new @@ -292,6 +292,38 @@ module Stripe assert_equal({ :id => 'id', :metadata => { :foo => 'bar' } }, serialized) end + should "#to_s will call to_s for all embedded stripe objects" do + obj = Stripe::StripeObject.construct_from({ + id: 'id', + #embeded list object + refunds: Stripe::ListObject.construct_from({ data: [ + #embedded object in list + Stripe::StripeObject.construct_from({ + id: 'id', + #embedded object in an object in a list object + metadata: Stripe::StripeObject.construct_from({ + foo: 'bar', + }) + }) + ]}), + # embeded stripe object + metadata: Stripe::StripeObject.construct_from({ + foo: 'bar', + }) + }) + expected = JSON.pretty_generate({ + id: 'id', + refunds: { + data: [ + {id: 'id', metadata: {foo: 'bar'}} + ] + }, + metadata: { foo: 'bar' } + }) + + assert_equal(expected, obj.to_s) + end + should "warn that .serialize_params is deprecated" do old_stderr = $stderr $stderr = StringIO.new