-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Pitney Bowes to its own file and prepare for location parsing
- Loading branch information
1 parent
6ff312b
commit 3b5d65c
Showing
6 changed files
with
274 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters