Implements ShowAction, Library

This commit is contained in:
Meutel 2014-01-21 20:25:07 +01:00
parent da0a827e7e
commit a8b13820f1
6 changed files with 142 additions and 11 deletions

View File

@ -3,5 +3,6 @@
"debugLevel":"debug",
"endpoints": {
"library":"endpoint/library.js"
}
},
"urlPrefix": "http://127.0.0.1:8080/bouquins"
}

View File

@ -59,8 +59,7 @@ Action.prototype = Object.create(EventEmitter.prototype, {
value: function(callback) {
logger.debug('prepare Action');
// TODO impl per action type
// default: OK, no headers
// default: OK, no headers
callback(null, 200, {});
},
enumerable: true,
@ -89,7 +88,90 @@ function ShowAction() {
Action.call(this, 'show');
};
// inherits Action
ShowAction.prototype = Object.create(Action.prototype);
ShowAction.prototype = Object.create(Action.prototype, {
/**
* Resource id.
*/
resId: { value: null, enumerable: true, configurable: true, writable: true },
/**
* Resource.
*/
res: { value: null, enumerable: true, configurable: true, writable: true },
/**
* Prepare show action.
* Headers:
* - Link: related resource.
*/
prepare: {
value: function(callback) {
var self = this;
if (this.resId) {
this.loadResource(this.resId, function(err, res) {
self.res = res;
//TODO err
var link = '';
var rels = self.getRelated(res); // { type: 'author', path: '/author/id'}
logger.debug(rels);
rels.forEach(function (e) {
if (link.length > 0) link+=', ';
link += '<' + config.urlPrefix + e.path + '>; rel=' + e.type;
});
var headers = {};
if (link.length > 0) {
headers.Link = link;
}
callback(null, 200, headers);
});
} else {
//TODO error?
callback(null, '5OO', {});
}
},
enumerable: true,
configurable: true,
writable: true
},
/**
* Load resource.
* @param resource id
* @param callback <function(err, resource)>
*/
loadResource: {
value: function(resId, callback){
//implement
callback(new Error('Cannot load resource ' + resId));
},
enumerable: true,
configurable: true,
writable: true
},
/**
* Get related resources.
* @param res resource
* @return related resources { type: 'author', path: '/author/id'}
*/
getRelated: {
value: function(res){
return [];
},
enumerable: true,
configurable: true,
writable: true
},
doAction: {
value: function(){
logger.debug('doAction');
this.emit('data', this.res);
this.emit('end');
},
enumerable: true,
configurable: true,
writable: true
}
});
module.exports = {
ShowAction: ShowAction

View File

@ -34,6 +34,9 @@ Endpoint.prototype = {
}
},
/**
* @return target is a collection of resources.
*/
targetCollection : function(pathname) {
// TODO
return false;
@ -43,7 +46,7 @@ Endpoint.prototype = {
* Bind action to endpoint resource.
*/
bind : function(action, callback) {
//TODO
// implemented
callback(null, action);
}
};

View File

@ -6,5 +6,39 @@ function Library() {
Endpoint.call(this);
}
Library.prototype = Object.create(Endpoint.prototype, {
bind: {
value: function(action, callback) {
if (action.name == 'show') {
action.resId = 'library';
action.loadResource = function(resId, callback) {
//TODO load from db
callback(null, {
name: 'Bibliothèque Meutel'
});
};
action.getRelated = function(res){
return new Array(
// authors list
{ type: 'authors', path: '/author/' },
// books list
{ type: 'books', path: '/books/' },
// tags list
{ type: 'tags', path: '/tags/' },
// series list
{ type: 'series', path: '/series/' },
// user favorites
{ type: 'favorites', path:'/favorites/'}
);
};
}
callback(null, action);
},
enumerable: true,
configurable: true,
writable: true
},
});
exports = module.exports = new Library();

View File

@ -26,14 +26,15 @@ Outputter.prototype = {
* Available before calling outputTo().
*/
addHeader: function (name, value) {},
/**
* Init before outputing.
*/
init: function () {},
/**
* Listen action 'data' event.
* Receive resource instance to output.
*/
output: function (resource) {
//TODO
},
output: function (resource) { },
/**
* Listen action 'end' event.
@ -41,7 +42,6 @@ Outputter.prototype = {
*/
end: function() {
logger.debug('Action ended');
//TODO
this.out.end();
},
@ -63,6 +63,16 @@ var JSONOutputter = function() {
};
// inherits Outputter
JSONOutputter.prototype = Object.create(Outputter.prototype, {
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
}
});
/**

View File

@ -104,7 +104,7 @@ Router.prototype = {
// allow outputter to set headers
outputter.addHeader = function(name, value) {
if (resp.headersSent) {
logger.warn('Header already sent, ignoring: ' + name);
logger.error('Header already sent, ignoring: ' + name);
} else {
resp.setHeader(name, value);
}
@ -117,6 +117,7 @@ Router.prototype = {
}
// when request data received, prepare action, send headers, exec action
req.on('end', function() {
outputter.init();
action.prepare(function(err, statusCode, headers){
//TODO err