Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Add support for specifying services for getPublic() and getCharts().
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Markwell committed Apr 1, 2014
1 parent 50aea99 commit b2944af
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/v1/api.js
Expand Up @@ -12,33 +12,65 @@
* @private
* Request data for a user from a user's json endpoint
*
* @param {String} [service] Service name
* @param {String} user User name
* @param {String} endpoint JSON endpoint
* @param {Function} callback(data) Callback with data from JSON endpoint
*/
_get: function(user, endpoint, callback) {
_get: function(service, user, endpoint, callback) {
if (!callback) {
callback = endpoint;
endpoint = user;
user = service;
service = undefined;
}

var serviceURI = '';
if (service) {
serviceURI = 'on/' + service + '/';
}

// Use CORS XHR instead of JSONp if protocols match or we're being called from tests
// Mostly so our mock Gittip API works, but CORS is a bit cleaner regardless
var mode = new RegExp('^(/|' + location.protocol + ')').test(gittipURI) ? 'json' : 'jsonp';
_[mode](gittipURI + encodeURIComponent(user) + '/' + endpoint + '.json', callback);
_[mode](gittipURI + serviceURI + encodeURIComponent(user) + '/' + endpoint + '.json', callback);
},

// TODO: Add some sort of helper function so we aren't repeating the same
// 6 lines of code with a single string changed for every endpoint.
// At the moment the duplicated code is just in are getPublic()
// and getCharts(), but it'll be required for every endpoint added.

/**
* Get a user's public.json
*
* @param {String} [service] Service name
* @param {String} user User name
* @param {Function} callback(data) Callback with data from user's public.json
*/
getPublic: function(user, callback) {
this._get(user, 'public', callback);
getPublic: function(service, user, callback) {
if (!callback) {
callback = user;
user = service;
service = undefined;
}

this._get(service, user, 'public', callback);
},

/**
* Get a user's charts.json
* @param {String} [service] Service name
* @param {String} user User name
* @param {Function} callback(data) Callback with data from user's charts.json
*/
getCharts: function(user, callback) {
if (!callback) {
callback = user;
user = service;
service = undefined;
}

this._get(user, 'charts', callback);
}
};
Expand Down

0 comments on commit b2944af

Please sign in to comment.