mirror of https://github.com/pelias/api.git
Stephen Hess
8 years ago
3 changed files with 1 additions and 202 deletions
@ -1,93 +0,0 @@
|
||||
|
||||
var logger = require( 'pelias-logger' ).get( 'api' ), |
||||
request = require( 'superagent' ), |
||||
peliasConfig = require( 'pelias-config' ); |
||||
|
||||
/** |
||||
|
||||
language subsitution service client |
||||
|
||||
this file provides a 'transport' which can be used to access the language |
||||
service via a network connnection. |
||||
|
||||
the exported method for this module checks pelias-config for a configuration block such as: |
||||
|
||||
"language": { |
||||
"client": { |
||||
"adapter": "http", |
||||
"host": "http://localhost:6100" |
||||
} |
||||
} |
||||
|
||||
for more info on running the service see: https://github.com/pelias/placeholder
|
||||
|
||||
**/ |
||||
|
||||
/** |
||||
NullTransport |
||||
|
||||
disables the service completely |
||||
**/ |
||||
function NullTransport(){} |
||||
NullTransport.prototype.query = function( ids, cb ){ |
||||
cb(); // no-op
|
||||
}; |
||||
|
||||
/** |
||||
HttpTransport |
||||
|
||||
allows the api to be used via a remote web service |
||||
**/ |
||||
function HttpTransport( host, settings ){ |
||||
this.query = function( ids, cb ){ |
||||
request |
||||
.get( host + '/parser/findbyid' ) |
||||
.set( 'Accept', 'application/json' ) |
||||
.query({ ids: Array.isArray( ids ) ? ids.join(',') : '' }) |
||||
.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( 'language: transport not connected' ); |
||||
}; |
||||
|
||||
/** |
||||
Setup |
||||
|
||||
allows instantiation of transport depending on configuration and preference |
||||
**/ |
||||
module.exports.findById = function setup(){ |
||||
|
||||
// user config
|
||||
var config = peliasConfig.generate(); |
||||
|
||||
// ensure config variables set correctly
|
||||
if( !config.hasOwnProperty('language') || !config.language.hasOwnProperty('client') ){ |
||||
logger.warn( 'language: configuration not found' ); |
||||
} |
||||
|
||||
// valid configuration found
|
||||
else { |
||||
|
||||
// get adapter settings from config
|
||||
var settings = config.language.client; |
||||
|
||||
// http adapter
|
||||
if( 'http' === settings.adapter && settings.hasOwnProperty('host') ){ |
||||
logger.info( 'language: using http transport:', settings.host ); |
||||
if( settings.hasOwnProperty('timeout') ){ |
||||
return new HttpTransport( settings.host, { timeout: parseInt( settings.timeout, 10 ) } ); |
||||
} |
||||
return new HttpTransport( settings.host ); |
||||
} |
||||
} |
||||
|
||||
// default adapter
|
||||
logger.info( 'language: using null transport' ); |
||||
return new NullTransport(); |
||||
}; |
@ -1,107 +0,0 @@
|
||||
|
||||
var fs = require('fs'), |
||||
tmp = require('tmp'), |
||||
setup = require('../../../service/language').findById; |
||||
|
||||
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 = { language: { 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, 2, 'query function signature'); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('null adapter', function(t) { |
||||
var config = { language: { 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, 2, '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, 2, '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, 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 language', testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue