Skip to content

Commit

Permalink
Merge pull request #401 from herimedia/add-options-to-save
Browse files Browse the repository at this point in the history
Allow options (headers) to be passed into .save
  • Loading branch information
brandur committed Mar 18, 2016
2 parents b2db930 + db3059a commit 38f3263
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/stripe/api_operations/update.rb
Expand Up @@ -13,7 +13,11 @@ module Update
# and includes them in the create or update. If +:req_url:+ is included
# in the list, it overrides the update URL used for the create or
# update.
def save(params={})
# * +opts+ - A Hash of additional options (separate from the params /
# object values) to be added to the request. E.g. to allow for an
# idempotency_key to be passed in the request headers, or for the
# api_key to be overwritten. See {APIOperations::Request.request}.
def save(params={}, opts={})
# We started unintentionally (sort of) allowing attributes sent to
# +save+ to override values used during the update. So as not to break
# the API, this makes that official here.
Expand All @@ -28,7 +32,7 @@ def save(params={})
# generated a uri for this object with an identifier baked in
values.delete(:id)

response, opts = request(:post, save_url, values)
response, opts = request(:post, save_url, values, opts)
initialize_from(response, opts)

self
Expand Down
25 changes: 25 additions & 0 deletions test/stripe/api_resource_test.rb
Expand Up @@ -387,6 +387,31 @@ class ApiResourceTest < Test::Unit::TestCase
assert_equal false, c.livemode
end

should "updating should send along the idempotency-key header" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:idempotency_key] == 'bar'
end.returns(make_response(make_customer))
c = Stripe::Customer.new
c.save({}, { :idempotency_key => 'bar' })
assert_equal false, c.livemode
end

should "updating should fail if api_key is overwritten with nil" do
c = Stripe::Customer.new
assert_raises TypeError do
c.save({}, { :api_key => nil })
end
end

should "updating should use the supplied api_key" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:authorization] == 'Bearer sk_test_local'
end.returns(make_response(make_customer))
c = Stripe::Customer.new
c.save({}, { :api_key => 'sk_test_local' })
assert_equal false, c.livemode
end

should "deleting should send no props and result in an object that has no props other deleted" do
@mock.expects(:get).never
@mock.expects(:post).never
Expand Down

0 comments on commit 38f3263

Please sign in to comment.