sparc-commons
Advanced tools
Comparing version 0.1.15 to 0.1.16
/*jshint multistr: true */ | ||
var _ = require('lodash') | ||
var polyline = require('polyline') | ||
var es = exports | ||
@@ -39,3 +38,3 @@ | ||
var revert = es.revert = function revert(lonlatObj, representation){ | ||
es.revert = function revert(lonlatObj, representation){ | ||
switch(representation){ | ||
@@ -59,3 +58,3 @@ case "latlon": | ||
//all representations back and forth | ||
var convert = es.convert = function convert(opts){ | ||
es.convert = function convert(opts){ | ||
var repr = _.get(opts, "representation", "latlon") | ||
@@ -106,3 +105,3 @@ var handleObj = function(coordinates, fn){ | ||
*/ | ||
var findClosest = es.findClosest = function findClosest(coords, coord, opts){ | ||
es.findClosest = function findClosest(coords, coord, opts){ | ||
var convert = this.convert(opts) | ||
@@ -124,3 +123,3 @@ var nCoord = convert(coord) | ||
var calculateLine = es.calculateLine = function calculateLine(b, e){ | ||
es.calculateLine = function calculateLine(b, e){ | ||
var slope = (e.lon - b.lon) / (e.lat - b.lat) | ||
@@ -131,3 +130,3 @@ var intercept = e.lon - (slope * e.lat) | ||
var distanceFromLine = es.distanceFromLine = function distanceFromLine(b, e, p){ | ||
es.distanceFromLine = function distanceFromLine(b, e, p){ | ||
var px = e.lon - b.lon | ||
@@ -147,3 +146,3 @@ var py = e.lat - b. lat | ||
var spliceVector = es.spliceVector = function spliceVector(segment, point, opts){ | ||
es.spliceVector = function spliceVector(segment, point, opts){ | ||
var self = this | ||
@@ -175,4 +174,3 @@ var convert = this.convert(opts) | ||
var _distance = es._distance = function _distance(coords, conversionFn){ | ||
var self = this; | ||
es._distance = function _distance(coords, conversionFn){ | ||
return _.reduce(coords, function(acc, current){ | ||
@@ -200,3 +198,3 @@ if(!_.isEmpty(acc.lastCoord)){ | ||
*/ | ||
var polylineDistance = es.polylineDistance = function polylineDistance(coordinates, opts) { | ||
es.polylineDistance = function polylineDistance(coordinates, opts) { | ||
var reduction = this._distance(coordinates, this.convert(opts)) | ||
@@ -206,3 +204,3 @@ return Math.round(reduction.dist)/1000; //return distance in Km | ||
var NMEAToEPSG3857 = es.NMEAToEPSG3857 = function NMEAToEPSG3857(lat, lon) { | ||
es.NMEAToEPSG3857 = function NMEAToEPSG3857(lat, lon) { | ||
var latitude; | ||
@@ -237,4 +235,3 @@ var longitude; | ||
if(_.isNaN(convlat) || _.isNaN(convlon)){ | ||
log.warn("Cannot convert lat: " + lat + " lon: " + lon + " into an EPSG3857 coordinate"); | ||
return ['', '']; | ||
throw Error("Cannot convert lat: " + lat + " lon: " + lon + " into an EPSG3857 coordinate"); | ||
} | ||
@@ -247,4 +244,4 @@ | ||
log.warn("One between lat: " + lat + " or lon: " + lon + " is empty"); | ||
throw Error("One between lat: " + lat + " or lon: " + lon + " is empty"); | ||
return ['', '']; | ||
} |
/*jshint multistr: true */ | ||
var needle = require('needle'), | ||
_ = require('lodash'), | ||
config = require('config'); | ||
var needle = require('needle') | ||
var _ = require('lodash') | ||
var config = require('config') | ||
@@ -9,14 +9,14 @@ var resources = {}, | ||
_.forOwn(confResource, function(v, k){ | ||
//uri field is treated as a container of services and their properties | ||
_.forOwn(confResource, function(v, k) { | ||
// uri field is treated as a container of services and their properties | ||
resources[k] = _.omit(v, 'uri'); | ||
var uri = _.get(v, 'uri', {}); | ||
_.forOwn(uri, function(uriField, uriKey){ | ||
//TODO: if uriField is an object, it means that the service has a multiplicity of properties | ||
_.forOwn(uri, function(uriField, uriKey) { | ||
// TODO: if uriField is an object, it means that the service has a multiplicity of properties | ||
_.set(resources, k + '.' + uriKey, uriField); | ||
}); | ||
//TODO: add templating management | ||
//TODO: add certificates to the picture. Both this and the latter properties imply reading files | ||
// TODO: add templating management | ||
// TODO: add certificates to the picture. Both this and the latter properties imply reading files | ||
}); | ||
@@ -36,10 +36,10 @@ | ||
*/ | ||
exports.requestResource = function requestResource(target, service, opts){ | ||
if(_.isUndefined(service)){ | ||
throw new Error("Resource invoked with undefined service parameter for resource " + target); | ||
exports.requestResource = function requestResource(target, service, opts) { | ||
if (_.isUndefined(service)) { | ||
throw new Error('Resource invoked with undefined service parameter for resource ' + target); | ||
} | ||
var options = opts || {} | ||
if(!options.open_timeout){ | ||
options.open_timeout = _.get(confResource, "default.request.timeout", 5000) | ||
if (!options.open_timeout) { | ||
options.open_timeout = _.get(confResource, 'default.request.timeout', 5000) | ||
} | ||
@@ -49,11 +49,11 @@ | ||
var url = resource.url + '/' + resource[service]; | ||
var data = _.get(opts, "data", {}); | ||
_.merge(data, _.get(resource, "api-params", {})); | ||
var data = _.get(opts, 'data', {}); | ||
_.merge(data, _.get(resource, 'api-params', {})); | ||
var requestStream = _.get(opts, 'stream'); | ||
if(!_.isUndefined(requestStream)){ | ||
//in this case all data options are brutally added to the url. | ||
//A cleaner way would be for needle to permit access to http_opts (see get_request_opts and config.http_opts in needle) | ||
if (!_.isUndefined(requestStream)) { | ||
// in this case all data options are brutally added to the url. | ||
// A cleaner way would be for needle to permit access to http_opts (see get_request_opts and config.http_opts in needle) | ||
url += '?'; | ||
url += _.reduce(data, function(u, v, k){ | ||
url += _.reduce(data, function(u, v, k) { | ||
u.push(k + '=' + encodeURIComponent(v)); | ||
@@ -64,14 +64,14 @@ return u; | ||
data = requestStream; | ||
} | ||
} | ||
return new Promise(function(res, rej){ | ||
try{ | ||
return new Promise(function(res, rej) { | ||
try { | ||
needle.request( | ||
resource.method, | ||
url, | ||
data, | ||
url, | ||
data, | ||
_.get(opts, 'request', {}), | ||
function(err, resp){ | ||
if(err){ | ||
if(_.get(err, 'code', 'ECONNRESET')){ | ||
function(err, resp) { | ||
if (err) { | ||
if (_.get(err, 'code', 'ECONNRESET')) { | ||
rej(Error(`The resource ${url} is not available or down`)) | ||
@@ -81,6 +81,6 @@ } else { | ||
} | ||
} else if(resp.statusCode == 200){ | ||
} else if (resp.statusCode == 200) { | ||
res(resp.body); | ||
} else { | ||
var myError = new Error("Resource " + target + " replied with " + resp.statusCode + " when requesting " + service + ". The url is " + url); | ||
var myError = new Error('Resource ' + target + ' replied with ' + resp.statusCode + ' when requesting ' + service + '. The url is ' + url); | ||
myError.statusCode = resp.statusCode; | ||
@@ -90,6 +90,6 @@ rej(myError); | ||
}); | ||
} catch(error){ | ||
} catch (error) { | ||
rej(error); | ||
} | ||
}); | ||
} | ||
} |
{ | ||
"name": "sparc-commons", | ||
"version": "0.1.15", | ||
"version": "0.1.16", | ||
"description": "Library with all small time common stuff used across the SPARC echosystem", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
28448
620