Permalink
Browse files

[api] Added more functionality to Chargify client

  • Loading branch information...
1 parent c89e826 commit e8a2ecb7a3814d7e1158a91c01aecc326b7a4cd4 @indexzero indexzero committed Oct 19, 2010
Showing with 103 additions and 67 deletions.
  1. +96 −59 lib/chargify/client.js
  2. +7 −8 spec/integration/chargify/customers.spec.js
View
@@ -1,106 +1,144 @@
require.paths.unshift(__dirname+'/../');
-var http = require ('http')
-var events = require ('events')
-var querystring = require ('querystring')
-exports.createClient = function(opts){
+var http = require ('http'),
+ events = require ('events'),
+ querystring = require ('querystring');
+
+exports.createClient = function (opts) {
var httpClient = new SimpleHttpClient(opts.key, opts.password, opts.site+'.chargify.com')
return new Client(httpClient)
}
exports._Client = Client
-function Client(httpClient){
- var callback = function(emitter){
- return function(status, response){
- if(status >= 200 && status < 300)
+function Client (httpClient) {
+ var callback = function (emitter) {
+ return function (status, response) {
+ if (status >= 200 && status < 300)
emitter.emit('success', JSON.parse(response))
else
emitter.emit('failure', JSON.parse(response))
}
- }
- var emit = function(cb){
- return function(){
+ };
+
+ var emit = function (cb) {
+ return function () {
var emitter = new events.EventEmitter()
cb.apply(emitter, arguments)
return emitter
}
- }
+ };
+
+ this.coupons = {
+ find: emit(function (familyId, couponId) {
+ httpClient.get('/product_families/' + familyId + '/coupons/' + couponId + '/find.json', '', callback(this));
+ })
+
+ get: emit(function (familyId, couponId) {
+ httpClient.get('/product_families/' + familyId + '/coupons/' + couponId + '.json', '', callback(this));
+ })
+ };
+
this.customers = {
- list:emit(function(opts){
- var path = '/customers.json' + (opts? '?page='+opts.page:'')
- httpClient.get(path, '', callback(this))
+ create: emit(function (customer) {
+ httpClient.post('/customers.json', JSON.stringify(customer), callback(this));
}),
- get:emit(function(id){
- httpClient.get('/customers/'+id+'.json', "", callback(this))
+
+ edit: emit(function (id, customer) {
+ httpClient.put('/customers/' + id + '.json', JSON.stringify(customer), callback(this));
}),
- getByReference:emit(function(ref){
- httpClient.get('/customers/lookup.json?'+querystring.encode({reference:ref}), "", callback(this))
+
+ delete: emit(function (id) {
+ httpClient.delete('/customers/' + id + '.json', '', callback(this));
}),
- create:emit(function(customer){
- httpClient.post('/customers.json', JSON.stringify(customer), callback(this))
+
+ get: emit(function (id) {
+ httpClient.get('/customers/' + id + '.json', '', callback(this));
}),
- edit:emit(function(id, customer){
- httpClient.put('/customers/'+id+'.json', JSON.stringify(customer), callback(this))
+
+ getByReference: emit(function (ref) {
+ httpClient.get('/customers/lookup.json?' + querystring.encode({ reference:ref }), "", callback(this));
}),
- delete:emit(function(id){
- httpClient.delete('/customers/'+id+'.json', '', callback(this))
+
+ list: emit(function (opts) {
+ var path = '/customers.json' + (opts? '?page='+opts.page:'');
+ httpClient.get(path, '', callback(this));
})
- }
+ };
+
this.subscriptions = {
- list:emit(function(opts){
- var path = '/subscriptions.json' + (opts? '?'+querystring.encode(opts):'')
- httpClient.get(path, "", callback(this))
+ create: emit(function (sub) {
+ httpClient.post('/subscriptions.json', JSON.stringify(sub), callback(this));
}),
- listByCustomer:emit(function(customer){
- httpClient.get('/customers/'+customer.id+'/subscriptions.json', "", callback(this))
+
+ get: emit(function (sub) {
+ httpClient.get('/subscriptions/' + sub.id + '.json', '', callback(this));
}),
- create:emit(function(sub){
- httpClient.post('/subscriptions.json', JSON.stringify(sub), callback(this))
+
+ list: emit(function (opts) {
+ var path = '/subscriptions.json' + (opts ? '?' + querystring.encode(opts) : '');
+ httpClient.get(path, '', callback(this));
}),
- get:emit(function(sub){
- httpClient.get('/subscriptions/'+sub.id+'.json', "", callback(this))
+
+ listByCustomer: emit(function (customer) {
+ httpClient.get('/customers/' + customer.id + '/subscriptions.json', '', callback(this));
+ }),
+
+ update: emit(function (sub) {
+ httpClient.post('/subscriptions.json', JSON.stringify(sub), callback(this));
})
- }
+ };
+
this.products = {
- list:emit(function(){
- httpClient.get('/products.json', "", callback(this))
+ get: emit(function (id) {
+ httpClient.get('/products/' + id + '.json', '', callback(this));
+ }),
+
+ getHandle: emit(function (handle) {
+ httpClient.get('/products/handle/' + handle + '.json', '', callback(this));
}),
- get: emit(function(id){
- httpClient.get('/products/'+id+'.json', "", callback(this))
+
+ list: emit(function () {
+ httpClient.get('/products.json', '', callback(this))
}),
- getHandle:emit(function(handle){
- httpClient.get('/products/handle/'+handle+'.json', "", callback(this))
+ };
+
+ this.refunds = {
+ create: emit(function (refund) {
+ httpClient.post('/subscriptions/' + subscription.id + '/refunds.json', JSON.stringify(refund), callback(this));
})
- }
+ };
this.transactions = {
- list:emit(function(params){
+ list: emit(function (params) {
httpClient.get('/transactions.json' + (params? '?'+querystring.encode(params):''), "", callback(this))
}),
- listForSubscription:emit(function(subscription, params){
- httpClient.get('/subscriptions/'+subscription.id+'/transactions.json' + (params? '?'+querystring.encode(params):''), "", callback(this))
+
+ listForSubscription: emit(function (subscription, params) {
+ httpClient.get('/subscriptions/' + subscription.id + '/transactions.json' + (params ? '?' + querystring.encode(params) : ''), '', callback(this));
})
- }
+ };
this.usage = {
- create:emit(function(subscription, component, usage){
- httpClient.post('/subscriptions/'+subscription.id+'/components/'+component.id+'/usages.json', JSON.stringify(usage), callback(this))
+ create: emit(function (subscription, component, usage) {
+ httpClient.post('/subscriptions/' + subscription.id + '/components/' + component.id + '/usages.json', JSON.stringify(usage), callback(this));
})
- }
-}
+ };
+};
function SimpleHttpClient(username, password, host){
- var auth = 'Basic ' + base64.encode(username +':'+ password)
+ var auth = 'Basic ' + base64.encode(username + ':' + password);
var createClient = function(){
return http.createClient(443, host, true)
- }
- var self = this
- ;['delete', 'get', 'post', 'put'].forEach(function(method){
+ };
+
+ var self = this;
+
+ ['delete', 'get', 'post', 'put'].forEach(function(method){
self[method] = function(path, body, cb){
makeRequest(method.toUpperCase(), path, body, cb)
}
- })
+ });
var makeRequest = function(method, path, body, cb) {
var req =createClient().request(method, path, {host:host,
@@ -117,9 +155,8 @@ function SimpleHttpClient(username, password, host){
cb(resp.headers.status, buffer)
})
})
- }
-
-}
+ };
+};
var base64 = { encode: function(str){
return new Buffer(str).toString('base64')
@@ -10,16 +10,16 @@ var client = paynode.createClient({
,password:'test12345'})
vows.describe('module').addBatch({
- 'Given I have tried to create a customer with missing info':{
- topic:function(){
+ 'Given I have tried to create a customer with missing info': {
+ topic: function() {
client.customers.create({}).on('failure', this.callback)
},
- 'it should contain an errors array':function(result, ign){
+ 'it should contain an errors array': function(result, ignore) {
assert.isArray(result.errors)
}
},
- 'Given I have customer data':{
- topic:function(){
+ 'Given I have customer data': {
+ topic: function() {
client.customers.create({
customer:{
first_name:'Joe',
@@ -28,13 +28,12 @@ vows.describe('module').addBatch({
}
}).on('success', this.callback);
},
- 'should get response with same details sent':function(response, ignore){
+ 'should get response with same details sent': function(response) {
assert.equal(response.customer.first_name, 'Joe')
},
- 'should have populated an id':function(response, ignored){
+ 'should have populated an id':function(response) {
assert.isNotNull(response.customer.id)
}
-
},
'listing customers':{
topic:function(){

0 comments on commit e8a2ecb

Please sign in to comment.