adds npr one api module

This commit is contained in:
Todd Treece 2015-08-01 20:05:28 -04:00
parent 7beb3d1525
commit 8d77acd4cf

66
lib/one.js Normal file
View file

@ -0,0 +1,66 @@
/************************ DEPENDENCIES *****************************/
var util = require('util'),
bunyan = require('bunyan'),
helpers = require('./helpers'),
swagger = require('swagger-client');
var proto = One.prototype;
exports = module.exports = One;
/************************* CONSTRUCTOR ****************************/
function One(config) {
if (! (this instanceof One))
return new One(config);
util._extend(this, config || {});
if(! this.credentials)
throw('Missing API credentials');
if(! this.log)
this.log = bunyan.createLogger({name: 'npr-one'});
this.credentials.grant_type = 'password';
this.getAccessToken = this.getAccessToken.bind(this);
}
/*************************** DEFAULTS *****************************/
proto.credentials = false;
proto.client = false;
proto.swagger_url = 'https://api.npr.org/documentation/beryllium/api-docs';
proto.log = false;
proto.init = function() {
var self = this;
return new Promise(function(resolve, reject) {
self.client = new swagger({
url: self.swagger_url,
success: function() {
helpers.processSwagger(self);
resolve(self);
},
failure: function() {
reject('swagger init failed');
}
});
});
};
proto.getAccessToken = function() {
var self = this;
if(! this.access_token)
return this.authorization.createToken(this.credentials);
return new Promise(function(resolve, reject) {
resolve(self.access_token);
});
};