From ee3ce638b7ebd9a53bc1fa8c7c60339a515f7ded Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Sat, 7 Sep 2013 21:01:54 -0500 Subject: [PATCH] Refactored how endpoints are defined, added mapObj utility --- index.js | 25 +++++++++++++++---------- oauth.js | 2 +- test/getLoginUrl.js | 1 + util/mapObj.js | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 util/mapObj.js diff --git a/index.js b/index.js index f45360a..75e8558 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ */ var _ = require('lodash'), + mapObj = require('./util/mapObj'), Request = require('./request'), OAuth = require('./oauth'), affordances = require('./affordances'); @@ -29,14 +30,18 @@ function DZ (options) { // (use case: e.g. future API versions) - // Main API endpoint URL - // Pulled from http://developers.deezer.com/api - this.apiEndpointUrl = 'https://api.deezer.com'; + // Pulled from http://developers.deezer.com/api and http://developers.deezer.com/api/oauth + this.endpoints = { + // Base URL for accessing core Deezer resources as a particular user + resources: 'https://api.deezer.com', - // Main authentication endpoint for OAuth - // Pulled from http://developers.deezer.com/api/oauth - this.authenticationUrl = 'https://connect.deezer.com/oauth/auth.php'; + // URL where user should be redirected to allow this app's access to the Deezer API + userAuth: 'https://connect.deezer.com/oauth/auth.php', + + // URL where the access token for a user can be retrieved + accessToken: 'https://connect.deezer.com/oauth/access_token.php/' + }; // Mixin OAuth methods @@ -52,10 +57,10 @@ function DZ (options) { */ // Ensure that both URLs have no trailing slash - this.apiEndpointUrl.replace(/\/$/, ''); - this.authenticationUrl.replace(/\/$/, ''); - - + this.endpoints = mapObj(this.endpoints, function (url) { + return url.replace(/\/$/, ''); + }); + /** * Apply usage affordances diff --git a/oauth.js b/oauth.js index a323490..dd20a46 100644 --- a/oauth.js +++ b/oauth.js @@ -52,7 +52,7 @@ module.exports = { // to verify that the user whose account you'd like to access // is on board and cool w/ it and everything // Then return the ready-to-go URL: - return this.authenticationUrl + + return this.endpoints.userAuth + '?' + querystringify({ app_id : appId, redirect_uri : redirectUrl, diff --git a/test/getLoginUrl.js b/test/getLoginUrl.js index 77b5a03..06dc335 100644 --- a/test/getLoginUrl.js +++ b/test/getLoginUrl.js @@ -26,6 +26,7 @@ describe('deezer#getLoginUrl()', function() { it('should return a valid url', function () { assert.doesNotThrow(function () { var loginUrl = deezer.getLoginUrl(1234, 'localhost'); + console.log(loginUrl); check(loginUrl).isUrl(); }); assert.doesNotThrow(function () { diff --git a/util/mapObj.js b/util/mapObj.js new file mode 100644 index 0000000..3c96cc6 --- /dev/null +++ b/util/mapObj.js @@ -0,0 +1,16 @@ +/** + * Module dependencies + */ + +var _ = require('lodash'); + +/** + * _.map for objects, keeps key/value associations + */ + +module.exports = function mapObj (input, mapper, context) { + return _.reduce(input, function(obj, v, k) { + obj[k] = mapper.call(context, v, k, input); + return obj; + }, {}, context); +}; \ No newline at end of file