resource.js | |
---|---|
var underscore = require('underscore'),
oauthhelper = require('./oauth-helper'),
helpers = require('./helpers'),
ResponseParser = require('./responseparser'),
Request = require('./request'); | |
A resource on the API, this class will build up a method for each action that can be performed on the resource. The
| function Resource(options) {
this.format = options.format;
this.logger = options.logger;
this.resourceName = options.resourceDefinition.resource;
this.host = options.schema.host;
this.version = options.schema.version;
this.consumerkey = options.consumerkey;
this.consumersecret = options.consumersecret;
this.logger.info('Creating constructor for resource: ' +
this.resourceName);
underscore.each(options.resourceDefinition.actions,
function processAction(action) {
this.createAction(action, this.request);
}, this);
} |
Figure out the appropriate method name for an action on a resource on the API
| Resource.prototype.chooseMethodName = function (actionDefinition) {
var fnName; |
Default the action name to getXXX if we only have the URL slug as the action definition. | if (underscore.isString(actionDefinition)) {
fnName = 'get' + helpers.capitalize(actionDefinition);
} else {
fnName = actionDefinition.methodName;
}
return fnName;
}; |
Utility method for creating the necessary methods on the Resource for dispatching the request to the 7digital API.
| Resource.prototype.createAction = function (actionDefinition) {
var fnName = this.chooseMethodName(actionDefinition),
action = actionDefinition.apiCall || actionDefinition,
httpMethod = (actionDefinition.method || 'GET'),
url;
this.logger.info('Creating method: ' + fnName + ' for ' + action +
' action with ' + httpMethod + ' HTTP verb');
this[fnName] = function (params, callback) {
var parser = new ResponseParser({
format: this.format,
logger: this.logger
}),
request = new Request({
host: this.host,
version: this.version,
consumerkey: this.consumerkey,
consumersecret: this.consumersecret,
logger: this.logger,
format: this.format,
authtype: actionDefinition.oauth
});
console.log('token: ' + params.accesstoken + ' secret: ' +
params.accesssecret); |
Dispatch the request to the API binding the request parser to the callback when the response returns. | url = request.formatPath(this.resourceName, action);
request[httpMethod.toLowerCase()](url, params,
underscore.bind(parser.parse, parser, callback));
};
};
module.exports = Resource;
|