Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

geo.what3words

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geo.what3words - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

90

lib/geo.what3words.js

@@ -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 @@ });

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc