Socket
Socket
Sign inDemoInstall

google-distance-matrix

Package Overview
Dependencies
51
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 0.0.3

170

index.js
'use strict';
var request = require('request'),
qs = require('qs');
debug = require('debug')("google:dm"),
qs = require('qs-google-signature');
var GOOGLE_DISTANCE_API_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json?',
SEPARATOR = '|',
SEPARATOR = '|',
// free api key
GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || null,
// free api key
GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || null,
// maps for business users key
GOOGLE_CLIENT_KEY = process.env.GOOGLE_BUSINESS_CLIENT_KEY || null,
GOOGLE_SIGNATURE_KEY = process.env.GOOGLE_SIGNATURE_KEY || null;
// maps for business users key
GOOGLE_CLIENT_KEY = process.env.GOOGLE_BUSINESS_CLIENT_KEY || null,
GOOGLE_SIGNATURE_KEY = process.env.GOOGLE_SIGNATURE_KEY || null;
var GoogleDistanceMatrix = function () {
this.options = {
origins: null,
destinations: null,
mode: 'driving',
units: 'metric',
language: 'en',
avoid: null
}
if (GOOGLE_CLIENT_KEY && GOOGLE_SIGNATURE_KEY) {
this.options.client = GOOGLE_CLIENT_KEY;
this.options.signature = GOOGLE_SIGNATURE_KEY;
} else {
this.options.key = GOOGLE_API_KEY;
}
var GoogleDistanceMatrix = function() {
this.options = {
origins: null,
destinations: null,
mode: 'driving',
units: 'metric',
language: 'en',
avoid: null
}
if (GOOGLE_CLIENT_KEY && GOOGLE_SIGNATURE_KEY) {
debug("Using Business Client/Key pair", GOOGLE_CLIENT_KEY, GOOGLE_SIGNATURE_KEY)
this.options.client = GOOGLE_CLIENT_KEY;
this.options.signature = GOOGLE_SIGNATURE_KEY;
} else {
debug("Using simple API Key", GOOGLE_API_KEY)
this.options.key = GOOGLE_API_KEY;
}
};
function formatLocations (locations) {
return locations.join(SEPARATOR);
function formatLocations(locations) {
return locations.join(SEPARATOR);
}
function makeRequest (options, callback) {
var requestURL = GOOGLE_DISTANCE_API_URL + qs.stringify(options);
request(requestURL, function(err, response, data) {
if (err || response.statusCode != 200) {
return callback(new Error('Google API request error: ' + data));
}
callback(null, JSON.parse(data));
})
function makeRequest(options, callback) {
debug("request options", options)
var requestURL = GOOGLE_DISTANCE_API_URL + qs.stringify(options, GOOGLE_DISTANCE_API_URL);
debug("requestURL", requestURL)
request(requestURL, function(err, response, data) {
if (err || response.statusCode != 200) {
return callback(new Error('Google API request error: ' + data));
}
callback(null, JSON.parse(data));
})
}
GoogleDistanceMatrix.prototype.matrix = function (args, cb) {
GoogleDistanceMatrix.prototype.matrix = function(args, cb) {
// validate arguments
// validate arguments
if (arguments.length < 3) {
throw new Error('Invalid number of arguments');
}
var callback = arguments[arguments.length - 1];
if (typeof callback != 'function') {
throw new Error('Missing callback function');
}
if (arguments.length < 3) {
throw new Error('Invalid number of arguments');
}
var callback = arguments[arguments.length - 1];
if (typeof callback != 'function') {
throw new Error('Missing callback function');
}
// format arguments
// format arguments
this.options.origins = formatLocations(arguments[0]);
this.options.destinations = formatLocations(arguments[1]);
this.options.origins = formatLocations(arguments[0]);
this.options.destinations = formatLocations(arguments[1]);
// makes a request to google api
// makes a request to google api
makeRequest(this.options, function(err, data) {
if (err) {
return callback(err);
}
return callback(null, data);
});
makeRequest(this.options, function(err, data) {
if (err) {
return callback(err);
}
return callback(null, data);
});
}
GoogleDistanceMatrix.prototype.mode = function (mode) {
if (mode != 'driving' && mode != 'walking' && mode != 'bicycling') {
throw new Error('Invalid mode: ' + mode);
}
this.options.mode = mode;
GoogleDistanceMatrix.prototype.mode = function(mode) {
if (mode != 'driving' && mode != 'walking' && mode != 'bicycling') {
throw new Error('Invalid mode: ' + mode);
}
this.options.mode = mode;
}
GoogleDistanceMatrix.prototype.language = function (language) {
this.options.language = language;
GoogleDistanceMatrix.prototype.language = function(language) {
this.options.language = language;
}
GoogleDistanceMatrix.prototype.avoid = function (avoid) {
if (avoid != 'tolls' && avoid != 'highways' && avoid != 'ferries') {
throw new Error('Invalid restriction: ' + avoid);
}
this.options.avoid = avoid;
GoogleDistanceMatrix.prototype.avoid = function(avoid) {
if (avoid != 'tolls' && avoid != 'highways' && avoid != 'ferries') {
throw new Error('Invalid restriction: ' + avoid);
}
this.options.avoid = avoid;
}
GoogleDistanceMatrix.prototype.units = function (units) {
if (units != 'metric' && units != 'imperial') {
throw new Error('Invalid units: ' + units);
}
this.options.units = units;
GoogleDistanceMatrix.prototype.units = function(units) {
if (units != 'metric' && units != 'imperial') {
throw new Error('Invalid units: ' + units);
}
this.options.units = units;
}
GoogleDistanceMatrix.prototype.departure_time = function (departure_time) {
this.options.departure_time = departure_time;
GoogleDistanceMatrix.prototype.departure_time = function(departure_time) {
this.options.departure_time = departure_time;
}
GoogleDistanceMatrix.prototype.key = function (key) {
delete this.options.client;
delete this.options.signature;
this.options.key = key;
GoogleDistanceMatrix.prototype.key = function(key) {
delete this.options.client;
delete this.options.signature;
this.options.key = key;
}
GoogleDistanceMatrix.prototype.client = function (client) {
delete this.options.key;
this.options.client = client;
GoogleDistanceMatrix.prototype.client = function(client) {
delete this.options.key;
this.options.client = client;
}
GoogleDistanceMatrix.prototype.signature = function (signature) {
delete this.options.key;
this.options.signature = signature;
GoogleDistanceMatrix.prototype.signature = function(signature) {
delete this.options.key;
this.options.signature = signature;
}
module.exports = new GoogleDistanceMatrix();
{
"name": "google-distance-matrix",
"version": "0.0.2",
"version": "0.0.3",
"description": "A Node.js wrapper for Google Maps Distance Matrix API",

@@ -29,5 +29,6 @@ "main": "index.js",

"dependencies": {
"qs": "^0.6.6",
"debug": "^2.1.0",
"qs-google-signature": "^1.1.1",
"request": "^2.36.0"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc