Comparing version
133
lib/API.js
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
var API, Serializer, append_slash, callable, debug, querystring, request, _ref, | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | ||
__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; }; | ||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | ||
@@ -19,3 +18,3 @@ querystring = require('querystring'); | ||
function _Class(base_url, opts, fn) { | ||
var _base, _base1, _base2, _base3; | ||
var _base, _base1, _base2, _base3, _base4; | ||
this.opts = opts != null ? opts : {}; | ||
@@ -30,6 +29,6 @@ if (fn == null) { | ||
this.get = __bind(this.get, this); | ||
this._prepare_opts = __bind(this._prepare_opts, this); | ||
this._request = __bind(this._request, this); | ||
this._try_to_serialize = __bind(this._try_to_serialize, this); | ||
this._create_child = __bind(this._create_child, this); | ||
debug("constructor base_url=" + base_url); | ||
if (base_url != null) { | ||
@@ -44,6 +43,11 @@ this.opts.base_url = base_url; | ||
} | ||
if ((_base2 = this.opts).format == null) { | ||
_base2.format = 'json'; | ||
if ((_base2 = this.opts).request_opts == null) { | ||
_base2.request_opts = { | ||
rejectUnauthorized: false | ||
}; | ||
} | ||
this.serializer = (_base3 = this.opts).serializer != null ? (_base3 = this.opts).serializer : _base3.serializer = new Serializer(this.opts.format); | ||
if ((_base3 = this.opts).format == null) { | ||
_base3.format = 'json'; | ||
} | ||
this.serializer = (_base4 = this.opts).serializer != null ? (_base4 = this.opts).serializer : _base4.serializer = new Serializer(this.opts.format); | ||
if (this.opts.append_slash) { | ||
@@ -88,3 +92,3 @@ this.opts.base_url = append_slash(this.opts.base_url); | ||
_Class.prototype._request = function(method, kwargs, fn) { | ||
var req, request_options; | ||
var key, req, request_options, value, _ref1; | ||
request_options = { | ||
@@ -97,2 +101,9 @@ url: this.base_url, | ||
}; | ||
_ref1 = this.opts.request_opts; | ||
for (key in _ref1) { | ||
value = _ref1[key]; | ||
if (request_options[key] == null) { | ||
request_options[key] = value; | ||
} | ||
} | ||
if (kwargs.args != null) { | ||
@@ -117,15 +128,40 @@ request_options.url += '?' + querystring.stringify(kwargs.args); | ||
_Class.prototype.get = function(kwargs, fn) { | ||
var handle, resp, | ||
_this = this; | ||
if ('function' === typeof kwargs) { | ||
fn = kwargs; | ||
kwargs = {}; | ||
} else { | ||
if (kwargs.args == null) { | ||
kwargs = { | ||
args: kwargs | ||
}; | ||
_Class.prototype._prepare_opts = function(from, default_dest) { | ||
var k, key, section, to, translation, v, value; | ||
if (default_dest == null) { | ||
default_dest = 'args'; | ||
} | ||
to = { | ||
args: {}, | ||
data: {} | ||
}; | ||
translation = { | ||
query: 'args' | ||
}; | ||
for (key in from) { | ||
value = from[key]; | ||
if (key.slice(0, 2) === '__') { | ||
section = key.slice(2); | ||
if (translation[section] != null) { | ||
section = translation[section]; | ||
} | ||
for (k in value) { | ||
v = value[k]; | ||
to[section][k] = v; | ||
} | ||
} else { | ||
to[default_dest][key] = value; | ||
} | ||
} | ||
return to; | ||
}; | ||
_Class.prototype.get = function(query, fn) { | ||
var handle, opts, resp, | ||
_this = this; | ||
if ('function' === typeof query) { | ||
fn = query; | ||
query = {}; | ||
} | ||
opts = this._prepare_opts(query, 'args'); | ||
handle = function(err, response, body) { | ||
@@ -136,21 +172,16 @@ var _ref1; | ||
} else { | ||
return fn(true); | ||
return fn(true, response.statusCode); | ||
} | ||
}; | ||
return resp = this._request('GET', kwargs, handle); | ||
return resp = this._request('GET', opts, handle); | ||
}; | ||
_Class.prototype["delete"] = function(kwargs, fn) { | ||
var handle, resp, | ||
_Class.prototype["delete"] = function(query, fn) { | ||
var handle, opts, resp, | ||
_this = this; | ||
if ('function' === typeof kwargs) { | ||
fn = kwargs; | ||
kwargs = {}; | ||
} else { | ||
if (kwargs.args == null) { | ||
kwargs = { | ||
args: kwargs | ||
}; | ||
} | ||
if ('function' === typeof query) { | ||
fn = query; | ||
query = {}; | ||
} | ||
opts = this._prepare_opts(query, 'args'); | ||
handle = function(err, response, body) { | ||
@@ -168,13 +199,9 @@ var _ref1; | ||
}; | ||
return resp = this._request('DELETE', kwargs, handle); | ||
return resp = this._request('DELETE', opts, handle); | ||
}; | ||
_Class.prototype.post = function(kwargs, fn) { | ||
var handle, resp, | ||
_Class.prototype.post = function(data, fn) { | ||
var handle, opts, resp, | ||
_this = this; | ||
if (__indexOf.call(kwargs, 'args') < 0) { | ||
kwargs = { | ||
data: kwargs | ||
}; | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = function(err, response, body) { | ||
@@ -187,13 +214,9 @@ var _ref1; | ||
}; | ||
return resp = this._request('POST', kwargs, handle); | ||
return resp = this._request('POST', opts, handle); | ||
}; | ||
_Class.prototype.put = function(kwargs, fn) { | ||
var handle, resp, | ||
_Class.prototype.put = function(data, fn) { | ||
var handle, opts, resp, | ||
_this = this; | ||
if (__indexOf.call(kwargs, 'args') < 0) { | ||
kwargs = { | ||
data: kwargs | ||
}; | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = function(err, response, body) { | ||
@@ -206,13 +229,9 @@ var _ref1; | ||
}; | ||
return resp = this._request('PUT', kwargs, handle); | ||
return resp = this._request('PUT', opts, handle); | ||
}; | ||
_Class.prototype.patch = function(kwargs, fn) { | ||
var handle, resp, | ||
_Class.prototype.patch = function(data, fn) { | ||
var handle, opts, resp, | ||
_this = this; | ||
if (__indexOf.call(kwargs, 'args') < 0) { | ||
kwargs = { | ||
data: kwargs | ||
}; | ||
} | ||
opts = this._prepare_opts(data, 'data'); | ||
handle = function(err, response, body) { | ||
@@ -225,3 +244,3 @@ var _ref1; | ||
}; | ||
return resp = this._request('PATCH', kwargs, handle); | ||
return resp = this._request('PATCH', opts, handle); | ||
}; | ||
@@ -228,0 +247,0 @@ |
{ | ||
"name": "slumber", | ||
"version": "0.2.2", | ||
"version": "0.3.1", | ||
"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", |
@@ -397,3 +397,3 @@ // Generated by CoffeeScript 1.6.3 | ||
return api('customers').get({ | ||
'args': { | ||
'__args': { | ||
'gender': 'male' | ||
@@ -400,0 +400,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53040
2.18%888
2.07%