2013-05-13 22:27:20 +04:00
|
|
|
const api = require('./api');
|
|
|
|
const apiMethod = api.apiMethod;
|
|
|
|
const paginate = api.paginate;
|
|
|
|
const _ = require('underscore');
|
|
|
|
|
2013-05-14 06:59:46 +04:00
|
|
|
const ENDPOINT = process.env['CSOL_OPENBADGER_URL'];
|
|
|
|
var remote = api.remote(ENDPOINT);
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
/* For swapping in a test object */
|
|
|
|
exports.setRemote = function setRemote(newRemote) {
|
|
|
|
var oldRemote = remote;
|
|
|
|
remote = newRemote;
|
|
|
|
return oldRemote;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure badges returned from remote API
|
|
|
|
// contain all the information we need
|
|
|
|
function normalizeBadge (badge, id) {
|
|
|
|
if (!id)
|
|
|
|
id = badge.shortname;
|
|
|
|
|
|
|
|
if (!badge.id)
|
|
|
|
badge.id = id;
|
|
|
|
|
|
|
|
if (!badge.url)
|
2013-05-14 06:59:46 +04:00
|
|
|
badge.url = '/badge/' + badge.id;
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
return badge;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getBadges = apiMethod(paginate('badges', function getBadges (query, callback) {
|
2013-05-13 23:46:49 +04:00
|
|
|
remote.get('/v2/badges', function(err, data) {
|
2013-05-13 22:27:20 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
|
|
|
var badges = _.map(data.badges, normalizeBadge);
|
|
|
|
|
|
|
|
return callback(null, {
|
|
|
|
badges: badges
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
exports.getBadge = apiMethod(function getBadge (query, callback) {
|
|
|
|
var id = query.id;
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
return callback(400, 'Invalid badge key');
|
|
|
|
|
2013-05-13 23:46:49 +04:00
|
|
|
remote.get('/v2/badge/' + id, function(err, data) {
|
2013-05-13 22:27:20 +04:00
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
2013-05-13 23:46:49 +04:00
|
|
|
var badge = data.badge;
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
normalizeBadge(badge, id);
|
|
|
|
|
|
|
|
callback(null, {
|
|
|
|
badge: badge
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|