mirror of https://github.com/pelias/api.git
Browse Source
- added URI-formatted `api.pipService` config support in schema - added predicate that determines whether pipService is enabled/disabled - reworked should-execute conditions for non-coarse reversepull/810/head
Stephen Hess
8 years ago
8 changed files with 218 additions and 30 deletions
@ -0,0 +1,9 @@ |
|||||||
|
const _ = require('lodash'); |
||||||
|
|
||||||
|
module.exports = (uri) => { |
||||||
|
// this predicate relies upon the fact that the schema has already validated
|
||||||
|
// that api.pipService is a URI-formatted string
|
||||||
|
return (request, response) => { |
||||||
|
return uri !== undefined; |
||||||
|
}; |
||||||
|
}; |
@ -1,33 +1,44 @@ |
|||||||
const logger = require( 'pelias-logger' ).get( 'pointinpolygon' ); |
const logger = require( 'pelias-logger' ).get( 'pointinpolygon' ); |
||||||
const request = require('request'); |
const request = require('request'); |
||||||
|
const _ = require('lodash'); |
||||||
|
|
||||||
module.exports = (url) => { |
module.exports = (url) => { |
||||||
function service( centroid, callback ){ |
if (!_.isEmpty(url)) { |
||||||
const requestUrl = `${url}/${centroid.lon}/${centroid.lat}`; |
logger.info(`using point-in-polygon service at ${url}`); |
||||||
|
|
||||||
request.get(requestUrl, (err, response, body) => { |
return function pointinpolygon( centroid, callback ) { |
||||||
if (err) { |
const requestUrl = `${url}/${centroid.lon}/${centroid.lat}`; |
||||||
logger.error(JSON.stringify(err)); |
|
||||||
callback(err); |
request.get(requestUrl, (err, response, body) => { |
||||||
} |
if (err) { |
||||||
else if (response.statusCode === 200) { |
logger.error(JSON.stringify(err)); |
||||||
try { |
callback(err); |
||||||
const parsed = JSON.parse(body); |
|
||||||
callback(err, parsed); |
|
||||||
} |
} |
||||||
catch (err) { |
else if (response.statusCode === 200) { |
||||||
logger.error(`${requestUrl}: could not parse response body: ${body}`); |
try { |
||||||
callback(`${requestUrl} returned status 200 but with non-JSON response: ${body}`); |
const parsed = JSON.parse(body); |
||||||
|
callback(err, parsed); |
||||||
|
} |
||||||
|
catch (err) { |
||||||
|
logger.error(`${requestUrl}: could not parse response body: ${body}`); |
||||||
|
callback(`${requestUrl} returned status 200 but with non-JSON response: ${body}`); |
||||||
|
} |
||||||
} |
} |
||||||
} |
else { |
||||||
else { |
logger.error(`${requestUrl} returned status ${response.statusCode}: ${body}`); |
||||||
logger.error(`${requestUrl} returned status ${response.statusCode}: ${body}`); |
callback(`${requestUrl} returned status ${response.statusCode}: ${body}`); |
||||||
callback(`${requestUrl} returned status ${response.statusCode}: ${body}`); |
} |
||||||
} |
}); |
||||||
}); |
|
||||||
|
|
||||||
} |
}; |
||||||
|
|
||||||
return service; |
} else { |
||||||
|
logger.warn('point-in-polygon service disabled'); |
||||||
|
|
||||||
|
return (centroid, callback) => { |
||||||
|
callback(`point-in-polygon service disabled, unable to resolve ${JSON.stringify(centroid)}`); |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
}; |
}; |
||||||
|
@ -0,0 +1,42 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const _ = require('lodash'); |
||||||
|
const is_pip_service_enabled = require('../../../../controller/predicates/is_pip_service_enabled'); |
||||||
|
|
||||||
|
module.exports.tests = {}; |
||||||
|
|
||||||
|
module.exports.tests.interface = (test, common) => { |
||||||
|
test('valid interface', (t) => { |
||||||
|
t.equal(typeof is_pip_service_enabled, 'function', 'is_pip_service_enabled is a function'); |
||||||
|
t.equal(typeof is_pip_service_enabled(), 'function', 'is_pip_service_enabled() is a function'); |
||||||
|
t.end(); |
||||||
|
}); |
||||||
|
}; |
||||||
|
|
||||||
|
module.exports.tests.true_conditions = (test, common) => { |
||||||
|
test('string uri should return true', (t) => { |
||||||
|
t.ok(is_pip_service_enabled('pip uri')()); |
||||||
|
t.end(); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
module.exports.tests.false_conditions = (test, common) => { |
||||||
|
test('undefined uri should return false', (t) => { |
||||||
|
t.notOk(is_pip_service_enabled()()); |
||||||
|
t.end(); |
||||||
|
|
||||||
|
}); |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
module.exports.all = (tape, common) => { |
||||||
|
function test(name, testFunction) { |
||||||
|
return tape(`GET /is_pip_service_enabled ${name}`, testFunction); |
||||||
|
} |
||||||
|
|
||||||
|
for( const testCase in module.exports.tests ){ |
||||||
|
module.exports.tests[testCase](test, common); |
||||||
|
} |
||||||
|
}; |
Loading…
Reference in new issue