mirror of https://github.com/pelias/api.git
Stephen Hess
7 years ago
6 changed files with 681 additions and 560 deletions
@ -1,118 +0,0 @@
|
||||
|
||||
var logger = require( 'pelias-logger' ).get( 'api' ), |
||||
request = require( 'superagent' ), |
||||
peliasConfig = require( 'pelias-config' ); |
||||
|
||||
/** |
||||
|
||||
street address interpolation service client |
||||
|
||||
this file provides several different 'transports' which can be used to access the interpolation |
||||
service, either directly from disk or via a network connnection. |
||||
|
||||
the exported method for this module checks pelias-config for a configuration block such as: |
||||
|
||||
"interpolation": { |
||||
"client": { |
||||
"adapter": "http", |
||||
"host": "http://localhost:4444" |
||||
} |
||||
} |
||||
|
||||
for more info on running the service see: https://github.com/pelias/interpolation
|
||||
|
||||
**/ |
||||
|
||||
/** |
||||
NullTransport |
||||
|
||||
disables the service completely |
||||
**/ |
||||
function NullTransport(){} |
||||
NullTransport.prototype.query = function( coord, number, street, cb ){ |
||||
cb(); // no-op
|
||||
}; |
||||
|
||||
/** |
||||
RequireTransport |
||||
|
||||
allows the api to be used by simply requiring the module |
||||
**/ |
||||
function RequireTransport( addressDbPath, streetDbPath ){ |
||||
try { |
||||
var lib = require('pelias-interpolation'); // lazy load dependency
|
||||
this.query = lib.api.search( addressDbPath, streetDbPath ); |
||||
} catch( e ){ |
||||
logger.error( 'RequireTransport: failed to connect to interpolation service' ); |
||||
} |
||||
} |
||||
RequireTransport.prototype.query = function( coord, number, street, cb ){ |
||||
throw new Error( 'interpolation: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
HttpTransport |
||||
|
||||
allows the api to be used via a remote web service |
||||
**/ |
||||
function HttpTransport( host, settings ){ |
||||
this.query = function( coord, number, street, cb ){ |
||||
request |
||||
.get( host + '/search/geojson' ) |
||||
.set( 'Accept', 'application/json' ) |
||||
.query({ lat: coord.lat, lon: coord.lon, number: number, street: street }) |
||||
.timeout( settings && settings.timeout || 1000 ) |
||||
.end( function( err, res ){ |
||||
if( err || !res ){ return cb( err ); } |
||||
if( 200 !== res.status ){ return cb( 'non 200 status' ); } |
||||
return cb( null, res.body ); |
||||
}); |
||||
}; |
||||
} |
||||
HttpTransport.prototype.query = function( coord, number, street, cb ){ |
||||
throw new Error( 'interpolation: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
Setup |
||||
|
||||
allows instantiation of transport depending on configuration and preference |
||||
**/ |
||||
module.exports.search = function setup(){ |
||||
|
||||
// user config
|
||||
var config = peliasConfig.generate(); |
||||
|
||||
// ensure config variables set correctly
|
||||
if( !config.hasOwnProperty('interpolation') || !config.interpolation.hasOwnProperty('client') ){ |
||||
logger.warn( 'interpolation: configuration not found' ); |
||||
} |
||||
|
||||
// valid configuration found
|
||||
else { |
||||
|
||||
// get adapter settings from config
|
||||
var settings = config.interpolation.client; |
||||
|
||||
// http adapter
|
||||
if( 'http' === settings.adapter && settings.hasOwnProperty('host') ){ |
||||
logger.info( 'interpolation: using http transport:', settings.host ); |
||||
if( settings.hasOwnProperty('timeout') ){ |
||||
return new HttpTransport( settings.host, { timeout: parseInt( settings.timeout, 10 ) } ); |
||||
} |
||||
return new HttpTransport( settings.host ); |
||||
} |
||||
|
||||
// require adapter
|
||||
else if( 'require' === settings.adapter ){ |
||||
if( settings.hasOwnProperty('streetdb') && settings.hasOwnProperty('addressdb') ){ |
||||
logger.info( 'interpolation: using require transport' ); |
||||
return new RequireTransport( settings.addressdb, settings.streetdb ); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// default adapter
|
||||
logger.info( 'interpolation: using null transport' ); |
||||
return new NullTransport(); |
||||
}; |
@ -1,128 +0,0 @@
|
||||
|
||||
var fs = require('fs'), |
||||
tmp = require('tmp'), |
||||
setup = require('../../../service/interpolation').search; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('valid interface', function(t) { |
||||
t.equal(typeof setup, 'function', 'setup is a function'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
// adapter factory
|
||||
module.exports.tests.factory = function(test, common) { |
||||
|
||||
test('http adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'http', |
||||
host: 'http://example.com' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'HttpTransport', 'HttpTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('require adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'require', |
||||
addressdb: '/tmp/address.db', |
||||
streetdb: '/tmp/street.db' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'RequireTransport', 'RequireTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('null adapter', function(t) { |
||||
var config = { interpolation: { client: { |
||||
adapter: 'null' |
||||
}}}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('default adapter', function(t) { |
||||
var config = {}; |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, JSON.stringify( config ), { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
t.equal(adapter.constructor.name, 'NullTransport', 'NullTransport'); |
||||
t.equal(typeof adapter, 'object', 'adapter is an object'); |
||||
t.equal(typeof adapter.query, 'function', 'query is a function'); |
||||
t.equal(adapter.query.length, 4, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
// null transport
|
||||
module.exports.tests.NullTransport = function(test, common) { |
||||
|
||||
test('null transport', function(t) { |
||||
|
||||
// adapter is driven by config
|
||||
var tmpfile = tmp.tmpNameSync({ postfix: '.json' }); |
||||
fs.writeFileSync( tmpfile, '{}', { encoding: 'utf8' } ); |
||||
process.env.PELIAS_CONFIG = tmpfile; |
||||
var adapter = setup(); |
||||
delete process.env.PELIAS_CONFIG; |
||||
|
||||
// test null transport performs a no-op
|
||||
adapter.query( null, null, null, function( err, res ){ |
||||
t.equal(err, undefined, 'no-op'); |
||||
t.equal(res, undefined, 'no-op'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SERVICE interpolation', testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue