2014-01-19 13:37:31 +00:00
|
|
|
/**
|
|
|
|
* TODO license
|
|
|
|
* Outputter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputter class.
|
|
|
|
* Abstract (not exported).
|
|
|
|
*/
|
|
|
|
function Outputter() {
|
|
|
|
//
|
|
|
|
// ATTRIBUTES
|
|
|
|
//
|
|
|
|
/**
|
|
|
|
* Output stream.
|
|
|
|
*/
|
|
|
|
this.out = null;
|
|
|
|
};
|
|
|
|
Outputter.prototype = {
|
|
|
|
//
|
|
|
|
// FUNCTIONS
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add header to request.
|
|
|
|
* Available before calling outputTo().
|
|
|
|
*/
|
|
|
|
addHeader: function (name, value) {},
|
2014-01-21 19:25:07 +00:00
|
|
|
/**
|
|
|
|
* Init before outputing.
|
|
|
|
*/
|
|
|
|
init: function () {},
|
2014-01-19 13:37:31 +00:00
|
|
|
/**
|
|
|
|
* Listen action 'data' event.
|
|
|
|
* Receive resource instance to output.
|
|
|
|
*/
|
2014-01-21 19:25:07 +00:00
|
|
|
output: function (resource) { },
|
2014-01-19 13:37:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen action 'end' event.
|
|
|
|
* End of action data transmission.
|
|
|
|
*/
|
|
|
|
end: function() {
|
|
|
|
logger.debug('Action ended');
|
|
|
|
this.out.end();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set target stream and start outputting.
|
|
|
|
*/
|
|
|
|
outputTo: function(stream) {
|
|
|
|
this.out = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputter in json format.
|
|
|
|
*/
|
|
|
|
var JSONOutputter = function() {
|
|
|
|
Outputter.call(this);
|
|
|
|
logger.debug('JSON');
|
|
|
|
};
|
|
|
|
// inherits Outputter
|
|
|
|
JSONOutputter.prototype = Object.create(Outputter.prototype, {
|
2014-01-21 19:25:07 +00:00
|
|
|
output: {
|
|
|
|
value: function(resource) {
|
|
|
|
this.out.write(JSON.stringify(resource));
|
|
|
|
}, enumerable: true, configurable: true, writable: true
|
|
|
|
},
|
|
|
|
init: {
|
|
|
|
value: function() {
|
|
|
|
this.addHeader('Content-Type', 'application/json');
|
|
|
|
}, enumerable: true, configurable: true, writable: true
|
|
|
|
}
|
2014-01-19 13:37:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputter in html.
|
|
|
|
*/
|
|
|
|
var HtmlOutputter = function() {
|
|
|
|
Outputter.call(this);
|
|
|
|
};
|
|
|
|
// inherits Outputter
|
|
|
|
HtmlOutputter.prototype = Object.create(Outputter.prototype, {
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputter in text.
|
|
|
|
*/
|
|
|
|
var TextOutputter = function() {
|
|
|
|
Outputter.call(this);
|
|
|
|
};
|
|
|
|
// inherits Outputter
|
|
|
|
TextOutputter.prototype = Object.create(Outputter.prototype, {
|
|
|
|
});
|
|
|
|
|
|
|
|
//module.exports.JSONOutputter = JSONOutputter;
|
|
|
|
module.exports = {
|
|
|
|
JSONOutputter: JSONOutputter,
|
|
|
|
TextOutputter: TextOutputter,
|
|
|
|
HtmlOutputter: HtmlOutputter
|
|
|
|
};
|