wantworthy.js
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -49,3 +49,3 @@ var API = require("./wantworthy/api").API; | ||
return self.loadSession(account.session.get('token'), callback); | ||
return self.login({secret : account.get("secret")}, callback); | ||
}); | ||
@@ -52,0 +52,0 @@ }; |
@@ -6,4 +6,2 @@ var Wantworthy = require("../wantworthy"), | ||
var self = this; | ||
this.attributes = attrs; | ||
}; | ||
@@ -71,21 +69,22 @@ | ||
return function parser(res) { | ||
try { | ||
if(res.ok) { | ||
if(res.header['content-type'] === 'text/plain') return callback(null); | ||
var parser = function(x) { return x; }; | ||
return callback(null, self.new(JSON.parse(res.text) )); | ||
} else if(res.unauthorized) { | ||
var error = new Error(res.text); | ||
error.statusCode = res.status; | ||
return callback(error); | ||
} else if(res.header['content-type'] === 'text/plain'){ | ||
var error = new Error(res.text) | ||
error.statusCode = res.status; | ||
return callback(error); | ||
} else { | ||
return callback(JSON.parse(res.text)); | ||
if(res.header['content-type'] ) { | ||
var type = res.header['content-type']; | ||
var content = type.split(";")[0].split(/\+|\//); | ||
if(content && ~content.indexOf('json')){ | ||
parser = JSON.parse; | ||
} | ||
} catch(err){ | ||
return callback(err); | ||
} | ||
if(res.ok) { | ||
if(res.header['content-type'] === 'text/plain') return callback(null, parser(res.text)); | ||
return callback(null, self.new(parser(res.text) )); | ||
} else { | ||
var error = new Error(parser(res.text)); | ||
error.statusCode = res.status; | ||
return callback(error); | ||
} | ||
} | ||
@@ -128,2 +127,26 @@ }; | ||
Resource.prototype.save = function(callback) { | ||
if(this.isNew()) { | ||
this.constructor.create(this.toJSON(), callback); | ||
} else { | ||
this.update(this.toJSON(), callback); | ||
} | ||
}; | ||
Resource.prototype.destroy = function(callback) { | ||
if(this.isNew()) { | ||
return callback(); // nothing to delete on the server | ||
} else { | ||
var r = this.constructor._request | ||
.del(this.url()) | ||
.set(Resource.auth()); | ||
if(this.constructor.withCredentials['update']) { | ||
Resource.acceptCookiesFor(r); | ||
} | ||
r.end(this.constructor.parseResponse(callback)); | ||
}; | ||
}; | ||
Resource.prototype.update = function(attrs, callback) { | ||
@@ -142,2 +165,6 @@ var r = this.constructor._request | ||
Resource.prototype.isNew = function() { | ||
return !this.links || !this.links.self; | ||
}; | ||
// var Want = require("./lib/wantworthy"); | ||
@@ -144,0 +171,0 @@ // var w = new Want({url: "http://api.dev.wantworthy.com:9000"}); |
@@ -13,2 +13,5 @@ var _ = require('underscore'); | ||
self.links = {}; | ||
self.attributes = {}; | ||
if(attrs && attrs._embedded) { | ||
@@ -35,2 +38,4 @@ Object.keys(attrs._embedded).forEach(function(resourceName){ | ||
if(attrs) self.attributes = attrs; | ||
resourceful.Resource.call(this, attrs); | ||
@@ -37,0 +42,0 @@ }; |
{ | ||
"name": "wantworthy.js", | ||
"description": "Javascript client for working with the wantworthy.com api. Runs in node and the browser", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"author": "Ryan Fitzgerald <ryan@codebrewstudios.com>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -78,2 +78,44 @@ var helper = require("./test-helper").TestHelper, | ||
}); | ||
describe("is new", function() { | ||
it("should return true", function() { | ||
var p = new Product({name : "foo"}); | ||
p.isNew().should.be.true; | ||
}); | ||
it("should return false", function() { | ||
var p = new Product({name : "foo"}); | ||
p.links.self = {href : "http://api.wantworthy.com/products/123"}; | ||
p.isNew().should.be.false; | ||
}); | ||
}); | ||
describe("save", function() { | ||
it("should create new resource", function(done) { | ||
var p = new Product({name : "foo"}); | ||
apiServer.post('/products', p.toJSON()).reply(200, helper.amazonProduct, {'content-type': Product.schema.mediaType}); | ||
p.save(function(err, prod){ | ||
prod.get('id').should.equal(helper.amazonProduct.id); | ||
done(); | ||
}); | ||
}); | ||
it("should update existing product", function(done) { | ||
var p = new Product({name : "existing"}); | ||
p.links.self = {href : Product.url() + "/12345"}; | ||
apiServer.put("/products/12345", p.toJSON()).reply(200, helper.amazonProduct, {'content-type': Product.schema.mediaType}); | ||
p.save(function(err, prod){ | ||
prod.get('id').should.equal(helper.amazonProduct.id); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -80,0 +122,0 @@ }); |
Sorry, the diff of this file is too big to display
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
176067
5260