Browse Source

suggest service

pull/37/head
Peter Johnson 10 years ago
parent
commit
e24a467e96
  1. 12
      controller/suggest.js
  2. 28
      service/suggest.js

12
controller/suggest.js

@ -1,4 +1,5 @@
var service = { suggest: require('../service/suggest') };
var geojsonify = require('../helper/geojsonify').suggest; var geojsonify = require('../helper/geojsonify').suggest;
function setup( backend, query ){ function setup( backend, query ){
@ -16,18 +17,11 @@ function setup( backend, query ){
}; };
// query backend // query backend
backend().client.suggest( cmd, function( err, data ){ service.suggest( backend, cmd, function( err, docs ){
var docs = []; // error handler
// handle backend errors
if( err ){ return next( err ); } if( err ){ return next( err ); }
// map response to a valid FeatureCollection
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
}
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs );

28
service/suggest.js

@ -0,0 +1,28 @@
/**
cmd can be any valid ES suggest command
**/
function service( backend, cmd, cb ){
// query new backend
backend().client.suggest( cmd, function( err, data ){
// handle backend errors
if( err ){ return cb( err ); }
// map returned documents
var docs = [];
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
}
// fire callback
return cb( null, docs );
});
}
module.exports = service;
Loading…
Cancel
Save