Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode arrays as hashes when needed #596

Merged
merged 1 commit into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/stripe/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Invoice < APIResource
OBJECT_NAME = "invoice".freeze

def self.upcoming(params, opts = {})
params[:subscription_items] = Util.array_to_hash(params[:subscription_items]) if params[:subscription_items]
resp, opts = request(:get, upcoming_url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/stripe/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ def pay(params, opts = {})
end

def return_order(params, opts = {})
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
resp, opts = request(:post, returns_url, params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end

def self.create(params = {}, opts = {})
params[:items] = Util.array_to_hash(params[:items]) if params[:items]
super(params, opts)
end

private

def pay_url
Expand Down
10 changes: 10 additions & 0 deletions lib/stripe/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def self.create(params = {}, opts = {})
super(params, opts)
end

def serialize_params(options = {})
update_hash = super
if @unsaved_values.include?(:items)
value = Util.array_to_hash(@values[:items])
update_hash[:items] =
serialize_params_value(value, nil, true, options[:force], key: :items)
end
update_hash
end

private

def discount_url
Expand Down
46 changes: 46 additions & 0 deletions test/stripe/subscription_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,51 @@ class SubscriptionTest < Test::Unit::TestCase
assert subscription.is_a?(Stripe::Subscription)
end
end

context "#serialize_params" do
should "serialize when items is set to an Array" do
obj = Stripe::Util.convert_to_stripe_object({
object: "subscription",
items: Stripe::Util.convert_to_stripe_object(
object: "list",
data: []
),
}, {})
obj.items = [
{ id: "si_foo", deleted: true },
{ plan: "plan_bar" },
]

expected = {
items: {
:"0" => { id: "si_foo", deleted: true },
:"1" => { plan: "plan_bar" },
},
}
assert_equal(expected, obj.serialize_params)
end

should "serialize when items is set to a Hash" do
obj = Stripe::Util.convert_to_stripe_object({
object: "subscription",
items: Stripe::Util.convert_to_stripe_object(
object: "list",
data: []
),
}, {})
obj.items = {
"0" => { id: "si_foo", deleted: true },
"1" => { plan: "plan_bar" },
}

expected = {
items: {
:"0" => { id: "si_foo", deleted: true },
:"1" => { plan: "plan_bar" },
},
}
assert_equal(expected, obj.serialize_params)
end
end
end
end