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

Commit

Permalink
Updated platform array request interpretation handling and wrote tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rasvaan committed Jan 18, 2017
1 parent e44ecc2 commit 570ef4d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
27 changes: 27 additions & 0 deletions src/helpers/__tests__/request_interpretation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ describe.only('request interpretation', function() {
function(){interpret.platformParameter(wrongQuery)}
).to.throw(InterpretError);
});
it('platform array parameter', function() {
const emptyQuery = { };
const query = { "platform": 'soortenregister' };
const multipleQuery = { "platform": ['rijksmuseum', 'accurator'] };
const wrongQuery = { "platform": 'museum' };
const multipleWrongQuery = { "platform": ['museum', 'accurator'] };

expect(
interpret.platformArrayParameter(emptyQuery)
).to.be.an('array');

expect(
interpret.platformArrayParameter(query)
).to.deep.equal(['soortenregister']);

expect(
interpret.platformArrayParameter(multipleQuery)
).to.deep.equal(['rijksmuseum', 'accurator']);

expect(
function(){interpret.platformArrayParameter(wrongQuery)}
).to.throw(InterpretError);

expect(
function(){interpret.platformArrayParameter(multipleWrongQuery)}
).to.throw(InterpretError);
});
// it('object parameters', function() {
// let query = { "platform": 'soortenregister', "common_name": 'Rifleman' }
//
Expand Down
43 changes: 21 additions & 22 deletions src/helpers/request_interpretation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,10 @@ module.exports = {
objectParameters: function(query, res) {
// retrieve information about the requested bird and platforms
return this.birdParameters(query, res).then(parameters => {
parameters.platforms = this.platformParameters(query, res);
parameters.platforms = this.platformArrayParameter(query);
return parameters;
});
},
platformParameter: function(query) {
/* Return platform string based on url parameter
*
* Three situations to handle:
* 1. No parameter given -> return null
* 2. Correct platform parameter -> return input
* 3. Platform does not exist -> 404 response & return false
*/
let parameter = query.platform;

if (!parameter) throw new InterpretError('No platfom parameter provided', 400);

if (platforms.exists(parameter)) {
return parameter;
} else {
throw new InterpretError(`${parameter} platform parameter is unknown.`, 404);
}
},
annotationParameters: function(query, res) {
// retrieve one platform
const platformId = this.platformParameter(query, res);
Expand Down Expand Up @@ -130,7 +112,7 @@ module.exports = {

return date;
},
platformParameters: function(query, res) {
platformArrayParameter: function(query) {
/* Return array of platform strings based on url parameters
*
* Three situations to handle:
Expand All @@ -151,13 +133,30 @@ module.exports = {
if (platforms.exists(input[i])) {
platformParameters.push(input[i]);
} else {
res.status(404).send(`${input[i]} platform parameter is unknown.`);
return false;
throw new InterpretError(`${input[i]} platform parameter is unknown.`, 404);
}
}

return platformParameters;
},
platformParameter: function(query) {
/* Return platform string based on url parameter
*
* Three situations to handle:
* 1. No parameter given -> return null
* 2. Correct platform parameter -> return input
* 3. Platform does not exist -> 404 response & return false
*/
let parameter = query.platform;

if (!parameter) throw new InterpretError('No platfom parameter provided', 400);

if (platforms.exists(parameter)) {
return parameter;
} else {
throw new InterpretError(`${parameter} platform parameter is unknown.`, 404);
}
},
verifyGenus: function(genus) {
const url = `${platforms.platform('ioc').endpoint_location}/verify`;
const query = { "genus": genus};
Expand Down

0 comments on commit 570ef4d

Please sign in to comment.