Browse Source

map sources from helper/type_mapping.js

pull/936/head
Stephen Hess 8 years ago
parent
commit
cf167ce933
  1. 17
      middleware/interpolate.js
  2. 24
      test/unit/middleware/interpolate.js

17
middleware/interpolate.js

@ -1,5 +1,6 @@
const async = require('async'); const async = require('async');
const logger = require( 'pelias-logger' ).get( 'api' ); const logger = require( 'pelias-logger' ).get( 'api' );
const source_mapping = require('../helper/type_mapping').source_mapping;
const _ = require('lodash'); const _ = require('lodash');
/** /**
@ -64,13 +65,13 @@ function setup(service, should_execute) {
source_result.name.default = `${interpolation_result.properties.number} ${source_result.name.default}`; source_result.name.default = `${interpolation_result.properties.number} ${source_result.name.default}`;
// -- source -- // -- source --
if (interpolation_result.properties.source === 'OSM') { // lookup the lowercased source, defaulting to 'mixed' when not found
source_result.source = 'openstreetmap'; // the source mapping is a jagged string->array, so default to 'mixed' as an array
} else if (interpolation_result.properties.source === 'OA') { // to ensure that subscript works
source_result.source = 'openaddresses'; source_result.source = _.defaultTo(
} else { source_mapping[_.toLower(interpolation_result.properties.source)],
source_result.source = 'mixed'; ['mixed']
} )[0];
// -- source_id -- // -- source_id --
// note: interpolated values have no source_id // note: interpolated values have no source_id
@ -109,7 +110,7 @@ function setup(service, should_execute) {
}); });
}; };
} }
module.exports = setup; module.exports = setup;

24
test/unit/middleware/interpolate.js

@ -225,8 +225,8 @@ module.exports.tests.success_conditions = (test, common) => {
callback(null, { callback(null, {
properties: { properties: {
number: 17, number: 17,
source: 'OSM', source: 'Source Abbr 1',
source_id: 'openstreetmap source id', source_id: 'source 1 source id',
lat: 12.121212, lat: 12.121212,
lon: 21.212121 lon: 21.212121
} }
@ -236,8 +236,8 @@ module.exports.tests.success_conditions = (test, common) => {
callback(null, { callback(null, {
properties: { properties: {
number: 18, number: 18,
source: 'OA', source: 'Source Abbr 2',
source_id: 'openaddresses source id', source_id: 'source 2 source id',
lat: 13.131313, lat: 13.131313,
lon: 31.313131 lon: 31.313131
} }
@ -264,7 +264,13 @@ module.exports.tests.success_conditions = (test, common) => {
const logger = require('pelias-mock-logger')(); const logger = require('pelias-mock-logger')();
const controller = proxyquire('../../../middleware/interpolate', { const controller = proxyquire('../../../middleware/interpolate', {
'pelias-logger': logger 'pelias-logger': logger,
'../helper/type_mapping': {
source_mapping: {
'source abbr 1': ['full source name 1'],
'source abbr 2': ['full source name 2']
}
}
})(service, () => true); })(service, () => true);
const req = { const req = {
@ -331,8 +337,8 @@ module.exports.tests.success_conditions = (test, common) => {
name: { name: {
default: '17 street name 1' default: '17 street name 1'
}, },
source: 'openstreetmap', source: 'full source name 1',
source_id: 'openstreetmap source id', source_id: 'source 1 source id',
address_parts: { address_parts: {
number: 17 number: 17
}, },
@ -348,8 +354,8 @@ module.exports.tests.success_conditions = (test, common) => {
name: { name: {
default: '18 street name 3' default: '18 street name 3'
}, },
source: 'openaddresses', source: 'full source name 2',
source_id: 'openaddresses source id', source_id: 'source 2 source id',
address_parts: { address_parts: {
number: 18 number: 18
}, },

Loading…
Cancel
Save