hmpo-model
Advanced tools
Comparing version 0.1.1 to 0.1.2
233
index.js
@@ -1,232 +0,1 @@ | ||
var _ = require('underscore'), | ||
http = require('http'), | ||
https = require('https'), | ||
url = require('url'), | ||
concat = require('concat-stream'), | ||
util = require('util'), | ||
EventEmitter = require('events').EventEmitter; | ||
var Model = function (attributes, options) { | ||
this.options = options || {}; | ||
this.attributes = {}; | ||
this.set(attributes, { silent: true }); | ||
}; | ||
util.inherits(Model, EventEmitter); | ||
_.extend(Model.prototype, { | ||
save: function (options, callback) { | ||
if (typeof options === 'function' && arguments.length === 1) { | ||
callback = options; | ||
options = {}; | ||
} | ||
this.prepare(function (err, data) { | ||
if (err) { return callback(err); } | ||
data = JSON.stringify(data); | ||
var reqConf = url.parse(this.url(options)); | ||
reqConf.method = options.method || 'POST'; | ||
reqConf.headers = { | ||
'Content-Type': 'application/json', | ||
'Content-Length': data.length | ||
}; | ||
reqConf.data = data; | ||
this.request(reqConf, callback); | ||
}.bind(this)); | ||
}, | ||
fetch: function (options, callback) { | ||
if (typeof options === 'function' && arguments.length === 1) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var reqConf = url.parse(this.url(options)); | ||
reqConf.method = options.method || 'GET'; | ||
this.request(reqConf, callback); | ||
}, | ||
delete: function (options, callback) { | ||
if (typeof options === 'function' && arguments.length === 1) { | ||
callback = options; | ||
options = {}; | ||
} | ||
var reqConf = url.parse(this.url(options)); | ||
reqConf.method = options.method || 'DELETE'; | ||
this.request(reqConf, callback); | ||
}, | ||
request: function (settings, callback) { | ||
var protocol = (settings.protocol === 'http:') ? http : https; | ||
settings.auth = this.auth(); | ||
var _callback = function (err, data, statusCode) { | ||
if (err) { | ||
this.emit('fail', err, data, settings, statusCode); | ||
} else { | ||
this.emit('success', data, settings, statusCode); | ||
} | ||
callback(err, data); | ||
}.bind(this); | ||
var request = protocol.request(settings, function (response) { | ||
this.handleResponse(response, settings, _callback); | ||
}.bind(this)); | ||
request.on('error', function(e) { | ||
_callback(e); | ||
}); | ||
this.emit('sync', settings); | ||
if (settings.data) { | ||
request.write(settings.data); | ||
} | ||
request.end(); | ||
}, | ||
handleResponse: function(response, settings, callback) { | ||
response.pipe(concat(function (d) { | ||
var data = {}; | ||
try { | ||
data = JSON.parse(d.toString() || '{}'); | ||
} catch (e) { | ||
return callback(new Error('Invalid JSON response')); | ||
} | ||
if (response.statusCode < 400) { | ||
try { | ||
data = this.parse(data); | ||
callback(null, data, response.statusCode); | ||
} catch (e) { | ||
callback(e, null, response.statusCode); | ||
} | ||
} else { | ||
callback(this.parseError(response.statusCode, data), data, response.statusCode); | ||
} | ||
}.bind(this))); | ||
}, | ||
prepare: function (callback) { | ||
callback(null, this.toJSON()); | ||
}, | ||
parse: function (data) { | ||
return data; | ||
}, | ||
parseError: function (statusCode, data) { | ||
return _.extend({ status: statusCode }, data); | ||
}, | ||
get: function (key) { | ||
return this.attributes[key]; | ||
}, | ||
set: function (key, value, options) { | ||
var attrs = {}; | ||
if (typeof key === 'string') { | ||
attrs[key] = value; | ||
} else { | ||
attrs = key; | ||
options = value; | ||
} | ||
options = options || {}; | ||
var old = this.toJSON(), | ||
changed = {}; | ||
_.each(attrs, function (value, key) { | ||
if (value !== old[key]) { | ||
changed[key] = value; | ||
} | ||
}); | ||
_.extend(this.attributes, attrs); | ||
if (!options.silent && !_.isEmpty(changed)) { | ||
_.each(changed, function (value, key) { | ||
this.emit('change:' + key, this.get(key), old[key]); | ||
}, this); | ||
this.emit('change', changed); | ||
} | ||
return this; | ||
}, | ||
unset: function (fields, options) { | ||
options = options || {}; | ||
if (typeof fields === 'string') { | ||
fields = [fields]; | ||
} | ||
var old = this.toJSON(), | ||
changed = {}; | ||
_.each(fields, function (key) { | ||
if (old[key] !== undefined) { | ||
changed[key] = undefined; | ||
delete this.attributes[key]; | ||
} | ||
}, this); | ||
if (!options.silent && !_.isEmpty(changed)) { | ||
_.each(changed, function (value, key) { | ||
this.emit('change:' + key, undefined, old[key]); | ||
}, this); | ||
this.emit('change', changed); | ||
} | ||
return this; | ||
}, | ||
increment: function (property, amount) { | ||
if (!property || typeof property !== 'string') { | ||
throw new Error('Trying to increment undefined property'); | ||
} | ||
var val = this.get(property) || 0; | ||
amount = amount || 1; | ||
this.set(property, val + amount); | ||
}, | ||
reset: function (options) { | ||
options = options || {}; | ||
var keys = Object.keys(this.attributes); | ||
this.attributes = {}; | ||
if (!options.silent) { | ||
_.each(keys, function (key) { | ||
this.emit('change:' + key, undefined); | ||
}, this); | ||
this.emit('reset'); | ||
} | ||
}, | ||
url: function (options) { | ||
options = options || {}; | ||
var opts = {}; | ||
if (options.url) { | ||
opts = url.parse(options.url); | ||
} | ||
// passing a host to url.format overrides other options, so remove it | ||
delete opts.host; | ||
_.extend(opts, options); | ||
return url.format(opts); | ||
}, | ||
auth: function () { | ||
return; | ||
}, | ||
toJSON: function () { | ||
return _.clone(this.attributes); | ||
} | ||
}); | ||
module.exports = Model; | ||
module.exports = require('./lib/model'); |
{ | ||
"name": "hmpo-model", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Simple model for interacting with http/rest apis.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "eslint ./index.js && mocha test/spec/ --recursive --require test/helper.js" | ||
"test": "eslint ./lib && mocha test/spec/ --recursive --require test/helper.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -21,3 +21,3 @@ # hmpo-model | ||
### `save | ||
### `save` | ||
@@ -24,0 +24,0 @@ ```javascript |
@@ -220,3 +220,3 @@ var http = require('http'), | ||
err.should.be.an.instanceOf(Error); | ||
expect(data).to.be.undefined; | ||
expect(data).to.be.null; | ||
done(); | ||
@@ -406,3 +406,3 @@ }); | ||
err.should.be.an.instanceOf(Error); | ||
expect(data).to.be.undefined; | ||
expect(data).to.be.null; | ||
done(); | ||
@@ -583,3 +583,3 @@ }); | ||
err.should.be.an.instanceOf(Error); | ||
expect(data).to.be.undefined; | ||
expect(data).to.be.null; | ||
done(); | ||
@@ -586,0 +586,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43946
10
998