Replace api_key with an options hash

For now, only two options are supported: `api_key` and
`idempotency_key`. In the future, we'll be adding support for additional
headers as needed.
This commit is contained in:
Kyle Conroy 2014-12-17 19:38:33 -08:00
parent 9407c932af
commit e3a68bb3b9
10 changed files with 103 additions and 30 deletions

View File

@ -2,7 +2,8 @@ module Stripe
module APIOperations
module Create
module ClassMethods
def create(params={}, api_key=nil, headers={})
def create(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(:post, self.url, api_key, params, headers)
Util.convert_to_stripe_object(response, api_key)
end

View File

@ -1,7 +1,8 @@
module Stripe
module APIOperations
module Delete
def delete(params = {}, api_key=nil, headers={})
def delete(params = {}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(:delete, url, api_key, params, headers)
refresh_from(response, api_key)
end

View File

@ -2,7 +2,8 @@ module Stripe
module APIOperations
module List
module ClassMethods
def all(filters={}, api_key=nil, headers={})
def all(filters={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(:get, url, api_key, filters, headers)
Util.convert_to_stripe_object(response, api_key)
end

View File

@ -6,7 +6,8 @@ module Stripe
'/v1/application_fees'
end
def refund(params={}, api_key=nil, headers={})
def refund(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(:post, refund_url, api_key, params, headers)
refresh_from(response, api_key)
end

View File

@ -4,24 +4,32 @@ module Stripe
include Stripe::APIOperations::Create
include Stripe::APIOperations::Update
def refund(params={}, headers={})
response, api_key = Stripe.request(:post, refund_url, @api_key, params, headers)
def refund(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, refund_url, api_key || @api_key, params, headers)
refresh_from(response, api_key)
end
def capture(params={}, headers={})
response, api_key = Stripe.request(:post, capture_url, @api_key, params, headers)
def capture(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, capture_url, api_key || @api_key, params, headers)
refresh_from(response, api_key)
end
def update_dispute(params={}, headers={})
response, api_key = Stripe.request(:post, dispute_url, @api_key, params, headers)
def update_dispute(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, dispute_url, api_key || @api_key, params, headers)
refresh_from({ :dispute => response }, api_key, true)
dispute
end
def close_dispute
response, api_key = Stripe.request(:post, close_dispute_url, @api_key)
def close_dispute(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, close_dispute_url, api_key || @api_key, params, headers)
refresh_from(response, api_key)
end

View File

@ -5,8 +5,9 @@ module Stripe
include Stripe::APIOperations::Update
include Stripe::APIOperations::List
def add_invoice_item(params)
InvoiceItem.create(params.merge(:customer => id), @api_key)
def add_invoice_item(params, opts={})
opts[:api_key] = @api_key
InvoiceItem.create(params.merge(:customer => id), opts)
end
def invoices
@ -25,24 +26,31 @@ module Stripe
Charge.all({ :customer => id }, @api_key)
end
def create_upcoming_invoice(params={})
Invoice.create(params.merge(:customer => id), @api_key)
def create_upcoming_invoice(params={}, opts={})
opts[:api_key] = @api_key
Invoice.create(params.merge(:customer => id), opts)
end
def cancel_subscription(params={}, headers={})
response, api_key = Stripe.request(:delete, subscription_url, @api_key, params, headers)
def cancel_subscription(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:delete, subscription_url, api_key || @api_key, params, headers)
refresh_from({ :subscription => response }, api_key, true)
subscription
end
def update_subscription(params={}, headers={})
response, api_key = Stripe.request(:post, subscription_url, @api_key, params, headers)
def update_subscription(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, subscription_url, api_key || @api_key, params, headers)
refresh_from({ :subscription => response }, api_key, true)
subscription
end
def create_subscription(params={}, headers={})
response, api_key = Stripe.request(:post, subscriptions_url, @api_key, params, headers)
def create_subscription(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
response, api_key = Stripe.request(
:post, subscriptions_url, api_key || @api_key, params, headers)
refresh_from({ :subscription => response }, api_key, true)
subscription
end

View File

@ -20,15 +20,17 @@ module Stripe
Util.convert_to_stripe_object(response, api_key)
end
def create(params={}, api_key=nil, headers={})
def create(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
api_key ||= @api_key
response, api_key = Stripe.request(:post, url, api_key, params, headers)
Util.convert_to_stripe_object(response, api_key)
end
def all(params={}, api_key=nil)
def all(params={}, opts={})
api_key, headers = Util.parse_opts(opts)
api_key ||= @api_key
response, api_key = Stripe.request(:get, url, api_key, params)
response, api_key = Stripe.request(:get, url, api_key, params, headers)
Util.convert_to_stripe_object(response, api_key)
end
end

View File

@ -113,5 +113,24 @@ module Stripe
end
result
end
# The secondary opts argument can either be a string or hash
# Turn this value into an api_key and a set of headers
def self.parse_opts(opts)
case opts
when NilClass
return nil, {}
when String
return opts, {}
when Hash
headers = {}
if opts[:idempotency_key]
headers[:idempotency_key] = opts[:idempotency_key]
end
return opts[:api_key], headers
else
raise TypeError.new("parse_opts expects a string or a hash")
end
end
end
end

View File

@ -114,13 +114,15 @@ module Stripe
end
context "with valid credentials" do
should "send along additional headers" do
should "send along the idempotency-key header" do
Stripe.expects(:execute_request).with do |opts|
opts[:headers][:foo] == 'bar'
opts[:headers][:idempotency_key] == 'bar'
end.returns(test_response(test_charge))
Stripe::Charge.create({:card => {:number => '4242424242424242'}},
'local', {:foo => 'bar'})
Stripe::Charge.create({:card => {:number => '4242424242424242'}}, {
:idempotency_key => 'bar',
:api_key => 'local',
})
end
should "urlencode values in GET params" do

View File

@ -25,5 +25,35 @@ module Stripe
symbolized = Stripe::Util.symbolize_names(start)
assert_equal(finish, symbolized)
end
should "parse a nil opts argument" do
api_key, headers = Stripe::Util.parse_opts(nil)
assert_equal({}, headers)
assert_equal(nil, api_key)
end
should "parse a string opts argument" do
api_key, headers = Stripe::Util.parse_opts('foo')
assert_equal({}, headers)
assert_equal('foo', api_key)
end
should "parse a hash opts argument with just api_key" do
api_key, headers = Stripe::Util.parse_opts({:api_key => 'foo'})
assert_equal({}, headers)
assert_equal('foo', api_key)
end
should "parse a hash opts argument with just idempotency_key" do
api_key, headers = Stripe::Util.parse_opts({:idempotency_key => 'foo'})
assert_equal({:idempotency_key => 'foo'}, headers)
assert_equal(nil, api_key)
end
should "parse a hash opts argument both idempotency_key and api_key" do
api_key, headers = Stripe::Util.parse_opts({:api_key => 'bar', :idempotency_key => 'foo'})
assert_equal({:idempotency_key => 'foo'}, headers)
assert_equal('bar', api_key)
end
end
end
end