From 0dad6a470a9cb08ebbcb566e9c1fa8f12f3e4c7b Mon Sep 17 00:00:00 2001 From: indexzero Date: Mon, 7 Feb 2011 22:01:35 -0500 Subject: [PATCH] [api] add subscriptions.listAll() function --- lib/chargify/client.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/chargify/client.js b/lib/chargify/client.js index ca9089a..a1b1003 100644 --- a/lib/chargify/client.js +++ b/lib/chargify/client.js @@ -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)); })