geo.what3words
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -1,5 +0,3 @@ | ||
var http = require('http'), | ||
unirest = require('unirest'), | ||
Promise = require('bluebird'), | ||
_ = require('lodash'); | ||
var request = require('request'), | ||
_ = require('lodash'); | ||
@@ -25,3 +23,3 @@ /** | ||
this.apiKey = apiKey; | ||
this.endpoint = options.endpoint || 'http://api.what3words.com/'; | ||
this.endpoint = options.endpoint || 'https://api.what3words.com/'; | ||
this.language = options.language || 'en'; | ||
@@ -41,3 +39,3 @@ this.userAgent = options.userAgent || 'JS Geo::What3Words'; | ||
*/ | ||
What3Words.prototype.oneWordAvailable = function (options, callback) { | ||
What3Words.prototype.oneWordAvailable = function (options) { | ||
var language = options.lang || this.language; | ||
@@ -55,3 +53,3 @@ | ||
.catch(function(err) { | ||
reject(err) | ||
reject(err); | ||
}); | ||
@@ -69,6 +67,12 @@ }, this)); | ||
*/ | ||
What3Words.prototype.positionToWords = function (options, callback) { | ||
var language = options.lang || this.language; | ||
What3Words.prototype.positionToWords = function (options) { | ||
var language = options.lang || this.language, | ||
position = options.position || false; | ||
return new Promise(_.bind(function(resolve, reject) { | ||
if(!this.validateLatLng(position)) { | ||
reject({error:21, message: "Invalid coordinates"}); | ||
} | ||
this.execute('position', {position: options.position, lang: language}) | ||
@@ -79,7 +83,7 @@ .then(function(res) { | ||
} else { | ||
resolve(res.words.join(',')); | ||
resolve(res.words.join('.')); | ||
} | ||
}) | ||
.catch(function(err) { | ||
reject(err) | ||
reject(err); | ||
}); | ||
@@ -97,6 +101,11 @@ }, this)); | ||
*/ | ||
What3Words.prototype.wordsToPosition = function (options, callback) { | ||
What3Words.prototype.wordsToPosition = function (options) { | ||
var language = options.lang || this.language; | ||
return new Promise(_.bind(function(resolve, reject) { | ||
if(!this.validateWord(options.words)){ | ||
reject({error:11, message: "String not recognised"}); | ||
} | ||
this.execute('w3w', {string: options.words, lang: language}) | ||
@@ -111,3 +120,3 @@ .then(function(res) { | ||
.catch(function(err) { | ||
reject(err) | ||
reject(err); | ||
}); | ||
@@ -140,2 +149,21 @@ }, this)); | ||
/** | ||
* Validations | ||
*/ | ||
What3Words.prototype.validateWord = function (word) { | ||
return word.match(/^\w+\.\w+\.\w+$/); | ||
}; | ||
What3Words.prototype.validateLatLng = function (latlng) { | ||
var coordinates = latlng.split(','); | ||
if(coordinates.length === 2) { | ||
var lat = Number(coordinates[0]), | ||
lng = Number(coordinates[1]); | ||
if((lng > -180 && lng < 180) && (lat > -90 && lat < 90)) { | ||
return true; | ||
} | ||
} | ||
return; | ||
}; | ||
/** | ||
* Getters | ||
@@ -157,29 +185,35 @@ */ | ||
/** | ||
* Sends a given request as a JSON object to the W3W API and finally | ||
* calls the given callback function with the resulting JSON object. | ||
* Sends a given request as a JSON object to the W3W API and returns | ||
* a promise which if resolved will contain the resulting JSON object. | ||
* | ||
* @param {[type]} method W3W API method to call | ||
* @param {[type]} params Object containg parameters to call the API with | ||
* @param {Function} callback To be called on success | ||
* @param {Function} Promise | ||
*/ | ||
What3Words.prototype.execute = function (method, params) { | ||
return new Promise(_.bind(function(resolve, reject) { | ||
var finalParams = _.extend({ key: this.apiKey }, params); | ||
unirest.post(this.endpoint + method) | ||
.headers({ | ||
'Accept': 'application/json', | ||
'User-Agent': this.userAgent | ||
}) | ||
.send(finalParams) | ||
.end(function (response) { | ||
if (response.code !== 200) { | ||
reject('Unable to connect to the What3Words API endpoint.'); | ||
options = { | ||
url: this.endpoint + method, | ||
qs: finalParams | ||
}; | ||
request.get(options, function (error, response, body) { | ||
if(error) { | ||
reject({code: 404, msg: error}); | ||
} else { | ||
if(response.statusCode !== 200) { | ||
reject({code: response.statusCode, msg: 'Unable to connect to the API endpoint ' + options.url}); | ||
} else if (response.body.error) { | ||
reject(response.body.error + '. Message: ' + response.body.message); | ||
reject(response.body); | ||
} | ||
if(body){ | ||
resolve(JSON.parse(response.body)); | ||
} | ||
} | ||
}); | ||
resolve(response.body); | ||
}); | ||
}, this)); | ||
}; |
The MIT License | ||
=============== | ||
Copyright (c) 2010 Daniel Leinich <leinich@gmx.net>. | ||
Copyright (c) 2014 Lokku ltd <ignacio@lokku.com>. | ||
@@ -6,0 +6,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
@@ -5,3 +5,3 @@ { | ||
"main": "index.js", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"private": false, | ||
@@ -16,5 +16,4 @@ "repository": { | ||
"dependencies": { | ||
"bluebird": "^2.5.3", | ||
"lodash": "~2.4.1", | ||
"unirest": "^0.2.7" | ||
"request": "^2.69.0" | ||
}, | ||
@@ -21,0 +20,0 @@ "devDependencies": { |
@@ -13,3 +13,3 @@ # JS.Geo.What3Words | ||
npm install what3words | ||
npm install geo.what3words | ||
@@ -21,3 +21,3 @@ If you don't have npm installed or don't want to use it: | ||
Please note that parts of this library depend on [unirest](https://github.com/Mashape/unirest-nodejs) by [Mashape](https://github.com/Mashape/). This library needs to be installed for the API to work. | ||
Please note that parts of this library depend on [request](https://github.com/request/request). This library needs to be installed for the API to work. | ||
@@ -38,4 +38,4 @@ | ||
w3w = new What3Words('YOUR_API_KEY', { | ||
language: ''ru', | ||
userAgent: ''Your custom UA' | ||
language: 'ru', | ||
userAgent: 'Your custom UA' | ||
}); | ||
@@ -94,3 +94,3 @@ ``` | ||
w3w.getLanguages({}).then(function(response) { | ||
console.log(response); // [ 'de', 'en', 'es', 'fr', 'pt', 'ru', 'sv', 'tr' ] | ||
console.log(response); // [ 'de', 'en', 'es', 'fr', 'it', 'pt', 'ru', 'sv', 'sw', 'tr' ] | ||
}); | ||
@@ -107,6 +107,6 @@ ``` | ||
All the methods return a [Bluebird](https://github.com/petkaantonov/bluebird/blob/master/API.md) promise. | ||
All the methods return a promise. | ||
## License | ||
_JS.Geo.What3Words_ is licensed under the MIT License. (See [LICENSE](https://github.com/lokku/js-geo-what3words/blob/master/LICENCe.md)) | ||
_JS.Geo.What3Words_ is licensed under the MIT License. (See [LICENSE](https://github.com/lokku/js-geo-what3words/blob/master/LICENCe.md)) |
var should = require('should'), | ||
What3Words = require('../lib/geo.what3words'), | ||
API_KEY = 'YOUR_API_KEY'; | ||
API_KEY = 'YOUR_API_KEY'; | ||
@@ -31,3 +31,3 @@ describe('What3Words API Wrapper', function(){ | ||
w3w.getUserAgent().should.be.exactly('custom').and.be.a.String; | ||
w3w.getEndpoint().should.be.exactly('http://api.what3words.com/').and.be.a.String; | ||
w3w.getEndpoint().should.be.exactly('https://api.what3words.com/').and.be.a.String; | ||
}); | ||
@@ -47,3 +47,5 @@ | ||
w3w.getLanguages({}).then(function(res) { | ||
res.should.eql(['de', 'en', 'es', 'fr', 'pt', 'ru', 'sv', 'tr']); | ||
//res.should.eql(['de', 'en', 'es', 'fr', 'it', 'pt', 'ru', 'sv', 'sw', 'tr']); | ||
//regarding evolving languages just check this is an array | ||
res.should.be.a.Array; | ||
done(); | ||
@@ -66,3 +68,3 @@ }); | ||
}).then(function(res) { | ||
res.should.eql('prom,cape,pump').and.be.a.String; | ||
res.should.eql('prom.cape.pump').and.be.a.String; | ||
done(); | ||
@@ -69,0 +71,0 @@ }); |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
12709
2
246
108
0
+ Addedrequest@^2.69.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.13.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
- Removedbluebird@^2.5.3
- Removedunirest@^0.2.7
- Removedasn1@0.1.11(transitive)
- Removedassert-plus@0.1.5(transitive)
- Removedasync@0.9.2(transitive)
- Removedaws-sign2@0.5.0(transitive)
- Removedbluebird@2.11.0(transitive)
- Removedboom@0.4.2(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removedcryptiles@0.2.2(transitive)
- Removedctype@0.5.3(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedforever-agent@0.5.2(transitive)
- Removedform-data@0.1.4(transitive)
- Removedhawk@1.1.1(transitive)
- Removedhoek@0.9.1(transitive)
- Removedhttp-signature@0.10.1(transitive)
- Removedmime@1.2.11(transitive)
- Removedmime-types@1.0.2(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removedoauth-sign@0.3.0(transitive)
- Removedqs@1.0.2(transitive)
- Removedrequest@2.40.0(transitive)
- Removedshould@2.0.2(transitive)
- Removedsntp@0.2.4(transitive)
- Removedstringstream@0.0.6(transitive)
- Removedtldts@6.1.63(transitive)
- Removedtldts-core@6.1.63(transitive)
- Removedtough-cookie@5.0.0(transitive)
- Removedtunnel-agent@0.4.3(transitive)
- Removedunirest@0.2.9(transitive)