node-easypost
Advanced tools
Comparing version 2.0.2 to 2.0.3
var apiKey = 'cueqNZUb3ldeWTNX7MU3Mel8UXtaAMUi'; | ||
var easypost = require('./lib/main.js')(apiKey); | ||
var easypost = require('./lib/main.js')(apiKey); // after installing with NPM this can be require('node-easypost')(apiKey); | ||
@@ -8,3 +8,2 @@ // set addresses | ||
street1: "1A Larkspur Cres.", | ||
street2: "", | ||
city: "St. Albert", | ||
@@ -14,11 +13,13 @@ state: "AB", | ||
country: "CA", | ||
phone: "780-283-9384" | ||
phone: "780-123-4567" | ||
}; | ||
var fromAddress = { | ||
name: "Jon Calhoun", | ||
street1: "388 Townsend St", | ||
name: "EasyPost", | ||
street1: "164 Townsend St", | ||
street2: "#1", | ||
city: "San Francisco", | ||
state: "CA", | ||
zip: "94107", | ||
phone: "415-456-7890" | ||
phone: "415-123-4567" | ||
}; | ||
@@ -49,9 +50,8 @@ | ||
var parcel = { | ||
length: 10.2, | ||
width: 7.8, | ||
height: 4.3, | ||
weight: 21.2 | ||
length: 10.2, // inches | ||
width: 7.8, // inches | ||
height: 4.3, // inches | ||
weight: 21.2 // ounces | ||
}; | ||
// create customs_info form for intl shipping | ||
@@ -85,13 +85,7 @@ var customsItem = { | ||
customs_info: customsInfo | ||
}, function(err, response) { | ||
// buy postage label with one of the rate objects | ||
// shipment.buy(rate = shipment.rates[0]) | ||
// shipment.buy(rate = shipment.lowest_rate('usps')) | ||
var shipment = response; | ||
console.log(shipment); | ||
// shipment.lowestRate(['USPS', 'ups'], 'priorityMAILInternational'); | ||
shipment.buy({rate: shipment.lowestRate(['USPS', 'ups'])}, function(err, response) { | ||
console.log(response); | ||
}); | ||
}, function(err, shipment) { | ||
// shipment.lowestRate filters by carrier name and service name, and accepts negative filters by preceding the name with an exclamation mark | ||
shipment.buy({rate: shipment.lowestRate(['USPS', 'ups'], '!LibraryMail, !mediaMAIL')}, function(err, shipment) { | ||
console.log(shipment); | ||
}); | ||
}); |
@@ -62,3 +62,3 @@ "use strict"; | ||
var obj = new easypost[cls]; | ||
var obj = new easypost[cls](); | ||
obj.createFromResponse(response); | ||
@@ -76,3 +76,3 @@ return obj; | ||
} | ||
Requestor.prototype.request = function(method, path, params, callback) { | ||
Requestor.prototype.request = function(method, path, params, callback, parent) { | ||
delete params.apiKey; | ||
@@ -83,6 +83,2 @@ this.encodeParams(params); | ||
path = '/v2' + path; | ||
// console.log(method, "request for", path); | ||
// console.log("http request", requestData); | ||
var auth = 'Basic ' + new Buffer(apiKey + ":").toString('base64'); | ||
@@ -107,3 +103,3 @@ | ||
// var req = http.request(request_options); | ||
this.responseListener(req, callback); | ||
this.responseListener(req, callback, parent); | ||
req.write(requestData); | ||
@@ -143,3 +139,3 @@ req.end(); | ||
} | ||
Requestor.prototype.responseListener = function(req, cb) { | ||
Requestor.prototype.responseListener = function(req, cb, parent) { | ||
if(typeof cb !== 'function') { | ||
@@ -160,3 +156,3 @@ throw new easypost.Error("Requestor requires a callback to handle response."); | ||
if(response.error !== undefined && response.error !== null) { | ||
err = new easypost.Error(response.error.message); | ||
err = new easypost.Error(response.error); | ||
err.param = response.error.param; | ||
@@ -172,3 +168,13 @@ response = null; | ||
var parsedResponse = responseToResource(response); | ||
cb(err, parsedResponse); | ||
if (parent !== undefined && parent !== null) { | ||
for (var attr in parsedResponse) { | ||
if (attr === 'cls') { | ||
continue; | ||
} | ||
parent[attr] = parsedResponse[attr]; | ||
} | ||
cb(err, parent); | ||
} else { | ||
cb(err, parsedResponse); | ||
} | ||
} catch(e) { | ||
@@ -346,3 +352,3 @@ console.log(e.stack); | ||
var requestor = new Requestor(this.apiKey); | ||
requestor.request('post', url, args.params, args.cb); | ||
requestor.request('post', url, args.params, args.cb, this); | ||
} | ||
@@ -370,5 +376,9 @@ easypost.Shipment.prototype.refund = function(params, cb) { | ||
} | ||
easypost.Shipment.prototype.lowestRate = function(carriers, services) { | ||
carriers = null; | ||
services = null; | ||
easypost.Shipment.prototype.lowestRate = function() { | ||
var carriers = null; | ||
var services = null; | ||
var carriersInclude = []; | ||
var servicesInclude = []; | ||
var carriersExclude = []; | ||
var servicesExclude = []; | ||
@@ -384,3 +394,3 @@ if (arguments.length === 1) { | ||
if (carriers != null) { | ||
if (carriers !== null) { | ||
try { | ||
@@ -391,7 +401,11 @@ carriers = carriers.split(','); | ||
for(var i=0; i < carriers.length; i++) { | ||
carriers[i] = carriers[i].toLowerCase(); | ||
if (carriers[i].trim().charAt(0) === '!') { | ||
carriersExclude.push(carriers[i].trim().substring(1).toLowerCase()); | ||
} else { | ||
carriersInclude.push(carriers[i].trim().toLowerCase()); | ||
} | ||
} | ||
} | ||
if (services != null) { | ||
if (services !== null) { | ||
try { | ||
@@ -401,17 +415,27 @@ services = services.split(','); | ||
for(var i=0; i < services.length; i++) { | ||
services[i] = services[i].toLowerCase(); | ||
for(var j=0; j < services.length; j++) { | ||
if (services[j].trim().charAt(0) === '!') { | ||
servicesExclude.push(services[j].trim().substring(1).toLowerCase()); | ||
} else { | ||
servicesInclude.push(services[j].trim().toLowerCase()); | ||
} | ||
} | ||
} | ||
for(var i=0; i < this.rates.length; i++) { | ||
if (carriers != null && carriers.length > 0 && carriers.indexOf(this.rates[i].carrier.toLowerCase()) === -1) { | ||
for(var k=0; k < this.rates.length; k++) { | ||
if (carriersInclude.length > 0 && carriersInclude.indexOf(this.rates[k].carrier.toLowerCase()) === -1) { | ||
continue; | ||
} | ||
if (services != null && services.length > 0 && services.indexOf(this.rates[i].service.toLowerCase()) === -1) { | ||
if (carriersExclude.length > 0 && carriersExclude.indexOf(this.rates[k].carrier.toLowerCase()) !== -1) { | ||
continue; | ||
} | ||
if (servicesInclude.length > 0 && servicesInclude.indexOf(this.rates[k].service.toLowerCase()) === -1) { | ||
continue; | ||
} | ||
if (servicesExclude.length > 0 && servicesExclude.indexOf(this.rates[k].service.toLowerCase()) !== -1) { | ||
continue; | ||
} | ||
if (lowestRate === null || lowestRate.rate > this.rates[i].rate) { | ||
lowestRate = this.rates[i]; | ||
if (lowestRate === null || parseFloat(lowestRate.rate) > parseFloat(this.rates[k].rate)) { | ||
lowestRate = this.rates[k]; | ||
} | ||
@@ -530,8 +554,8 @@ } | ||
easypost.Event.receive = function(req) { | ||
var reqJson; | ||
try { | ||
var reqJson = JSON.parse(req); | ||
reqJson = JSON.parse(req); | ||
} catch(e) { | ||
throw new Error("Invalid event request from EasyPost."); | ||
} | ||
return responseToResource(reqJson); | ||
@@ -538,0 +562,0 @@ } |
{ | ||
"name": "node-easypost", | ||
"description": "EasyPost Node Client Library", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"author": "Sawyer Bateman <support@easypost.com>", | ||
@@ -6,0 +6,0 @@ "homepage": "https://easypost.com", |
@@ -17,3 +17,3 @@ # EasyPost Node Client Library | ||
var apiKey = 'cueqNZUb3ldeWTNX7MU3Mel8UXtaAMUi'; | ||
var easypost = require('./lib/main.js')(apiKey); | ||
var easypost = require('node-easypost')(apiKey); | ||
@@ -111,3 +111,3 @@ // set addresses | ||
Up-to-date documentation at: https://easypost.com/docs | ||
Up-to-date documentation at: https://www.easypost.com/docs | ||
@@ -114,0 +114,0 @@ Tests |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37487
747