Permalink
Browse files

[api] add subscriptions.listAll() function

  • Loading branch information...
1 parent b49fe40 commit 0dad6a470a9cb08ebbcb566e9c1fa8f12f3e4c7b @indexzero indexzero committed Feb 8, 2011
Showing with 25 additions and 1 deletion.
  1. +25 −1 lib/chargify/client.js
View
@@ -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) {
@@ -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));
})

0 comments on commit 0dad6a4

Please sign in to comment.