From ec1936598194158cf24973bbc289bdbd2cfe7617 Mon Sep 17 00:00:00 2001 From: Brian J Brennan Date: Thu, 6 Jun 2013 17:33:38 -0400 Subject: [PATCH 1/2] Implement filtering. This takes advantage of the fact that OpenBadger will have filtering at the API level once mozilla/openbadger#209 lands. --- api.js | 8 +++----- controllers/learn.js | 2 +- controllers/program.js | 3 +-- openbadger.js | 26 +++++++------------------- views/learn/list.html | 2 +- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/api.js b/api.js index b036641..4ee1e9c 100644 --- a/api.js +++ b/api.js @@ -13,7 +13,7 @@ var DEFAULT_QUERY = { function middleware (method, default_query) { if (!_.isFunction(method)) - method = this[method]; + method = this[method]; if (!_.isFunction(method)) { throw new Error('Supplied method ' + method + ' not valid'); @@ -174,7 +174,7 @@ function paginate(key, dataFn) { if (err) return callback(err, data); - if (typeof data[key].length !== 'number') + if (typeof data[key].length !== 'number') return callback(new errors.BadGateway('Unpageable data returned from upstream'), data); var pages = Math.ceil(data[key].length / pageSize); @@ -239,7 +239,7 @@ module.exports = function Api(origin, globalFilters, config) { }, /* TODO: writable is set to true for mocking, but it would be nice to revisit and try to remove that line. */ - writable: true + writable: true }); }, this); @@ -273,5 +273,3 @@ module.exports = function Api(origin, globalFilters, config) { }); }; - - diff --git a/controllers/learn.js b/controllers/learn.js index 9ac8717..21554bd 100644 --- a/controllers/learn.js +++ b/controllers/learn.js @@ -126,4 +126,4 @@ module.exports = function(app) { next(); }); -} \ No newline at end of file +} diff --git a/controllers/program.js b/controllers/program.js index f63ee62..e3d0658 100644 --- a/controllers/program.js +++ b/controllers/program.js @@ -44,7 +44,6 @@ module.exports = function (app) { app.get('/explore', badger.middleware('getPrograms'), function (req, res, next) { var data = req.remote; - res.render('programs/list.html', { filters: getFilters(req.query, ['categories', 'orgs', 'ageRanges', 'activityTypes']), items: data.programs, @@ -385,4 +384,4 @@ module.exports = function (app) { app.get('/orgs/:orgName/unfavorite', function (req, res, next) { return res.redirect('/login', 303); }); -}; \ No newline at end of file +}; diff --git a/openbadger.js b/openbadger.js index 1e8a86f..22acda4 100644 --- a/openbadger.js +++ b/openbadger.js @@ -169,23 +169,6 @@ function filterBadges (data, query) { return data; } -function filterPrograms (data, query) { - var category = confirmFilterValue(query.category, categories), - org = confirmFilterValue(query.org, orgs), - ageGroup = confirmFilterValue(query.age, ageRanges), - activityType = confirmFilterValue(query.activity, activityTypes); - - if (!category && !org && !ageGroup && !activityType) - return data; - - return applyFilter(data, { - 'categories': category, - 'issuer.shortname': org, - 'ageRange': ageGroup, - 'activityType': activityType - }); -} - function getJWTToken(email) { var claims = { prn: email, @@ -234,7 +217,13 @@ var openbadger = new Api(ENDPOINT, { getPrograms: { func: function getPrograms (query, callback) { - this.get('/programs', function(err, data) { + var qs = { + category: query.category, + org: query.org, + age: query.age, + activity: query.activity, + }; + this.get('/programs', {qs: qs}, function(err, data) { if (err) return callback(err, data); @@ -243,7 +232,6 @@ var openbadger = new Api(ENDPOINT, { }); }); }, - filters: filterPrograms, paginate: true, key: 'programs' }, diff --git a/views/learn/list.html b/views/learn/list.html index a30f6ab..41ee24d 100644 --- a/views/learn/list.html +++ b/views/learn/list.html @@ -4,4 +4,4 @@ {% block item_actions %} {{ super() }} -{% endblock %} \ No newline at end of file +{% endblock %} From 12c9c1673c9c5c7a48d214058bf349184c740002 Mon Sep 17 00:00:00 2001 From: Brian J Brennan Date: Thu, 6 Jun 2013 17:58:49 -0400 Subject: [PATCH 2/2] Figured out how to make the tests pass. --- test/openbadger.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/openbadger.test.js b/test/openbadger.test.js index 29e4fc0..ecde834 100644 --- a/test/openbadger.test.js +++ b/test/openbadger.test.js @@ -211,10 +211,11 @@ test('getProgram', function(t) { }); test('getPrograms', function(t) { + const CALLBACK_INDEX = 2; t.test('on error', function(t) { var getStub = mock.expects('get'); - getStub.callsArgWith(1, 500, 'error of some sort'); + getStub.callsArgWith(CALLBACK_INDEX, 500, 'error of some sort'); openbadger.getPrograms(DEFAULT_QUERY, function(err, data) { t.same(err, 500, 'error'); t.similar(data, 'error of some sort', 'data'); @@ -224,7 +225,7 @@ test('getPrograms', function(t) { t.test('with data', function(t) { var getStub = mock.expects('get'); - getStub.callsArgWith(1, null, DATA['programs']); + getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']); openbadger.getPrograms(DEFAULT_QUERY, function(err, data) { t.notOk(err, 'no error'); t.same(data.programs.length, 3, 'data length'); @@ -237,7 +238,7 @@ test('getPrograms', function(t) { t.test('paginates', function(t) { var getStub = mock.expects('get'); - getStub.callsArgWith(1, null, DATA['programs']); + getStub.callsArgWith(CALLBACK_INDEX, null, DATA['programs']); openbadger.getPrograms({ pageSize: 2, page: 1 }, function(err, data) { t.notOk(err, 'no error'); t.same(data.programs.length, 2, 'paginated');