-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration.js
More file actions
112 lines (95 loc) · 5.24 KB
/
Copy pathintegration.js
File metadata and controls
112 lines (95 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
let _ = require('lodash');
let rest = require('unirest');
let async = require('async');
let util = require('util');
let log = null;
const STREET_REGEX = /\d{1,9} ([^\n\r0-9,]{2,100}, ([A-Za-z ]{1,20}\d{1,9}|\d{1,3}[A-Za-z ]{1-20})[, ]?[^\n\r0-9]{2,100}[, ]?|(N |S |E |W |north |south |east |west )?\d{1,9}[^\n\r0-9 ][^\n\r0-9]{2,100}|(N |S |E |W |north |south |east |west )?\d{1,9}[^\n\r0-9, ][^\n\r0-9,]{2,100},[A-Za-z ]{1,20}\d{1,9},?[^\n\r0-9,]{2,100},?|[^\n\r0-9,]{2,100},[A-Za-z ]{1,20}\d{1,9},?[^\n\r0-9]{2,100},?|[^\n\r0-9]{2,100})[, ](AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New[ ]Hampshire|New[ ]Jersey|New[ ]Mexico|New[ ]York|North[ ]Carolina|North[ ]Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode[ ]Island|South[ ]Carolina|South[ ]Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West[ ]Virginia|Wisconsin|Wyoming)[, ]+\d{5}(?:-\d{4})?/i;
function startup(logger){
log = logger;
}
function doLookup(entities, options, cb){
if(typeof cb !== 'function'){
return;
}
if(typeof(options.apikey) !== 'string' || options.apikey.length === 0){
cb("The API key is not set.");
return;
}
let entityResults = new Array();
//look up all of the entities that are geo codes before continuing and push them into the entityResults
async.each(entities, function(entity, next){
if(entity.isGeo){
log.trace("https://maps.googleapis.com/maps/api/geocode/json?latlng="+entity.latitude+","+entity.longitude+"&key="+options.apikey);
//do a reverse geocoding lookup using google maps
rest.get("https://maps.googleapis.com/maps/api/geocode/json?latlng="+entity.latitude+","+entity.longitude+"&key="+options.apikey)
.end(function(response){
if( _.isObject(response.body) ){
let resultsObject = response.body;
log.trace({resultsObject: resultsObject});
//if the status is OK and not an error
if(resultsObject.status === "OK"){
//add any tags that the user should know (right now just the first formatted address)
entityResults.push({
entity: entity,
data: {
summary: [resultsObject.results[0].formatted_address],
details: entity
}
});
}
}
next();
});
}else if(STREET_REGEX.test(entity.value.trim())){
log.trace("https://maps.googleapis.com/maps/api/geocode/json?address="+entity.value+"&key="+options.apikey);
//do a reverse geocoding lookup using google maps
rest.get("https://maps.googleapis.com/maps/api/geocode/json?address="+entity.value+"&key="+options.apikey)
.end(function(response){
if( _.isObject(response.body) ){
let resultsObject = response.body;
log.trace({resultsObject: resultsObject});
//if the status is OK and not an error
if(resultsObject.status === "OK" && Array.isArray(resultsObject.results) && resultsObject.results.length > 0){
let result = resultsObject.results[0];
let lat = result.geometry.location.lat;
let lon = result.geometry.location.lng;
entity.longitude = lon;
entity.latitude = lat;
//add any tags that the user should know (right now just the first formatted address)
entityResults.push({
entity: entity,
data: {
summary: [util.format("Lat: %d, Long: %d", lat, lon)],
details: entity
}
});
}
}
next();
});
}else{
next();
}
},function(){
cb(null, entityResults);
});
}
function _validateOptions(options) {
let errors = [];
if(typeof(options.apikey.value) !== 'string' || options.apikey.value.length === 0){
errors.push({
key: "apikey",
message: "You must provide a valid Google Maps API Key"
});
}
return errors;
}
function validateOptions(options, cb) {
cb(null, _validateOptions(options));
}
module.exports = {
doLookup: doLookup,
startup: startup,
validateOptions: validateOptions
};