Skip to content

Commit

Permalink
Refactored how endpoints are defined, added mapObj utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Sep 8, 2013
1 parent c5492f6 commit ee3ce63
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
25 changes: 15 additions & 10 deletions index.js
Expand Up @@ -3,6 +3,7 @@
*/

var _ = require('lodash'),
mapObj = require('./util/mapObj'),
Request = require('./request'),
OAuth = require('./oauth'),
affordances = require('./affordances');
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion oauth.js
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/getLoginUrl.js
Expand Up @@ -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 () {
Expand Down
16 changes: 16 additions & 0 deletions 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);
};

0 comments on commit ee3ce63

Please sign in to comment.