Skip to content

Commit

Permalink
Added support for geography.parseLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
freshlogic committed Jul 1, 2019
1 parent 3b5d65c commit f0653c8
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 43 deletions.
18 changes: 15 additions & 3 deletions carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,28 @@ function PitneyBowes(options) {

data.scanDetailsList.forEach(scanDetail => {
const address = addresses.find(a => a.location === scanDetail.location);
let timezone = 'America/New_York';

if (address && address.timezone) {
timezone = address.timezone;
}

const event = {
address: scanDetail.address,
date: moment.tz(`${scanDetail.eventDate} ${scanDetail.eventTime}`, 'YYYY-MM-DD HH:mm:ss', address.timezone).toDate(),
date: moment.tz(`${scanDetail.eventDate} ${scanDetail.eventTime}`, 'YYYY-MM-DD HH:mm:ss', timezone).toDate(),
description: scanDetail.scanDescription
};

// Use the city and state from the parsed address (for scenarios where the city includes the state like "New York, NY")
event.address.city = address.city;
event.address.state = address.state;
if (address) {
if (address.city) {
event.address.city = address.city;
}

if (address.state) {
event.address.state = address.state;
}
}

results.events.push(event);
});
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"dependencies": {
"async": "~3.1.0",
"moment-timezone": "~0.5.25",
"node-geocoder": "~3.23.0",
"pitney-bowes": "~0.1.0",
"shipping-fedex": "0.2.0"
"shipping-fedex": "0.2.0",
"tz-lookup": "~6.1.18",
"us-states-normalize": "~1.0.0"
},
"devDependencies": {
"coveralls": "*",
Expand Down
68 changes: 35 additions & 33 deletions test/carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const assert = require('assert');
const Bloodhound = require('../../index.js');

describe('Newgistics', function() {
this.timeout(10000);

it('4206336792748927005269000010615207', function(done) {
const bloodhound = new Bloodhound({
pitneyBowes: {
Expand All @@ -11,99 +13,99 @@ describe('Newgistics', function() {
}
});

bloodhound.track('4206336792748927005269000010615207', 'newgistics', function(err, actual) {
bloodhound.track('4209302392612927005269000027783702', 'newgistics', function(err, actual) {
assert.ifError(err);

const expected = {
events: [
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-28T13:35:00.000Z'),
description: 'Delivered, In/At Mailbox'
date: new Date('2019-06-30T18:03:00.000Z'),
description: 'Delivered, Front Door/Porch'
},
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-28T09:19:00.000Z'),
description: 'Out for Delivery'
date: new Date('2019-06-30T02:51:00.000Z'),
description: 'Delivery Attempted - No Access to Delivery Location'
},
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-28T09:09:00.000Z'),
description: 'Sorting Complete'
date: new Date('2019-06-29T16:10:00.000Z'),
description: 'Out for Delivery'
},
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-27T12:21:00.000Z'),
description: 'Arrived at Post Office'
date: new Date('2019-06-29T16:00:00.000Z'),
description: 'Sorting Complete'
},
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-27T11:06:00.000Z'),
description: 'Accepted at USPS Destination Facility'
date: new Date('2019-06-28T22:58:00.000Z'),
description: 'Arrived at Post Office'
},
{
address: {
city: 'Carrollton',
city: 'Ojai',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-22T16:05:00.000Z'),
description: 'Departed Shipping Partner Facility, USPS Awaiting Item'
date: new Date('2019-06-28T21:43:00.000Z'),
description: 'Accepted at USPS Destination Facility'
},
{
address: {
city: 'Carrollton',
city: 'LA',
country: null,
state: 'TX',
state: 'CA',
zip: null
},
date: new Date('2019-03-22T11:39:00.000Z'),
date: new Date('2019-06-28T01:49:00.000Z'),
description: 'Arrived Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-21T17:16:00.000Z'),
date: new Date('2019-06-26T20:26:00.000Z'),
description: 'Departed Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
city: 'Grapevine',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-21T08:18:00.000Z'),
date: new Date('2019-06-26T13:04:00.000Z'),
description: 'Arrived Shipping Partner Facility, USPS Awaiting Item'
},
{
Expand All @@ -113,7 +115,7 @@ describe('Newgistics', function() {
state: 'TX',
zip: null
},
date: new Date('2019-03-18T11:06:00.000Z'),
date: new Date('2019-06-25T13:09:00.000Z'),
description: 'Picked Up by Shipping Partner, USPS Awaiting Item'
}
]
Expand Down
28 changes: 28 additions & 0 deletions test/util/geography.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,32 @@ describe('geography.addressToString', function() {

assert.strictEqual(geography.addressToString(address), 'New York, NY 10001, US');
});
});

describe('geography.parseLocation', function() {
this.timeout(10000);

it('New York NY', function(done) {
geography.parseLocation('New York NY', function(err, actual) {
assert.ifError(err);

const expected = {
city: 'NYC',
state: 'NY',
timezone: 'America/New_York'
};

assert.deepStrictEqual(actual, expected);
done();
});
});

it('O FALLON, MO', function(done) {
geography.parseLocation('O FALLON, MO', function(err, actual) {
assert.ifError(err);

assert.strictEqual(actual, undefined);
done();
});
});
});
62 changes: 56 additions & 6 deletions util/geography.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
const async = require('async');
const NodeGeocoder = require('node-geocoder');
const normalize = require('us-states-normalize');
const tzlookup = require('tz-lookup');

const geocoder = NodeGeocoder({ provider: 'openstreetmap' });

function geocode(location, callback) {
// Geocode the location
async.retry(function(callback) {
geocoder.geocode(location, callback);
}, function(err, results) {
if (err) {
return callback(err);
}

if (!results.length) {
return callback();
}

const firstResult = results[0];

// Check to see if the first result has the data we need
if (firstResult.city && firstResult.state && firstResult.zipcode) {
return callback(null, firstResult);
}

// Reverse geocode to ensure we get a city, state, and zip
async.retry(function(callback) {
geocoder.reverse({ lat: firstResult.latitude, lon: firstResult.longitude }, callback);
}, function(err, results) {
if (err) {
return callback(err);
}

callback(null, results[0]);
});
});
}

exports.addressToString = function(address) {
var value = '';

Expand Down Expand Up @@ -28,10 +68,20 @@ exports.addressToString = function(address) {
return value.trim();
};

exports.parseLocation = function(address, callback) {
callback(null, {
city: 'Carrollton',
state: 'TX',
timzezone: 'America/Chicago'
exports.parseLocation = async.memoize(function(location, callback) {
geocode(location, function(err, result) {
if (err) {
return callback(err);
}

if (!result) {
return callback();
}

callback(null, {
city: result.city,
state: normalize(result.state),
timezone: tzlookup(result.latitude, result.longitude)
});
});
};
});

0 comments on commit f0653c8

Please sign in to comment.