Skip to content

Commit

Permalink
[api] add subscriptions.listAll() function
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Feb 8, 2011
1 parent b49fe40 commit 0dad6a4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/chargify/client.js
Expand Up @@ -99,7 +99,7 @@ function Client (httpClient) {

this.subscriptions = {
component: emit(function (sub, component) {
httpClient.get('/subscriptions/' + sub.id + '/components/' + component.id + '.json');
httpClient.get('/subscriptions/' + sub.id + '/components/' + component.id + '.json', '', callback(this));
}),

create: emit(function (sub) {
Expand All @@ -119,6 +119,30 @@ function Client (httpClient) {
httpClient.get('/customers/' + customer.id + '/subscriptions.json', '', callback(this));
}),

listAll: emit(function () {
var self = this, results = [];
function getPage (page) {
var path = '/subscriptions.json?page=' + page;
httpClient.get(path, '', function (status, response) {
if (status >= 200 && status < 300) {
var pageResults = tryParse(response);
if (pageResults && pageResults.length > 0) {
results = results.concat(pageResults);
getPage(++page);
}
else {
self.emit('success', results);
}
}
else {
self.emit('failure', tryParse(response));
}
});
}

getPage(1);
}),

update: emit(function (id, sub) {
httpClient.put('/subscriptions/' + id + '.json', JSON.stringify(sub), callback(this));
})
Expand Down

0 comments on commit 0dad6a4

Please sign in to comment.