mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
20 changed files with 528 additions and 326 deletions
@ -1,14 +1,14 @@
|
||||
{ |
||||
"USA": { |
||||
"local": ["local_admin", "locality", "neighborhood", "admin2"], |
||||
"regional": ["admin1_abbr", "admin1", "admin0"] |
||||
"local": ["localadmin", "locality", "neighbourhood", "county"], |
||||
"regional": ["region_a", "region", "country"] |
||||
}, |
||||
"GBR": { |
||||
"local": ["neighborhood", "admin2", "local_admin", "locality"], |
||||
"regional": ["admin2","admin0","admin1"] |
||||
"local": ["neighbourhood", "county", "localadmin", "locality"], |
||||
"regional": ["county","country","region"] |
||||
}, |
||||
"default": { |
||||
"local": ["local_admin", "locality", "neighborhood", "admin2"], |
||||
"regional": ["admin1_abbr", "admin1", "admin0"] |
||||
"local": ["localadmin", "locality", "neighbourhood", "county"], |
||||
"regional": ["region_a", "region", "country"] |
||||
} |
||||
} |
@ -0,0 +1,78 @@
|
||||
var extend = require('extend'); |
||||
var geojsonify = require('../helper/geojsonify').search; |
||||
|
||||
function setup(peliasConfig) { |
||||
|
||||
peliasConfig = peliasConfig || require( 'pelias-config' ).generate().api; |
||||
|
||||
function middleware(req, res, next) { |
||||
return convertToGeocodeJSON(peliasConfig, req, next); |
||||
} |
||||
|
||||
return middleware; |
||||
} |
||||
|
||||
function convertToGeocodeJSON(peliasConfig, req, next) { |
||||
|
||||
// do nothing if no result data set
|
||||
if (!req.results || !req.results.data) { |
||||
return next(); |
||||
} |
||||
|
||||
req.results.geojson = { geocoding: {} }; |
||||
|
||||
// REQUIRED. A semver.org compliant version number. Describes the version of
|
||||
// the GeocodeJSON spec that is implemented by this instance.
|
||||
req.results.geojson.geocoding.version = '0.1'; |
||||
|
||||
// OPTIONAL. Default: null. The licence of the data. In case of multiple sources,
|
||||
// and then multiple licences, can be an object with one key by source.
|
||||
// Can be a freeform text property describing the licensing details.
|
||||
// Can be a URI on the server, which outlines licensing details.
|
||||
req.results.geojson.geocoding.license = peliasConfig.host + '/license'; |
||||
|
||||
// OPTIONAL. Default: null. The attribution of the data. In case of multiple sources,
|
||||
// and then multiple attributions, can be an object with one key by source.
|
||||
// Can be a URI on the server, which outlines attribution details.
|
||||
req.results.geojson.geocoding.attribution = peliasConfig.host + '/attribution'; |
||||
|
||||
// OPTIONAL. Default: null. The query that has been issued to trigger the
|
||||
// search.
|
||||
// Freeform object.
|
||||
// This is the equivalent of how the engine interpreted the incoming request.
|
||||
// Helpful for debugging and understanding how the input impacts results.
|
||||
req.results.geojson.geocoding.query = req.clean; |
||||
|
||||
// OPTIONAL. Warnings and errors.
|
||||
addMessages(req.results, 'warnings', req.results.geojson.geocoding); |
||||
addMessages(req.results, 'errors', req.results.geojson.geocoding); |
||||
|
||||
// OPTIONAL
|
||||
// Freeform
|
||||
addEngine(peliasConfig, req.results.geojson.geocoding); |
||||
|
||||
// response envelope
|
||||
req.results.geojson.geocoding.timestamp = new Date().getTime(); |
||||
|
||||
// convert docs to geojson and merge with geocoding block
|
||||
extend(req.results.geojson, geojsonify(req.results.data, req.clean)); |
||||
|
||||
next(); |
||||
} |
||||
|
||||
function addMessages(results, msgType, geocoding) { |
||||
if (results.hasOwnProperty(msgType)) { |
||||
geocoding.messages = geocoding.messages || {}; |
||||
geocoding.messages[msgType] = results[msgType]; |
||||
} |
||||
} |
||||
|
||||
function addEngine(peliasConfig, geocoding) { |
||||
geocoding.engine = { |
||||
name: 'Pelias', |
||||
author: 'Mapzen', |
||||
version: peliasConfig.version |
||||
}; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,66 @@
|
||||
var extend = require('extend'); |
||||
|
||||
/** |
||||
- P is a preferred English name |
||||
- Q is a preferred name (in other languages) |
||||
- V is a well-known (but unofficial) variant for the place |
||||
(e.g. "New York City" for New York) |
||||
- S is either a synonym or a colloquial name for the place |
||||
(e.g. "Big Apple" for New York), or a version of the name which |
||||
is stripped of accent characters. |
||||
- A is an abbreviation or code for the place (e.g. "NYC" for New |
||||
York) |
||||
*/ |
||||
// config mapping of old names to new ones
|
||||
var NAME_MAP = { |
||||
'number': 'housenumber', |
||||
'zip': 'postalcode', |
||||
'alpha3': 'country_a', |
||||
'admin0': 'country', |
||||
'admin1': 'region', |
||||
'admin1_abbr': 'region_a', |
||||
'admin2': 'county', |
||||
'local_admin': 'localadmin', |
||||
'neighborhood': 'neighbourhood' |
||||
}; |
||||
|
||||
function setup() { |
||||
|
||||
return renamePlacenames; |
||||
} |
||||
|
||||
function renamePlacenames(req, res, next) { |
||||
|
||||
// do nothing if no result data set
|
||||
if (!req.results || !req.results.data) { |
||||
return next(); |
||||
} |
||||
|
||||
// loop through data items and remap placenames
|
||||
req.results.data = req.results.data.map(renameProperties); |
||||
|
||||
next(); |
||||
} |
||||
|
||||
function renameProperties(place) { |
||||
var newPlace = {}; |
||||
Object.keys(place).forEach(function (property) { |
||||
if (property === 'address') { |
||||
extend(newPlace, renameProperties(place[property])); |
||||
} |
||||
else { |
||||
renameProperty(place, newPlace, property); |
||||
} |
||||
}); |
||||
return newPlace; |
||||
} |
||||
|
||||
function renameProperty(oldObj, newObj, property) { |
||||
if (!oldObj.hasOwnProperty(property)) { |
||||
return; |
||||
} |
||||
|
||||
newObj[(NAME_MAP[property] || property)] = oldObj[property]; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,12 @@
|
||||
function sendJSONResponse(req, res, next) { |
||||
|
||||
// do nothing if no result data set
|
||||
if (!req.results || !req.results.geojson) { |
||||
return next(); |
||||
} |
||||
|
||||
// respond
|
||||
return res.status(200).json(req.results.geojson); |
||||
} |
||||
|
||||
module.exports = sendJSONResponse; |
@ -1,36 +1,91 @@
|
||||
var Router = require('express').Router; |
||||
var reverseQuery = require('../query/reverse'); |
||||
|
||||
/** ----------------------- sanitisers ----------------------- **/ |
||||
var sanitisers = {}; |
||||
sanitisers.place = require('../sanitiser/place'); |
||||
sanitisers.suggest = require('../sanitiser/suggest'); |
||||
sanitisers.search = require('../sanitiser/search'); |
||||
sanitisers.coarse = require('../sanitiser/coarse'); |
||||
sanitisers.reverse = require('../sanitiser/reverse'); |
||||
var sanitisers = { |
||||
place: require('../sanitiser/place'), |
||||
search: require('../sanitiser/search'), |
||||
reverse: require('../sanitiser/reverse') |
||||
}; |
||||
|
||||
/** ----------------------- controllers ----------------------- **/ |
||||
|
||||
var controllers = {}; |
||||
controllers.index = require('../controller/index'); |
||||
controllers.place = require('../controller/place'); |
||||
controllers.search = require('../controller/search'); |
||||
var controllers = { |
||||
index: require('../controller/index'), |
||||
place: require('../controller/place'), |
||||
search: require('../controller/search') |
||||
}; |
||||
|
||||
/** ----------------------- controllers ----------------------- **/ |
||||
|
||||
var postProc = { |
||||
renamePlacenames: require('../middleware/renamePlacenames'), |
||||
geocodeJSON: require('../middleware/geocodeJSON'), |
||||
sendJSON: require('../middleware/sendJSON') |
||||
}; |
||||
|
||||
/** |
||||
* Append routes to app |
||||
* |
||||
* @param {object} app |
||||
* @param {object} peliasConfig |
||||
*/ |
||||
function addRoutes(app, peliasConfig) { |
||||
// api root
|
||||
app.get( '/v1/', controllers.index() ); |
||||
|
||||
// place API
|
||||
app.get( '/v1/place', sanitisers.place.middleware, controllers.place() ); |
||||
/** ------------------------- routers ------------------------- **/ |
||||
|
||||
// suggest APIs
|
||||
app.get( '/v1/suggest', sanitisers.search.middleware, controllers.search() ); |
||||
app.get( '/v1/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); |
||||
app.get( '/v1/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); |
||||
var routers = { |
||||
index: createRouter([ |
||||
controllers.index() |
||||
]), |
||||
search: createRouter([ |
||||
sanitisers.search.middleware, |
||||
controllers.search(), |
||||
postProc.renamePlacenames(), |
||||
postProc.geocodeJSON(peliasConfig), |
||||
postProc.sendJSON |
||||
]), |
||||
reverse: createRouter([ |
||||
sanitisers.reverse.middleware, |
||||
controllers.search(undefined, reverseQuery), |
||||
postProc.renamePlacenames(), |
||||
postProc.geocodeJSON(peliasConfig), |
||||
postProc.sendJSON |
||||
]), |
||||
place: createRouter([ |
||||
sanitisers.place.middleware, |
||||
controllers.place(), |
||||
postProc.renamePlacenames(), |
||||
postProc.geocodeJSON(peliasConfig), |
||||
postProc.sendJSON |
||||
]) |
||||
}; |
||||
|
||||
// search APIs
|
||||
app.get( '/v1/search', sanitisers.search.middleware, controllers.search() ); |
||||
app.get( '/v1/search/coarse', sanitisers.coarse.middleware, controllers.search() ); |
||||
|
||||
// reverse API
|
||||
app.get( '/v1/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('../query/reverse')) ); |
||||
var base = '/v1/'; |
||||
|
||||
// api root
|
||||
app.get ( base, routers.index ); |
||||
app.get ( base + 'place', routers.place ); |
||||
app.get ( base + 'autocomplete', routers.search ); |
||||
app.get ( base + 'search', routers.search ); |
||||
app.post( base + 'search', routers.search ); |
||||
app.get ( base + 'reverse', routers.reverse ); |
||||
} |
||||
|
||||
/** |
||||
* Helper function for creating routers |
||||
* |
||||
* @param {[{function}]} functions |
||||
* @returns {express.Router} |
||||
*/ |
||||
function createRouter(functions) { |
||||
var router = Router(); // jshint ignore:line
|
||||
functions.forEach(function (f) { |
||||
router.use(f); |
||||
}); |
||||
return router; |
||||
} |
||||
|
||||
|
||||
module.exports.addRoutes = addRoutes; |
||||
|
@ -1,30 +0,0 @@
|
||||
|
||||
var _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/_geo'), |
||||
details: require('../sanitiser/_details') |
||||
}; |
||||
|
||||
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(); |
||||
}); |
||||
}; |
@ -1,29 +0,0 @@
|
||||
|
||||
var _sanitize = require('../sanitiser/_sanitize'), |
||||
sanitizers = { |
||||
input: require('../sanitiser/_input'), |
||||
size: require('../sanitiser/_size'), |
||||
layers: require('../sanitiser/_layers'), |
||||
details: require('../sanitiser/_details'), |
||||
latlonzoom: function( req ) { |
||||
var geo = require('../sanitiser/_geo'); |
||||
return geo(req, true); |
||||
} |
||||
}; |
||||
|
||||
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(); |
||||
}); |
||||
}; |
@ -1,72 +0,0 @@
|
||||
|
||||
var coarse = require('../../../sanitiser/coarse'), |
||||
_sanitize = coarse.sanitize, |
||||
middleware = coarse.middleware, |
||||
valid_layers = [ 'admin0', 'admin1', 'admin2', 'neighborhood', 'locality', 'local_admin' ], |
||||
defaultClean = require('../sanitiser/_input').defaultClean,
|
||||
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 clean = defaultClean; |
||||
clean.layers = valid_layers; |
||||
|
||||
var next = function( message ){ |
||||
t.equal(message, undefined, 'no error message set'); |
||||
t.deepEqual(req.clean, clean); |
||||
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); |
||||
} |
||||
}; |
Loading…
Reference in new issue