content.js | |
---|---|
var _ = require("underscore"); | |
The purpose of the | |
One limitation of the current implementation is that it assumes the | |
The | var Content = function(options) {
this.body = options.body;
this.data = options.data;
this.type = options.type;
};
Content.prototype = { |
Treat
Commented out, but I've forgotten why. :/ | }; |
| Object.defineProperties(Content.prototype,{
|
| type: {
get: function() {
if (this._type) {
return this._type;
} else {
if (this._data) {
switch(typeof this._data) {
case "string": return "text/plain";
case "object": return "application/json";
}
}
}
return "text/plain";
},
set: function(value) {
this._type = value;
return this;
},
enumerable: true
}, |
| data: {
get: function() {
if (this._body) {
return this.processor.parser(this._body);
} else {
return this._data;
}
},
set: function(data) {
if (this._body&&data) Errors.setDataWithBody(this);
this._data = data;
return this;
},
enumerable: true
}, |
| body: {
get: function() {
if (this._data) {
return this.processor.stringify(this._data);
} else {
return this._body;
}
},
set: function(body) {
if (this._data&&body) Errors.setBodyWithData(this);
this._body = body;
return this;
},
enumerable: true
}, |
| processor: {
get: function() {
var processor = Content.processors[this.type];
if (processor) {
return processor;
} else { |
Return the first processor that matches any part of the content type. ex: application/vnd.foobar.baz+json will match json. | processor = _(this.type.split(";")[0]
.split(/\+|\//)).detect(function(type) {
return Content.processors[type];
});
return Content.processors[processor]||
{parser:identity,stringify:toString};
}
},
enumerable: true
}, |
| length: {
get: function() { return this.body.length; }
}
});
Content.processors = {}; |
The | Content.registerProcessor = function(types,processor) {
|
You can pass an array of types that will trigger this processor, or just one. We determine the array via duck-typing here. | if (types.forEach) {
types.forEach(function(type) {
Content.processors[type] = processor;
});
} else { |
If you didn't pass an array, we just use what you pass in. | Content.processors[types] = processor;
}
}; |
Register the identity processor, which is used for text-based media types. | var identity = function(x) { return x; }
, toString = function(x) { return x.toString(); }
Content.registerProcessor(
["text/html","text/plain","text"],
{ parser: identity, stringify: identity }); |
Register the JSON processor, which is used for JSON-based media types. | Content.registerProcessor(
["application/json; charset=utf-8","application/json","json"],
{
parser: function(string) {
return JSON.parse(string);
},
stringify: function(data) {
return JSON.stringify(data); }}); |
Error functions are defined separately here in an attempt to make the code easier to read. | var Errors = {
setDataWithBody: function(object) {
throw new Error("Attempt to set data attribute of a content object " +
"when the body attributes was already set.");
},
setBodyWithData: function(object) {
throw new Error("Attempt to set body attribute of a content object " +
"when the data attributes was already set.");
}
}
module.exports = Content;
|