Browse Source

Merge branch 'master' into search-bbox

Conflicts:
	package.json
pull/60/head
Harish Krishna 10 years ago
parent
commit
392494d8fb
  1. 9
      EXAMPLES.md
  2. 1
      package.json
  3. 4
      query/indeces.js
  4. 86
      sanitiser/_layers.js
  5. 2
      test/unit/query/indeces.js
  6. 20
      test/unit/sanitiser/suggest.js

9
EXAMPLES.md

@ -0,0 +1,9 @@
#### Search:
- Nearest Museums: http://pelias.mapzen.com/search?lat=51.533&lon=-0.0652&input=museum&size=40
- Nearest Hotels: http://pelias.mapzen.com/search?lat=51.533&lon=-0.0652&input=hotel&size=40
#### Autocomplete:
- Local Neighborhoods: http://pelias.mapzen.com/suggest/nearby?lat=40.7259&lon=-73.9806&input=e&layers=neighborhood&size=40

1
package.json

@ -37,6 +37,7 @@
"geojson": "^0.2.0",
"geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#geo_bbox",
"is-object": "^1.0.1",
"pelias-esclient": "0.0.25",
"toobusy": "^0.2.4"
},

4
query/indeces.js

@ -8,5 +8,7 @@ module.exports = [
'admin0',
'admin1',
'admin2',
'neighborhood'
'neighborhood',
'osmaddress',
'openaddresses'
];

86
sanitiser/_layers.js

@ -1,4 +1,6 @@
var indeces = require('../query/indeces');
var isObject = require('is-object'),
indeces = require('../query/indeces');
// validate inputs, convert types and apply defaults
function sanitize( req ){
@ -7,50 +9,56 @@ function sanitize( req ){
var params= req.query;
// ensure the input params are a valid object
if( Object.prototype.toString.call( params ) !== '[object Object]' ){
if( !isObject( params ) ){
params = {};
}
// which layers to query
if('string' === typeof params.layers && params.layers.length){
var alias_layers = ['poi', 'admin'];
var alias_indeces = indeces.concat(alias_layers);
var layers = params.layers.split(',').map( function( layer ){
return layer.toLowerCase(); // lowercase inputs
});
for( var x=0; x<layers.length; x++ ){
if( -1 === alias_indeces.indexOf( layers[x] ) ){
return {
'error': true,
'message': 'invalid param \'layer\': must be one or more of ' + alias_indeces.join(',')
};
}
}
var expand_aliases = function(alias, layers, layer_indeces) {
var alias_index = layers.indexOf(alias);
if (alias_index !== -1 ) {
layers.splice(alias_index, 1);
layers = layers.concat(layer_indeces);
}
return layers;
};
layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']);
layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood']);
// de-dup
layers = layers.filter(function(item, pos) {
return layers.indexOf(item) === pos;
});
clean.layers = layers;
// default case (no layers specified in GET params)
if('string' !== typeof params.layers || !params.layers.length){
// @note: 'address' alias disabled by default until feature testing completed
params.layers = 'poi,admin'; // default layers
}
else {
clean.layers = indeces; // default (all layers)
// decide which layers can be queried
var alias_layers = ['poi', 'admin', 'address'];
var alias_indeces = indeces.concat(alias_layers);
// parse GET params
var layers = params.layers.split(',').map( function( layer ){
return layer.toLowerCase(); // lowercase inputs
});
// validate layer names
for( var x=0; x<layers.length; x++ ){
if( -1 === alias_indeces.indexOf( layers[x] ) ){
return {
'error': true,
'message': 'invalid param \'layer\': must be one or more of ' + alias_indeces.join(',')
};
}
}
// expand aliases
var expand_aliases = function(alias, layers, layer_indeces) {
var alias_index = layers.indexOf(alias);
if (alias_index !== -1 ) {
layers.splice(alias_index, 1);
layers = layers.concat(layer_indeces);
}
return layers;
};
layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']);
layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood']);
layers = expand_aliases('address', layers, ['osmaddress','openaddresses']);
// de-dupe
layers = layers.filter(function(item, pos) {
return layers.indexOf(item) === pos;
});
// pass validated params to next middleware
clean.layers = layers;
req.clean = clean;
return { 'error': false };

2
test/unit/query/indeces.js

@ -6,7 +6,7 @@ module.exports.tests = {};
module.exports.tests.interface = function(test, common) {
test('valid interface', function(t) {
t.true(Array.isArray(indeces), 'valid array');
t.equal(indeces.length, 7, 'valid array');
t.equal(indeces.length, 9, 'valid array');
t.end();
});
};

20
test/unit/sanitiser/suggest.js

@ -220,8 +220,9 @@ module.exports.tests.sanitize_layers = function(test, common) {
});
test('invalid layer', function(t) {
sanitize({ layers: 'test_layer', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
var msg = 'invalid param \'layer\': must be one or more of geoname,osmnode,osmway,admin0,admin1,admin2,neighborhood,poi,admin';
t.equal(err, msg, 'invalid layer requested');
var msg = 'invalid param \'layer\': must be one or more of ';
t.true(err.match(msg), 'invalid layer requested');
t.true(err.length > msg.length, 'invalid error message');
t.end();
});
});
@ -239,6 +240,13 @@ module.exports.tests.sanitize_layers = function(test, common) {
t.end();
});
});
test('address (alias) layer', function(t) {
var address_layers = ['osmaddress','openaddresses'];
sanitize({ layers: 'address', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, address_layers, 'address layers set');
t.end();
});
});
test('poi alias layer plus regular layers', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
var reg_layers = ['admin0', 'admin1'];
@ -255,6 +263,14 @@ module.exports.tests.sanitize_layers = function(test, common) {
t.end();
});
});
test('address alias layer plus regular layers', function(t) {
var address_layers = ['osmaddress','openaddresses'];
var reg_layers = ['geoname', 'osmway'];
sanitize({ layers: 'address,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set');
t.end();
});
});
test('alias layer plus regular layers (no duplicates)', function(t) {
var poi_layers = ['geoname','osmnode','osmway'];
sanitize({ layers: 'poi,geoname,osmnode', input: 'test', lat: 0, lon: 0 }, function( err, clean ){

Loading…
Cancel
Save