response.js | |
---|---|
The | var _ = require("underscore")
, Content = require("./content")
, HeaderMixins = require("./mixins/headers")
; |
Construct a | var Response = function(raw, request, callback) {
var response = this;
this._raw = raw; |
The | this._setHeaders.call(this,raw.headers);
this.request = request;
this.client = request.client;
this.log = this.request.log; |
Stream the response content entity and fire the callback when we're done. | var body = "";
raw.on("data", function(data) { body += data; });
raw.on("end", function() {
response._body = new Content({
body: body,
type: response.getHeader("Content-Type")
});
callback(response);
});
}; |
The | Response.prototype = {
inspect: function() {
var response = this;
var headers = _(response.headers).reduce(function(array,value,key){
array.push("\t" + key + ": " + value); return array;
},[]).join("\n");
var summary = ["<Shred Response> ", response.status].join(" ")
return [ summary, "- Headers:", headers].join("\n");
}
}; |
| Object.defineProperties(Response.prototype, {
|
| status: {
get: function() { return this._raw.statusCode; },
enumerable: true
}, |
| body: {
get: function() { return this._body; }
},
content: {
get: function() { return this.body; },
enumerable: true
}, |
| isRedirect: {
get: function() {
return (this.status>299
&&this.status<400
&&this.getHeader("Location"));
},
enumerable: true
}, |
| isError: {
get: function() {
return (this.status>399)
},
enumerable: true
}
}); |
Add in the getters for accessing the normalized headers. | HeaderMixins.getters(Response);
HeaderMixins.privateSetters(Response);
module.exports = Response;
|