From 8d77acd4cf5a14042f2dd04c9693a19560033973 Mon Sep 17 00:00:00 2001 From: Todd Treece Date: Sat, 1 Aug 2015 20:05:28 -0400 Subject: [PATCH] adds npr one api module --- lib/one.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/one.js diff --git a/lib/one.js b/lib/one.js new file mode 100644 index 0000000..371f388 --- /dev/null +++ b/lib/one.js @@ -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); + }); + +};