diff --git a/middleware/parseBBox.js b/middleware/parseBBox.js new file mode 100644 index 00000000..990b3498 --- /dev/null +++ b/middleware/parseBBox.js @@ -0,0 +1,38 @@ +var logger = require('pelias-logger').get('api'); + +/** + * Parses the bounding box property in docs, if one is found + */ + +function setup() { + return function (req, res, next) { + // do nothing if no result data set + if (!res || !res.data) { + return next(); + } + + res.data = res.data.map(parseBBox); + + next(); + }; +} + +/* + * Parse the bbox property and form an object + */ +function parseBBox(place) { + + if (place && place.bounding_box) { + try { + place.bounding_box = JSON.parse(place.bounding_box); + } + catch (err) { + logger.error('Invalid bounding_box json string:', place); + delete place.bounding_box; + } + } + + return place; +} + +module.exports = setup; diff --git a/routes/v1.js b/routes/v1.js index 00c99aa5..eacd0a40 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -34,7 +34,8 @@ var postProc = { localNamingConventions: require('../middleware/localNamingConventions'), renamePlacenames: require('../middleware/renamePlacenames'), geocodeJSON: require('../middleware/geocodeJSON'), - sendJSON: require('../middleware/sendJSON') + sendJSON: require('../middleware/sendJSON'), + parseBoundingBox: require('../middleware/parseBBox') }; /** @@ -65,6 +66,7 @@ function addRoutes(app, peliasConfig) { postProc.dedupe(), postProc.localNamingConventions(), postProc.renamePlacenames(), + postProc.parseBoundingBox(), postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), @@ -76,6 +78,7 @@ function addRoutes(app, peliasConfig) { postProc.dedupe(), postProc.localNamingConventions(), postProc.renamePlacenames(), + postProc.parseBoundingBox(), postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), @@ -90,6 +93,7 @@ function addRoutes(app, peliasConfig) { postProc.dedupe(), postProc.localNamingConventions(), postProc.renamePlacenames(), + postProc.parseBoundingBox(), postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), @@ -98,6 +102,7 @@ function addRoutes(app, peliasConfig) { controllers.place(), postProc.localNamingConventions(), postProc.renamePlacenames(), + postProc.parseBoundingBox(), postProc.geocodeJSON(peliasConfig, base), postProc.sendJSON ]), diff --git a/test/unit/middleware/parseBBox.js b/test/unit/middleware/parseBBox.js new file mode 100644 index 00000000..4e2cd802 --- /dev/null +++ b/test/unit/middleware/parseBBox.js @@ -0,0 +1,65 @@ +var parseBBox = require('../../../middleware/parseBBox')(); + +module.exports.tests = {}; + +module.exports.tests.computeDistance = function(test, common) { + test('valid bounding_box json', function(t) { + var res = { + data: [ + { + bounding_box: '{"min_lat":40.6514712164,"max_lat":40.6737320588,"min_lon":-73.8967895508,"max_lon":-73.8665771484}' + } + ] + }; + + var expected = { + data: [ + { + bounding_box: { + min_lat: 40.6514712164, + max_lat: 40.6737320588, + min_lon: -73.8967895508, + max_lon: -73.8665771484 + } + } + ] + }; + + parseBBox({}, res, function () { + t.deepEquals(res, expected, 'correct bounding_box'); + t.end(); + }); + }); + + test('invalid bounding_box json', function(t) { + var res = { + data: [ + { + bounding_box: 'garbage json' + } + ] + }; + + var expected = { + data: [ + {} + ] + }; + + parseBBox({}, res, function () { + t.deepEquals(res, expected, 'correct bounding_box'); + t.end(); + }); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('[middleware] parseBBox: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index de13bbe0..0ce25e7d 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -27,6 +27,7 @@ var tests = [ require('./middleware/distance'), require('./middleware/localNamingConventions'), require('./middleware/dedupe'), + require('./middleware/parseBBox'), require('./query/autocomplete'), require('./query/autocomplete_defaults'), require('./query/search_defaults'),