Skip to content

Commit

Permalink
Move Pitney Bowes to its own file and prepare for location parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
freshlogic committed Jun 28, 2019
1 parent 6ff312b commit 3b5d65c
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 37 deletions.
73 changes: 73 additions & 0 deletions carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const async = require('async');
const moment = require('moment-timezone');
const PitneyBowesClient = require('pitney-bowes');

const geography = require('../util/geography');

function PitneyBowes(options) {
const pitneyBowesClient = new PitneyBowesClient(options);

this.track = function(trackingNumber, callback) {
pitneyBowesClient.tracking({ trackingNumber }, function(err, data) {
if (err) {
return callback(err);
}

const results = {
events: []
};

// Set address and location of each scan detail
data.scanDetailsList.forEach(scanDetail => {
scanDetail.address = {
city: scanDetail.eventCity,
country: scanDetail.country,
state: scanDetail.eventStateOrProvince,
zip: scanDetail.postalCode
};

scanDetail.location = geography.addressToString(scanDetail.address);
});

// Get unqiue array of locations
const locations = Array.from(new Set(data.scanDetailsList.map(scanDetail => scanDetail.location)));

// Lookup each location
async.mapLimit(locations, 10, function(location, callback) {
geography.parseLocation(location, function(err, address) {
if (err) {
return callback(err);
}

address.location = location;

callback(null, address);
});
}, function(err, addresses) {
if (err) {
return callback(err);
}

data.scanDetailsList.forEach(scanDetail => {
const address = addresses.find(a => a.location === scanDetail.location);

const event = {
address: scanDetail.address,
date: moment.tz(`${scanDetail.eventDate} ${scanDetail.eventTime}`, 'YYYY-MM-DD HH:mm:ss', address.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;

results.events.push(event);
});

callback(null, results);
});
});
}
}

module.exports = PitneyBowes;
23 changes: 2 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const moment = require('moment-timezone');
const PitneyBowes = require('pitney-bowes');

const FedEx = require('./carriers/fedEx');
const PitneyBowes = require('./carriers/pitneyBowes');

function Bloodhound(options) {
const fedEx = new FedEx(options && options.fedEx);
Expand Down Expand Up @@ -41,24 +39,7 @@ function Bloodhound(options) {
if (carrier === 'fedex') {
fedEx.track(trackingNumber, callback);
} else if (carrier === 'newgistics') {
pitneyBowes.tracking({ trackingNumber }, function(err, data) {
if (err) {
return callback(err);
}

const statuses = data.scanDetailsList.map(scanDetails => {
return {
address: {
city: scanDetails.eventCity,
state: scanDetails.eventStateOrProvince
},
date: moment(`${scanDetails.eventDate} ${scanDetails.eventTime}`, 'YYYY-MM-DD HH:mm:ss').toDate(),
description: scanDetails.scanDescription
};
});

callback(null, statuses);
});
pitneyBowes.track(trackingNumber, callback);
} else {
return callback(new Error(`Carrier ${carrier} is not supported.`));
}
Expand Down
126 changes: 126 additions & 0 deletions test/carriers/pitneyBowes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const assert = require('assert');

const Bloodhound = require('../../index.js');

describe('Newgistics', function() {
it('4206336792748927005269000010615207', function(done) {
const bloodhound = new Bloodhound({
pitneyBowes: {
api_key: process.env.PITNEY_BOWES_API_KEY,
api_secret: process.env.PITNEY_BOWES_API_SECRET
}
});

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

const expected = {
events: [
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-28T13:35:00.000Z'),
description: 'Delivered, In/At Mailbox'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-28T09:19:00.000Z'),
description: 'Out for Delivery'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-28T09:09:00.000Z'),
description: 'Sorting Complete'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-27T12:21:00.000Z'),
description: 'Arrived at Post Office'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-27T11:06:00.000Z'),
description: 'Accepted at USPS Destination Facility'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-22T16:05:00.000Z'),
description: 'Departed Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-22T11:39:00.000Z'),
description: 'Arrived Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-21T17:16:00.000Z'),
description: 'Departed Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-21T08:18:00.000Z'),
description: 'Arrived Shipping Partner Facility, USPS Awaiting Item'
},
{
address: {
city: 'Carrollton',
country: null,
state: 'TX',
zip: null
},
date: new Date('2019-03-18T11:06:00.000Z'),
description: 'Picked Up by Shipping Partner, USPS Awaiting Item'
}
]
};

assert.deepStrictEqual(actual, expected);
done();
});
});
});
16 changes: 0 additions & 16 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,4 @@ describe('bloodhound.guessCarrier', function() {
it('Should guess a FedEx tracking number', function() {
assert.strictEqual(bloodhound.guessCarrier('61299998620341515252'), 'FedEx');
});
});

describe.skip('Newgistics', function() {
it('4206336792748927005269000010615207', function(done) {
const bloodhound = new Bloodhound({
pitneyBowes: {
api_key: process.env.PITNEY_BOWES_API_KEY,
api_secret: process.env.PITNEY_BOWES_API_SECRET
}
});

bloodhound.track('4206336792748927005269000010615207', 'newgistics', function(err) {
assert.ifError(err);
done();
});
});
});
43 changes: 43 additions & 0 deletions test/util/geography.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const assert = require('assert');

const geography = require('../../util/geography');

describe('geography.addressToString', function() {
it('NY', function() {
const address = {
state: 'NY'
};

assert.strictEqual(geography.addressToString(address), 'NY');
});

it('New York, NY', function() {
const address = {
city: 'New York',
state: 'NY'
};

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

it('New York, NY 10001', function() {
const address = {
city: 'New York',
state: 'NY',
zip: '10001'
};

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

it('New York, NY 10001, US', function() {
const address = {
city: 'New York',
country: 'US',
state: 'NY',
zip: '10001'
};

assert.strictEqual(geography.addressToString(address), 'New York, NY 10001, US');
});
});
30 changes: 30 additions & 0 deletions util/geography.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
exports.addressToString = function(address) {
var value = '';

if (address.city) {
value += address.city.trim();
}

if (address.state) {
if (value) {
value += ',';
}

value += ` ${address.state.trim()}`;
}

if (address.zip) {
value += ` ${address.zip.trim()}`;
}

if (address.country) {
if (value) {
value += ',';
}

value += ` ${address.country.trim()}`;
}

return value.trim();
};

exports.parseLocation = function(address, callback) {
callback(null, {
city: 'Carrollton',
Expand Down

0 comments on commit 3b5d65c

Please sign in to comment.