From ec9d2245ffe7b61e6f47e61370bef3f41f294b27 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 13:50:39 -0500 Subject: [PATCH 1/6] coarse geocoder endpoint: using admin0, admin1, admin2 and neighborhood layers/datasets only --- app.js | 3 +++ helper/queryMixer.json | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/app.js b/app.js index e77bc3a2..c72dd515 100644 --- a/app.js +++ b/app.js @@ -37,6 +37,9 @@ app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.suggest(undefined, undefined, require('./helper/queryMixer').suggest_nearby) ); +app.get( '/suggest/coarse', + sanitisers.suggest.middleware, + controllers.suggest(undefined, undefined, require('./helper/queryMixer').coarse) ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/helper/queryMixer.json b/helper/queryMixer.json index e3becfba..e3096bf8 100644 --- a/helper/queryMixer.json +++ b/helper/queryMixer.json @@ -24,5 +24,16 @@ "precision": [], "fuzzy": "AUTO" } + ], + "coarse": [ + { + "layers": ["admin0","admin1","admin2","neighborhood"], + "precision": [] + }, + { + "layers": ["admin0","admin1","admin2","neighborhood"], + "precision": [], + "fuzzy": "AUTO" + } ] } \ No newline at end of file From ca6045ac8e787ba7dca402e5704fb663458e7133 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 14:02:13 -0500 Subject: [PATCH 2/6] adding variable precision levels for a comprehensive search radius --- helper/queryMixer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helper/queryMixer.json b/helper/queryMixer.json index e3096bf8..6d95ba05 100644 --- a/helper/queryMixer.json +++ b/helper/queryMixer.json @@ -28,11 +28,11 @@ "coarse": [ { "layers": ["admin0","admin1","admin2","neighborhood"], - "precision": [] + "precision": [5, 3, 1] }, { "layers": ["admin0","admin1","admin2","neighborhood"], - "precision": [], + "precision": [5, 3, 1], "fuzzy": "AUTO" } ] From a449efb5c38fd045a367cef119d9035fcbc8f413 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 14:13:45 -0500 Subject: [PATCH 3/6] adjusting precision for fuzzy matches --- helper/queryMixer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/queryMixer.json b/helper/queryMixer.json index 6d95ba05..c22670dd 100644 --- a/helper/queryMixer.json +++ b/helper/queryMixer.json @@ -32,7 +32,7 @@ }, { "layers": ["admin0","admin1","admin2","neighborhood"], - "precision": [5, 3, 1], + "precision": [3], "fuzzy": "AUTO" } ] From eeecbf71dc2fc3060bb21b4ac44dd257e8994971 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 15:09:26 -0500 Subject: [PATCH 4/6] search/coarse endpoint --- app.js | 3 +++ sanitiser/coarse.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 sanitiser/coarse.js diff --git a/app.js b/app.js index c72dd515..d499c283 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,7 @@ var sanitisers = {}; sanitisers.doc = require('./sanitiser/doc'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; +sanitisers.coarse = require('./sanitiser/coarse'); sanitisers.reverse = require('./sanitiser/reverse'); /** ----------------------- controllers ----------------------- **/ @@ -47,6 +48,8 @@ app.get( '/search', sanitisers.search.middleware, controllers.search() ); // reverse API app.get( '/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('./query/reverse')) ); +// coarse API +app.get( '/search/coarse', sanitisers.coarse.middleware, controllers.search() ); /** ----------------------- error middleware ----------------------- **/ diff --git a/sanitiser/coarse.js b/sanitiser/coarse.js new file mode 100644 index 00000000..b79f884f --- /dev/null +++ b/sanitiser/coarse.js @@ -0,0 +1,30 @@ + +var logger = require('../src/logger'), + _sanitize = require('../sanitiser/_sanitize'), + sanitizers = { + input: require('../sanitiser/_input'), + size: require('../sanitiser/_size'), + layers: function( req ) { + req.query.layers = 'admin'; + var layers = require('../sanitiser/_layers'); + return layers(req); + }, + latlonzoom: require('../sanitiser/_latlonzoom') + }; + +var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }; + +// export sanitize for testing +module.exports.sanitize = sanitize; + +// middleware +module.exports.middleware = function( req, res, next ){ + sanitize( req, function( err, clean ){ + if( err ){ + res.status(400); // 400 Bad Request + return next(err); + } + req.clean = clean; + next(); + }); +}; \ No newline at end of file From 143d8ad93a12c6b28dcc8578f1eba7f03be5b435 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 15:22:13 -0500 Subject: [PATCH 5/6] _geo --- sanitiser/coarse.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanitiser/coarse.js b/sanitiser/coarse.js index b79f884f..da07de7e 100644 --- a/sanitiser/coarse.js +++ b/sanitiser/coarse.js @@ -9,7 +9,7 @@ var logger = require('../src/logger'), var layers = require('../sanitiser/_layers'); return layers(req); }, - latlonzoom: require('../sanitiser/_latlonzoom') + latlonzoom: require('../sanitiser/_geo') }; var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }; From 68aabd7647a61a18ffb22c875d827ca9d5544565 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Mon, 19 Jan 2015 16:27:32 -0500 Subject: [PATCH 6/6] tests --- test/unit/run.js | 1 + test/unit/sanitiser/coarse.js | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/unit/sanitiser/coarse.js diff --git a/test/unit/run.js b/test/unit/run.js index ee45800b..20a5960d 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -12,6 +12,7 @@ var tests = [ require('./service/suggest'), require('./sanitiser/suggest'), require('./sanitiser/doc'), + require('./sanitiser/coarse'), require('./query/indeces'), require('./query/suggest'), require('./query/search'), diff --git a/test/unit/sanitiser/coarse.js b/test/unit/sanitiser/coarse.js new file mode 100644 index 00000000..f626141b --- /dev/null +++ b/test/unit/sanitiser/coarse.js @@ -0,0 +1,76 @@ + +var coarse = require('../../../sanitiser/coarse'), + _sanitize = coarse.sanitize, + middleware = coarse.middleware, + valid_layers = [ 'admin0', 'admin1', 'admin2', 'neighborhood' ], + sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('sanitize interface', function(t) { + t.equal(typeof sanitize, 'function', 'sanitize is a function'); + t.equal(sanitize.length, 2, 'sanitize interface'); + t.end(); + }); + test('middleware interface', function(t) { + t.equal(typeof middleware, 'function', 'middleware is a function'); + t.equal(middleware.length, 3, 'sanitizee has a valid middleware'); + t.end(); + }); +}; + +module.exports.tests.layers = function(test, common) { + test('valid layers', function(t) { + sanitize({ input: 'test', lat: 0, lon: 0 }, function( err, clean ){ + t.equal(err, undefined, 'no error'); + t.deepEqual(clean.layers, valid_layers, 'layers set correctly'); + }); + t.end(); + }); +}; + +module.exports.tests.middleware_failure = function(test, common) { + test('middleware failure', function(t) { + var res = { status: function( code ){ + t.equal(code, 400, 'status set'); + }}; + var next = function( message ){ + var defaultError = 'invalid param \'input\': text length, must be >0'; + t.equal(message, defaultError); + t.end(); + }; + middleware( {}, res, next ); + }); +}; + +module.exports.tests.middleware_success = function(test, common) { + test('middleware success', function(t) { + var req = { query: { input: 'test', lat: 0, lon: 0 }}; + var next = function( message ){ + var defaultClean = { + input: 'test', + size: 10, + layers: [ 'admin0', 'admin1', 'admin2', 'neighborhood' ], + lat: 0, + lon: 0, + zoom: 10 + }; + t.equal(message, undefined, 'no error message set'); + t.deepEqual(req.clean, defaultClean); + t.end(); + }; + middleware( req, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SANTIZE /coarse ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file