Comparing version
@@ -5,3 +5,3 @@ // Generated by CoffeeScript 1.6.3 | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
__slice = [].slice; | ||
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
@@ -25,2 +25,3 @@ querystring = require('querystring'); | ||
} | ||
this.post = __bind(this.post, this); | ||
this.get = __bind(this.get, this); | ||
@@ -85,3 +86,3 @@ this._request = __bind(this._request, this); | ||
_Class.prototype._request = function(method, args, fn) { | ||
_Class.prototype._request = function(method, kwargs, fn) { | ||
var req, request_options; | ||
@@ -95,5 +96,8 @@ request_options = { | ||
}; | ||
if (args[0]) { | ||
request_options.url += '?' + querystring.stringify(args[0]); | ||
if (kwargs.args != null) { | ||
request_options.url += '?' + querystring.stringify(kwargs.args); | ||
} | ||
if (kwargs.data != null) { | ||
request_options.form = kwargs.data; | ||
} | ||
debug("" + method, request_options.url); | ||
@@ -105,6 +109,15 @@ return req = request(request_options, fn); | ||
_Class.prototype.get = function() { | ||
var args, fn, handle, resp, _i, | ||
_Class.prototype.get = function(kwargs, fn) { | ||
var handle, resp, | ||
_this = this; | ||
args = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), fn = arguments[_i++]; | ||
if ('function' === typeof kwargs) { | ||
fn = kwargs; | ||
kwargs = {}; | ||
} else { | ||
if (kwargs.args == null) { | ||
kwargs = { | ||
args: kwargs | ||
}; | ||
} | ||
} | ||
handle = function(err, response, body) { | ||
@@ -118,5 +131,23 @@ var _ref1; | ||
}; | ||
return resp = this._request('GET', args, handle); | ||
return resp = this._request('GET', kwargs, handle); | ||
}; | ||
_Class.prototype.post = function(kwargs, fn) { | ||
var handle, resp, | ||
_this = this; | ||
if (__indexOf.call(kwargs, 'args') < 0) { | ||
kwargs = { | ||
data: kwargs | ||
}; | ||
} | ||
handle = function(err, response, body) { | ||
var _ref1; | ||
if ((200 <= (_ref1 = response.statusCode) && _ref1 <= 299)) { | ||
return fn(err, _this._try_to_serialize(response, body)); | ||
} | ||
return fn(true); | ||
}; | ||
return resp = this._request('POST', kwargs, handle); | ||
}; | ||
return _Class; | ||
@@ -123,0 +154,0 @@ |
{ | ||
"name": "slumber", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "Port of Python's slumber library -- A library that makes consuming a RESTful API easier and more convenient", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var CUSTOMERS, app, assert, base_url, express, freeport, querystring, slumber, url; | ||
var CUSTOMERS, app, assert, base_url, connect, express, freeport, querystring, slumber, url; | ||
@@ -13,2 +13,4 @@ querystring = require('querystring'); | ||
connect = require('connect'); | ||
freeport = require('freeport'); | ||
@@ -40,2 +42,6 @@ | ||
app.use(connect.urlencoded()); | ||
app.use(connect.json()); | ||
app.get('/', function(req, res) { | ||
@@ -67,2 +73,8 @@ return res.end('Hello World !'); | ||
app.post('/customers', function(req, res) { | ||
return res.json({ | ||
'parsed-body': req.body | ||
}); | ||
}); | ||
app.get('/customers-yml', function(req, res) { | ||
@@ -279,2 +291,16 @@ var yamljs; | ||
}); | ||
it('should post data', function(done) { | ||
return api('customers').post({ | ||
'user': 'Mickael', | ||
'age': 42, | ||
'gender': 'male' | ||
}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal('object', typeof ret); | ||
assert.equal(ret['parsed-body'].user, 'Mickael'); | ||
assert.equal(ret['parsed-body'].age, 42); | ||
assert.equal(ret['parsed-body'].gender, 'male'); | ||
return done(); | ||
}); | ||
}); | ||
it('should return customer object (from json) with id = 1', function(done) { | ||
@@ -297,2 +323,13 @@ return api('customers')(1).get(function(err, ret) { | ||
}); | ||
it('should return an array (from json) of customers for gender=male explicitely defining args', function(done) { | ||
return api('customers').get({ | ||
'args': { | ||
'gender': 'male' | ||
} | ||
}, function(err, ret) { | ||
assert.equal(err, null); | ||
assert.equal(ret.length, 2); | ||
return done(); | ||
}); | ||
}); | ||
it('should not detect yaml content-type and return an object', function(done) { | ||
@@ -299,0 +336,0 @@ return api('customers-yml').get(function(err, ret) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
44528
7.79%729
9.46%