start fixing URLs
This commit is contained in:
parent
ceb46b8da4
commit
3135bb34b3
@ -2,11 +2,15 @@ var Timeout = require('node-timeout');
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
// Converts an array [["param","val1"], ["param","val2"]] into param=val1¶m=val2
|
|
||||||
this.paramsToString = (params) => {
|
this.paramsToString = (params) => {
|
||||||
var kvPairs = params.map((kv) => kv[0].toString() + '=' + kv[1].toString());
|
var paramString = params.coordinates.join(';') + '.' + params.output;
|
||||||
var url = kvPairs.length ? kvPairs.join('&') : '';
|
delete params.coordinates;
|
||||||
return url.trim();
|
delete params.output;
|
||||||
|
if (Object.keys(params).length) {
|
||||||
|
paramString += '?' + Object.keys(params).map(k => k + '=' + params[k]).join('&');
|
||||||
|
}
|
||||||
|
|
||||||
|
return paramString;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.sendRequest = (baseUri, parameters, callback) => {
|
this.sendRequest = (baseUri, parameters, callback) => {
|
||||||
@ -15,7 +19,7 @@ module.exports = function () {
|
|||||||
var runRequest = (cb) => {
|
var runRequest = (cb) => {
|
||||||
var params = this.paramsToString(parameters);
|
var params = this.paramsToString(parameters);
|
||||||
|
|
||||||
this.query = baseUri + (params.length ? '?' + params : '');
|
this.query = baseUri + (params.length ? '/' + params : '');
|
||||||
|
|
||||||
var options = this.httpMethod === 'POST' ? {
|
var options = this.httpMethod === 'POST' ? {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -3,7 +3,7 @@ var request = require('request');
|
|||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
this.requestPath = (service, params, callback) => {
|
this.requestPath = (service, params, callback) => {
|
||||||
var uri = [this.HOST, service].join('/');
|
var uri = [this.HOST, service, 'v1', this.profile].join('/');
|
||||||
return this.sendRequest(uri, params, callback);
|
return this.sendRequest(uri, params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -28,70 +28,81 @@ module.exports = function () {
|
|||||||
// Overwrites the default values in defaults
|
// Overwrites the default values in defaults
|
||||||
// e.g. [[a, 1], [b, 2]], [[a, 5], [d, 10]] => [[a, 5], [b, 2], [d, 10]]
|
// e.g. [[a, 1], [b, 2]], [[a, 5], [d, 10]] => [[a, 5], [b, 2], [d, 10]]
|
||||||
this.overwriteParams = (defaults, other) => {
|
this.overwriteParams = (defaults, other) => {
|
||||||
var merged = {};
|
var otherMap = {};
|
||||||
var overwrite = (o) => {
|
for (var key in other) otherMap[key] = other[key];
|
||||||
merged[o[0]] = o[1];
|
return Object.assign({}, defaults, otherMap);
|
||||||
};
|
|
||||||
|
|
||||||
defaults.forEach(overwrite);
|
|
||||||
other.forEach(overwrite);
|
|
||||||
|
|
||||||
return Object.keys(merged).map((key) => [key, merged[key]]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var encodeWaypoints = (waypoints) => {
|
var encodeWaypoints = (waypoints) => {
|
||||||
return waypoints.map(w => ['loc', [w.lat, w.lon].join(',')]);
|
return waypoints.map(w => [w.lon, w.lat].join(','));
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestRoute = (waypoints, bearings, userParams, callback) => {
|
this.requestRoute = (waypoints, bearings, userParams, callback) => {
|
||||||
if (bearings.length && bearings.length !== waypoints.length) throw new Error('*** number of bearings does not equal the number of waypoints');
|
if (bearings.length && bearings.length !== waypoints.length) throw new Error('*** number of bearings does not equal the number of waypoints');
|
||||||
|
|
||||||
var defaults = [['output','json'], ['instructions','true'], ['alt',false]],
|
var defaults = {
|
||||||
|
output: 'json',
|
||||||
|
steps: 'true',
|
||||||
|
alternative: 'false'
|
||||||
|
},
|
||||||
params = this.overwriteParams(defaults, userParams),
|
params = this.overwriteParams(defaults, userParams),
|
||||||
encodedWaypoints = encodeWaypoints(waypoints);
|
encodedWaypoints = encodeWaypoints(waypoints);
|
||||||
|
|
||||||
|
params.coordinates = encodedWaypoints;
|
||||||
|
|
||||||
if (bearings.length) {
|
if (bearings.length) {
|
||||||
|
// TODOTODO
|
||||||
var encodedBearings = bearings.map(b => ['b', b.toString()]);
|
var encodedBearings = bearings.map(b => ['b', b.toString()]);
|
||||||
params = Array.prototype.concat.apply(params, encodedWaypoints.map((o, i) => [o, encodedBearings[i]]));
|
params = Array.prototype.concat.apply(params, encodedWaypoints.map((o, i) => [o, encodedBearings[i]]));
|
||||||
} else {
|
|
||||||
params = params.concat(encodedWaypoints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.requestPath('viaroute', params, callback);
|
return this.requestPath('route', params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestNearest = (node, userParams, callback) => {
|
this.requestNearest = (node, userParams, callback) => {
|
||||||
var defaults = [['output', 'json']],
|
var defaults = {
|
||||||
|
output: 'json'
|
||||||
|
},
|
||||||
params = this.overwriteParams(defaults, userParams);
|
params = this.overwriteParams(defaults, userParams);
|
||||||
params.push(['loc', [node.lat, node.lon].join(',')]);
|
params.coordinates = [[node.lon, node.lat].join(',')];
|
||||||
|
|
||||||
return this.requestPath('nearest', params, callback);
|
return this.requestPath('nearest', params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestTable = (waypoints, userParams, callback) => {
|
this.requestTable = (waypoints, userParams, callback) => {
|
||||||
var defaults = [['output', 'json']],
|
var defaults = {
|
||||||
|
output: 'json'
|
||||||
|
},
|
||||||
params = this.overwriteParams(defaults, userParams);
|
params = this.overwriteParams(defaults, userParams);
|
||||||
params = params.concat(waypoints.map(w => [w.type, [w.coord.lat, w.coord.lon].join(',')]));
|
|
||||||
|
params.coordinates = waypoints.map(w => [w.coord.lon, w.coord.lat].join(','));
|
||||||
|
// TODO what was 'type' here?
|
||||||
|
// params = params.concat(waypoints.map(w => [w.type, [w.coord.lat, w.coord.lon].join(',')]));
|
||||||
|
|
||||||
return this.requestPath('table', params, callback);
|
return this.requestPath('table', params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestTrip = (waypoints, userParams, callback) => {
|
this.requestTrip = (waypoints, userParams, callback) => {
|
||||||
var defaults = [['output', 'json']],
|
var defaults = {
|
||||||
|
output: 'json'
|
||||||
|
},
|
||||||
params = this.overwriteParams(defaults, userParams);
|
params = this.overwriteParams(defaults, userParams);
|
||||||
params = params.concat(encodeWaypoints(waypoints));
|
|
||||||
|
params.coordinates = encodeWaypoints(waypoints);
|
||||||
|
|
||||||
return this.requestPath('trip', params, callback);
|
return this.requestPath('trip', params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.requestMatching = (waypoints, timestamps, userParams, callback) => {
|
this.requestMatching = (waypoints, timestamps, userParams, callback) => {
|
||||||
var defaults = [['output', 'json']],
|
var defaults = {
|
||||||
|
output: 'json'
|
||||||
|
},
|
||||||
params = this.overwriteParams(defaults, userParams);
|
params = this.overwriteParams(defaults, userParams);
|
||||||
var encodedWaypoints = encodeWaypoints(waypoints);
|
|
||||||
|
params.coordinates = encodeWaypoints(waypoints);
|
||||||
|
|
||||||
if (timestamps.length) {
|
if (timestamps.length) {
|
||||||
var encodedTimestamps = timestamps.map(t => ['t', t.toString()]);
|
params.timestamps = timestamps.join(',');
|
||||||
params = Array.prototype.concat.apply(params, encodedWaypoints.map((o, i) => [o, encodedTimestamps[i]]));
|
|
||||||
} else {
|
|
||||||
params = params.concat(encodedWaypoints);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.requestPath('match', params, callback);
|
return this.requestPath('match', params, callback);
|
||||||
|
Loading…
Reference in New Issue
Block a user