Skip to content

Commit

Permalink
Add support for accounts resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Krausz committed Feb 13, 2015
1 parent aa068ef commit 8db8f0d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
36 changes: 32 additions & 4 deletions lib/resources/Account.js
Expand Up @@ -2,12 +2,40 @@

var StripeResource = require('../StripeResource');
var stripeMethod = StripeResource.method;
var utils = require('../utils');

module.exports = StripeResource.extend({
path: 'account',
// Since path can either be `account` or `accounts`, support both through stripeMethod path

retrieve: stripeMethod({
method: 'GET'
})
create: stripeMethod({
method: 'POST',
path: 'accounts'
}),

list: stripeMethod({
method: 'GET',
path: 'accounts'
}),

update: stripeMethod({
method: 'POST',
path: 'accounts/{id}',
urlParams: ['id']
}),

retrieve: function(id) {
// Old bindings supported no-arg access to /v1/account, support that if ID is not passed
if (id === undefined || (typeof id === 'string' && utils.isAuthKey(id))) {
return stripeMethod({
method: 'GET',
path: 'account'
}).apply(this, arguments);
} else {
return stripeMethod({
method: 'GET',
path: 'accounts/{id}',
urlParams: ['id']
}).apply(this, arguments);
}
},
});
11 changes: 8 additions & 3 deletions lib/utils.js
Expand Up @@ -13,11 +13,13 @@ var utils = module.exports = {
},

isOptionsHash: function (o) {
return toString.call(o) === '[object Object]' && (o.hasOwnProperty('api_key') || o.hasOwnProperty('idempotency_key'));
return _.isPlainObject(o) && _.some(['api_key', 'idempotency_key', 'stripe_account'], function(key) {
return o.hasOwnProperty(key);
});
},

isObject: function(o) {
return toString.call(o) === '[object Object]';
return _.isPlainObject(o);
},

/**
Expand Down Expand Up @@ -94,8 +96,11 @@ var utils = module.exports = {
if (params.idempotency_key) {
opts.headers['Idempotency-Key'] = params.idempotency_key;
}
if (params.stripe_account) {
opts.headers['Stripe-Account'] = params.stripe_account;
}
}
}
}
return opts;
},

Expand Down
28 changes: 27 additions & 1 deletion test/resources/Account.spec.js
Expand Up @@ -7,7 +7,7 @@ describe('Account Resource', function() {

describe('retrieve', function() {

it('Sends the correct request', function() {
it('Sends the correct request with no params', function() {

stripe.account.retrieve();
expect(stripe.LAST_REQUEST).to.deep.equal({
Expand All @@ -19,6 +19,32 @@ describe('Account Resource', function() {

});

it('Sends the correct request with ID param', function() {

stripe.account.retrieve('foo');
expect(stripe.LAST_REQUEST).to.deep.equal({
method: 'GET',
url: '/v1/accounts/foo',
data: {},
headers: {},
});

});

it('Sends the correct request with secret key', function() {

var key = 'sk_12345678901234567890123456789012'
stripe.account.retrieve(key);
expect(stripe.LAST_REQUEST).to.deep.equal({
auth: key,
method: 'GET',
url: '/v1/account',
data: {},
headers: {},
});

});

});

});

0 comments on commit 8db8f0d

Please sign in to comment.