mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
committed by
GitHub
21 changed files with 695 additions and 154 deletions
@ -0,0 +1,42 @@
|
||||
var Document = require('pelias-model').Document; |
||||
|
||||
/** |
||||
* Determine and set place id, type, and source |
||||
* |
||||
* @param {object} src |
||||
* @param {object} dst |
||||
*/ |
||||
function addMetaData(src, dst) { |
||||
dst.id = src._id; |
||||
dst.gid = makeGid(src); |
||||
dst.layer = lookupLayer(src); |
||||
dst.source = lookupSource(src); |
||||
dst.source_id = lookupSourceId(src); |
||||
if (src.hasOwnProperty('bounding_box')) { |
||||
dst.bounding_box = src.bounding_box; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Create a gid from a document |
||||
* |
||||
* @param {object} src |
||||
*/ |
||||
function makeGid(src) { |
||||
var doc = new Document(lookupSource(src), lookupLayer(src), src._id); |
||||
return doc.getGid(); |
||||
} |
||||
|
||||
function lookupSource(src) { |
||||
return src.source; |
||||
} |
||||
|
||||
function lookupSourceId(src) { |
||||
return src.source_id; |
||||
} |
||||
|
||||
function lookupLayer(src) { |
||||
return src.layer; |
||||
} |
||||
|
||||
module.exports = addMetaData; |
@ -0,0 +1,133 @@
|
||||
var _ = require('lodash'); |
||||
|
||||
// Properties to be copied
|
||||
// If a property is identified as a single string, assume it should be presented as a string in response
|
||||
// If something other than string is desired, use the following structure: { name: 'category', type: 'array' }
|
||||
var DETAILS_PROPS = [ |
||||
{ name: 'housenumber', type: 'string' }, |
||||
{ name: 'street', type: 'string' }, |
||||
{ name: 'postalcode', type: 'string' }, |
||||
{ name: 'confidence', type: 'default' }, |
||||
{ name: 'distance', type: 'default' }, |
||||
{ name: 'country', type: 'string' }, |
||||
{ name: 'country_gid', type: 'string' }, |
||||
{ name: 'country_a', type: 'string' }, |
||||
{ name: 'macroregion', type: 'string' }, |
||||
{ name: 'macroregion_gid', type: 'string' }, |
||||
{ name: 'macroregion_a', type: 'string' }, |
||||
{ name: 'region', type: 'string' }, |
||||
{ name: 'region_gid', type: 'string' }, |
||||
{ name: 'region_a', type: 'string' }, |
||||
{ name: 'macrocounty', type: 'string' }, |
||||
{ name: 'macrocounty_gid', type: 'string' }, |
||||
{ name: 'macrocounty_a', type: 'string' }, |
||||
{ name: 'county', type: 'string' }, |
||||
{ name: 'county_gid', type: 'string' }, |
||||
{ name: 'county_a', type: 'string' }, |
||||
{ name: 'localadmin', type: 'string' }, |
||||
{ name: 'localadmin_gid', type: 'string' }, |
||||
{ name: 'localadmin_a', type: 'string' }, |
||||
{ name: 'locality', type: 'string' }, |
||||
{ name: 'locality_gid', type: 'string' }, |
||||
{ name: 'locality_a', type: 'string' }, |
||||
{ name: 'borough', type: 'string' }, |
||||
{ name: 'borough_gid', type: 'string' }, |
||||
{ name: 'borough_a', type: 'string' }, |
||||
{ name: 'neighbourhood', type: 'string' }, |
||||
{ name: 'neighbourhood_gid', type: 'string' }, |
||||
{ name: 'bounding_box', type: 'default' }, |
||||
{ name: 'category', type: 'array', condition: checkCategoryParam } |
||||
]; |
||||
|
||||
function checkCategoryParam(params) { |
||||
return _.isObject(params) && params.hasOwnProperty('categories'); |
||||
} |
||||
|
||||
/** |
||||
* Add details properties |
||||
* |
||||
* @param {object} params clean query params |
||||
* @param {object} src |
||||
* @param {object} dst |
||||
*/ |
||||
function addDetails(params, src, dst) { |
||||
copyProperties(params, src, DETAILS_PROPS, dst); |
||||
} |
||||
|
||||
/** |
||||
* Copy specified properties from source to dest. |
||||
* Ignore missing properties. |
||||
* |
||||
* @param {object} params clean query params |
||||
* @param {object} source |
||||
* @param {[]} props |
||||
* @param {object} dst |
||||
*/ |
||||
function copyProperties( params, source, props, dst ) { |
||||
props.forEach( function ( prop ) { |
||||
|
||||
// if condition isn't met, just return without setting the property
|
||||
if (_.isFunction(prop.condition) && !prop.condition(params)) { |
||||
return; |
||||
} |
||||
|
||||
var property = { |
||||
name: prop.name || prop, |
||||
type: prop.type || 'default' |
||||
}; |
||||
|
||||
var value = null; |
||||
if ( source.hasOwnProperty( property.name ) ) { |
||||
|
||||
switch (property.type) { |
||||
case 'string': |
||||
value = getStringValue(source[property.name]); |
||||
break; |
||||
case 'array': |
||||
value = getArrayValue(source[property.name]); |
||||
break; |
||||
// default behavior is to copy property exactly as is
|
||||
default: |
||||
value = source[property.name]; |
||||
} |
||||
|
||||
if (_.isNumber(value) || (value && !_.isEmpty(value))) { |
||||
dst[property.name] = value; |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
function getStringValue(property) { |
||||
// isEmpty check works for all types of values: strings, arrays, objects
|
||||
if (_.isEmpty(property)) { |
||||
return ''; |
||||
} |
||||
|
||||
if (_.isString(property)) { |
||||
return property; |
||||
} |
||||
|
||||
// array value, take first item in array (at this time only used for admin values)
|
||||
if (_.isArray(property)) { |
||||
return property[0]; |
||||
} |
||||
|
||||
return _.toString(property); |
||||
} |
||||
|
||||
|
||||
function getArrayValue(property) { |
||||
// isEmpty check works for all types of values: strings, arrays, objects
|
||||
if (_.isEmpty(property)) { |
||||
return ''; |
||||
} |
||||
|
||||
if (_.isArray(property)) { |
||||
return property; |
||||
} |
||||
|
||||
return [property]; |
||||
} |
||||
|
||||
module.exports = addDetails; |
@ -0,0 +1,21 @@
|
||||
var _ = require('lodash'); |
||||
var sanitizeAll = require('../sanitiser/sanitizeAll'); |
||||
var reverseSanitizers = require('./reverse').sanitiser_list; |
||||
|
||||
// add categories to the sanitizer list
|
||||
var sanitizers = _.merge({}, reverseSanitizers, { |
||||
categories: require('../sanitiser/_categories') |
||||
}); |
||||
|
||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||
|
||||
// export sanitize for testing
|
||||
module.exports.sanitize = sanitize; |
||||
module.exports.sanitiser_list = sanitizers; |
||||
|
||||
// middleware
|
||||
module.exports.middleware = function( req, res, next ){ |
||||
sanitize( req, function( err, clean ){ |
||||
next(); |
||||
}); |
||||
}; |
@ -0,0 +1,86 @@
|
||||
module.exports = { |
||||
'query': { |
||||
'bool': { |
||||
'must': [{ |
||||
'match': { |
||||
'name.default': { |
||||
'query': 'test', |
||||
'boost': 1, |
||||
'analyzer': 'peliasQueryFullToken' |
||||
} |
||||
} |
||||
}], |
||||
'should': [{ |
||||
'match': { |
||||
'phrase.default': { |
||||
'query': 'test', |
||||
'analyzer': 'peliasPhrase', |
||||
'type': 'phrase', |
||||
'boost': 1, |
||||
'slop': 2 |
||||
} |
||||
} |
||||
}, { |
||||
'function_score': { |
||||
'query': { |
||||
'match': { |
||||
'phrase.default': { |
||||
'query': 'test', |
||||
'analyzer': 'peliasPhrase', |
||||
'type': 'phrase', |
||||
'slop': 2, |
||||
'boost': 1 |
||||
} |
||||
} |
||||
}, |
||||
'max_boost': 20, |
||||
'score_mode': 'first', |
||||
'boost_mode': 'replace', |
||||
'functions': [{ |
||||
'field_value_factor': { |
||||
'modifier': 'log1p', |
||||
'field': 'popularity', |
||||
'missing': 1 |
||||
}, |
||||
'weight': 1 |
||||
}] |
||||
} |
||||
}, { |
||||
'function_score': { |
||||
'query': { |
||||
'match': { |
||||
'phrase.default': { |
||||
'query': 'test', |
||||
'analyzer': 'peliasPhrase', |
||||
'type': 'phrase', |
||||
'slop': 2, |
||||
'boost': 1 |
||||
} |
||||
} |
||||
}, |
||||
'max_boost': 20, |
||||
'score_mode': 'first', |
||||
'boost_mode': 'replace', |
||||
'functions': [{ |
||||
'field_value_factor': { |
||||
'modifier': 'log1p', |
||||
'field': 'population', |
||||
'missing': 1 |
||||
}, |
||||
'weight': 2 |
||||
}] |
||||
} |
||||
}], |
||||
'filter': [{ |
||||
'terms': { |
||||
'category': ['retail', 'food'] |
||||
} |
||||
}] |
||||
} |
||||
}, |
||||
'size': 20, |
||||
'track_scores': true, |
||||
'sort': [ |
||||
'_score' |
||||
] |
||||
}; |
@ -0,0 +1,170 @@
|
||||
var sanitize = require( '../../../sanitiser/_categories'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.no_categories = function(test, common) { |
||||
test('categories not set', function(t) { |
||||
var req = { |
||||
query: { }, |
||||
clean: { } |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.equal(req.clean.categories, undefined, 'no categories should be defined'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('categories is empty string', function(t) { |
||||
var req = { |
||||
query: { |
||||
categories: '' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var expected_error = 'Categories parameter cannot be left blank. See documentation of service for valid options.'; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.equal(req.clean.categories, undefined, 'no categories should be defined'); |
||||
t.deepEqual(messages.errors.length, 1, 'error returned'); |
||||
t.deepEqual(messages.errors[0], expected_error, 'error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('categories is an array of empty strings', function(t) { |
||||
var req = { |
||||
query: { |
||||
categories: ',,' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var expected_error = 'Invalid categories parameter value(s). See documentation of service for valid options.'; |
||||
|
||||
var messages = sanitize(req.query, req.clean); |
||||
|
||||
t.equal(req.clean.categories, undefined, 'no categories should be defined'); |
||||
t.deepEqual(messages.errors.length, 1, 'error returned'); |
||||
t.deepEqual(messages.errors[0], expected_error, 'error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.valid_categories = function(test, common) { |
||||
var isValidCategoryCalled = 0; |
||||
var validCategories = { |
||||
isValidCategory: function (cat) { |
||||
isValidCategoryCalled++; |
||||
return ['food','health','financial','education','government'].indexOf(cat) !== -1; } |
||||
}; |
||||
|
||||
test('single category', function(t) { |
||||
isValidCategoryCalled = 0; |
||||
|
||||
var req = { |
||||
query: { |
||||
categories: 'food' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean, validCategories); |
||||
|
||||
t.deepEqual(req.clean.categories, ['food'], 'categories should contain food'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
|
||||
t.equal(isValidCategoryCalled, 1); |
||||
|
||||
t.end(); |
||||
}); |
||||
|
||||
test('multiple categories', function(t) { |
||||
isValidCategoryCalled = 0; |
||||
var req = { |
||||
query: { |
||||
categories: 'food,health' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expectedCategories = ['food', 'health']; |
||||
|
||||
var messages = sanitize(req.query, req.clean, validCategories); |
||||
|
||||
t.deepEqual(req.clean.categories, expectedCategories, |
||||
'clean.categories should be an array with proper values'); |
||||
t.deepEqual(messages.errors, [], 'no error returned'); |
||||
t.deepEqual(messages.warnings, [], 'no warnings returned'); |
||||
|
||||
t.equal(isValidCategoryCalled, expectedCategories.length); |
||||
|
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.invalid_categories = function(test, common) { |
||||
var isValidCategoryCalled = 0; |
||||
var validCategories = { |
||||
isValidCategory: function (cat) { |
||||
isValidCategoryCalled++; |
||||
return ['food','health','financial','education','government'].indexOf(cat) !== -1; } |
||||
}; |
||||
|
||||
test('garbage category', function(t) { |
||||
var req = { |
||||
query: { |
||||
categories: 'barf' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_messages = { |
||||
errors: [ |
||||
'Invalid categories parameter value(s). See documentation of service for valid options.' |
||||
], |
||||
warnings: [] |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean, validCategories); |
||||
|
||||
t.deepEqual(messages, expected_messages, 'error with message returned'); |
||||
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('all garbage categories', function(t) { |
||||
var req = { |
||||
query: { |
||||
categories: 'barf,bleh' |
||||
}, |
||||
clean: { } |
||||
}; |
||||
var expected_messages = { |
||||
errors: [ |
||||
'Invalid categories parameter value(s). See documentation of service for valid options.' |
||||
], |
||||
warnings: [] |
||||
}; |
||||
|
||||
var messages = sanitize(req.query, req.clean, validCategories); |
||||
|
||||
t.deepEqual(messages, expected_messages, 'error with message returned'); |
||||
t.equal(req.clean.categories, undefined, 'clean.categories should remain empty'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _categories ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
@ -0,0 +1,61 @@
|
||||
|
||||
var nearby = require('../../../sanitiser/nearby'); |
||||
var defaults = require('../../../query/reverse_defaults'); |
||||
var sanitize = nearby.sanitize; |
||||
var middleware = nearby.middleware; |
||||
|
||||
var defaultClean = { 'point.lat': 0, |
||||
'point.lon': 0, |
||||
'boundary.circle.lat': 0, |
||||
'boundary.circle.lon': 0, |
||||
'boundary.circle.radius': parseFloat(defaults['boundary:circle:radius']), |
||||
size: 10, |
||||
private: false |
||||
}; |
||||
|
||||
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.sanitisers = function(test, common) { |
||||
test('check sanitiser list', function (t) { |
||||
var expected = ['quattroshapes_deprecation', 'singleScalarParameters', 'layers', |
||||
'sources', 'sources_and_layers', 'size', 'private', 'geo_reverse', 'boundary_country', 'categories']; |
||||
t.deepEqual(Object.keys(nearby.sanitiser_list), expected); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.middleware_success = function(test, common) { |
||||
test('middleware success', function(t) { |
||||
var req = { query: { 'point.lat': 0, 'point.lon': 0 }}; |
||||
var next = function(){ |
||||
t.deepEqual(req.errors, [], '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 /nearby ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue