2013-05-15 23:25:41 +04:00
|
|
|
const Api = require('./api');
|
2013-05-16 17:19:30 +04:00
|
|
|
const errors = require('./lib/errors');
|
2013-05-13 22:27:20 +04:00
|
|
|
const _ = require('underscore');
|
|
|
|
|
2013-05-14 06:59:46 +04:00
|
|
|
const ENDPOINT = process.env['CSOL_OPENBADGER_URL'];
|
2013-05-16 23:46:44 +04:00
|
|
|
if (!ENDPOINT)
|
|
|
|
throw new Error('Must specify CSOL_OPENBADGER_URL in the environment');
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
// 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-15 00:18:25 +04:00
|
|
|
badge.url = '/earn/' + badge.id;
|
2013-05-13 22:27:20 +04:00
|
|
|
|
|
|
|
return badge;
|
|
|
|
}
|
|
|
|
|
2013-05-15 00:18:25 +04:00
|
|
|
function normalizeProgram(program, id) {
|
|
|
|
if (!id)
|
|
|
|
id = program.shortname;
|
|
|
|
|
|
|
|
if (!program.id)
|
|
|
|
program.id = id;
|
|
|
|
|
|
|
|
if (!program.url)
|
|
|
|
program.url = '/learn/' + program.id;
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
var openbadger = new Api(ENDPOINT, {
|
|
|
|
getBadges: {
|
|
|
|
func: function getBadges (query, callback) {
|
|
|
|
this.get('/v2/badges', function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
var badges = _.map(data.badges, normalizeBadge);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
return callback(null, {
|
|
|
|
badges: badges
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
paginate: true,
|
|
|
|
key: 'badges'
|
|
|
|
},
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
getBadge: function getBadge (query, callback) {
|
|
|
|
var id = query.id;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
if (!id)
|
2013-05-16 17:19:30 +04:00
|
|
|
return callback(new errors.BadRequest('Invalid badge key'));
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
this.get('/v2/badge/' + id, function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
var badge = data.badge;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
normalizeBadge(badge, id);
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
callback(null, {
|
|
|
|
badge: badge
|
|
|
|
});
|
2013-05-15 00:18:25 +04:00
|
|
|
});
|
2013-05-15 23:25:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getPrograms: {
|
|
|
|
func: function getPrograms (query, callback) {
|
|
|
|
this.get('/v2/programs', function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
|
|
|
var programs = _.map(data.programs, normalizeProgram);
|
|
|
|
|
|
|
|
return callback(null, {
|
|
|
|
programs: programs
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
paginate: true,
|
|
|
|
key: 'programs'
|
|
|
|
},
|
|
|
|
|
|
|
|
getProgram: function getProgram (query, callback) {
|
|
|
|
var id = query.id;
|
2013-05-15 00:18:25 +04:00
|
|
|
|
2013-05-15 23:25:41 +04:00
|
|
|
if (!id)
|
2013-05-16 17:19:30 +04:00
|
|
|
return callback(new errors.BadRequest('Invalid program key'));
|
2013-05-15 23:25:41 +04:00
|
|
|
|
|
|
|
this.get('/v2/program/' + id, function(err, data) {
|
|
|
|
if (err)
|
|
|
|
return callback(err, data);
|
|
|
|
|
|
|
|
var program = data.program;
|
|
|
|
|
|
|
|
normalizeProgram(program, id);
|
|
|
|
|
|
|
|
callback(null, {
|
|
|
|
program: program
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-05-15 00:18:25 +04:00
|
|
|
});
|
2013-05-15 23:25:41 +04:00
|
|
|
|
|
|
|
module.exports = openbadger;
|