bouquins/lib/endpoint/endpoint.js
Meutel a609d80de3 endpoint book
refactoring showaction.getRelated: async
2014-01-22 22:08:21 +01:00

57 lines
1.3 KiB
JavaScript

/**
* Endpoint class.
*/
var Action = require('../action/action.js');
function Endpoint() {
// constructor
};
Endpoint.prototype = {
/**
* Build an action on resource endpoint.
* @param method <string> HTTP method
* @param url <url> target URL
* @param callback <function(err, action)> callback
*/
buildAction : function(method, url, callback) {
var col = this.targetCollection(url.pathname);
logger.debug('Building action ('+method+','+col+')');
var action;
if (col && method == 'POST') {
//TODO search
} else if (col && method == 'GET') {
action = new Action.ListAction();
action.page = url.query.page;
action.perPage = url.query.per_page;
} else if (!col && method == 'POST') {
//TODO edit
} else if (!col && method == 'GET') {
action = new Action.ShowAction();
}
if (action) {
this.bind(action, url.query, function(err) {
callback(err, action);
});
} else {
callback(new Error('no action'));
}
},
/**
* @return target is a collection of resources.
*/
targetCollection : function(pathname) {
// TODO
return false;
},
/**
* Bind action to endpoint resource.
*/
bind : function(action, params, callback) {
// implemented
callback(null, action);
}
};
exports = module.exports = Endpoint;