geo.what3words
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,4 +0,5 @@ | ||
var http = require('http'), | ||
unirest = require('unirest'), | ||
_ = require('lodash'); | ||
var http = require('http'), | ||
unirest = require('unirest'), | ||
Promise = require('bluebird'), | ||
_ = require('lodash'); | ||
@@ -42,9 +43,15 @@ /** | ||
this.execute('oneword-available', {word: options.word, lang: language}, function(response) { | ||
if (options.full) { | ||
callback(response); | ||
} | ||
callback(response.available); | ||
}); | ||
return new Promise(_.bind(function(resolve, reject) { | ||
this.execute('oneword-available', {word: options.word, lang: language}) | ||
.then(function(res) { | ||
if (options.full) { | ||
resolve(res); | ||
} else { | ||
resolve(res.available); | ||
} | ||
}) | ||
.catch(function(err) { | ||
reject(err) | ||
}); | ||
}, this)); | ||
}; | ||
@@ -63,10 +70,15 @@ | ||
this.execute('position', {position: options.position, lang: language}, function(response) { | ||
if (options.full) { | ||
callback(response); | ||
} | ||
callback(response.words.join('.')); | ||
}); | ||
return new Promise(_.bind(function(resolve, reject) { | ||
this.execute('position', {position: options.position, lang: language}) | ||
.then(function(res) { | ||
if (options.full) { | ||
resolve(res); | ||
} else { | ||
resolve(res.words.join(',')); | ||
} | ||
}) | ||
.catch(function(err) { | ||
reject(err) | ||
}); | ||
}, this)); | ||
}; | ||
@@ -85,10 +97,15 @@ | ||
this.execute('w3w', {string: options.words, lang: language}, function(response) { | ||
if (options.full) { | ||
callback(response); | ||
} | ||
callback(response.position.join(',')); | ||
}); | ||
return new Promise(_.bind(function(resolve, reject) { | ||
this.execute('w3w', {string: options.words, lang: language}) | ||
.then(function(res) { | ||
if (options.full) { | ||
resolve(res); | ||
} else { | ||
resolve(res.position.join(',')); | ||
} | ||
}) | ||
.catch(function(err) { | ||
reject(err) | ||
}); | ||
}, this)); | ||
}; | ||
@@ -102,12 +119,16 @@ | ||
*/ | ||
What3Words.prototype.getLanguages = function (options, callback) { | ||
this.execute('get-languages', {}, function(response) { | ||
if (options.full) { | ||
callback(response); | ||
return 1; | ||
} | ||
callback(_.pluck(response.languages, 'code')); | ||
}); | ||
What3Words.prototype.getLanguages = function (options) { | ||
return new Promise(_.bind(function(resolve, reject) { | ||
this.execute('get-languages', {}) | ||
.then(function(res) { | ||
if (options.full) { | ||
resolve(res); | ||
} else { | ||
resolve(_.pluck(res.languages, 'code')); | ||
} | ||
}) | ||
.catch(function(err) { | ||
reject(err); | ||
}); | ||
}, this)); | ||
}; | ||
@@ -139,20 +160,22 @@ | ||
*/ | ||
What3Words.prototype.execute = function (method, params, callback) { | ||
var finalParams = _.extend({ key: this.apiKey }, params); | ||
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) { | ||
throw new Error('Unable to connect to the What3Words API endpoint.'); | ||
} else if (response.body.error) { | ||
throw new Error(response.body.error + '. Message: ' + response.body.message); | ||
} | ||
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.'); | ||
} else if (response.body.error) { | ||
reject(response.body.error + '. Message: ' + response.body.message); | ||
} | ||
callback(response.body); | ||
}); | ||
resolve(response.body); | ||
}); | ||
}, this)); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"main": "index.js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"private": false, | ||
@@ -16,2 +16,3 @@ "repository": { | ||
"dependencies": { | ||
"bluebird": "^2.5.3", | ||
"lodash": "~2.4.1", | ||
@@ -18,0 +19,0 @@ "unirest": "^0.2.7" |
@@ -7,3 +7,3 @@ # JS.Geo.What3Words | ||
Further information on the What3Words API and its features is available at [http://what3words.com/api/reference](http://what3words.com/api/reference). | ||
Further information on the What3Words API and its features is available at [http://what3words.com/api/reference](http://what3words.com/api/reference). | ||
@@ -15,3 +15,3 @@ ## Installation | ||
npm install what3words | ||
If you don't have npm installed or don't want to use it: | ||
@@ -47,4 +47,6 @@ | ||
words: 'prom.cape.pump' | ||
}, function(response) { | ||
console.log(response); // 51.484463,-0.195405 | ||
}).then(function(response) { | ||
console.log(response); // 51.484463,-0.195405 | ||
}).catch(function(err) { | ||
console.log(err); | ||
}); | ||
@@ -62,3 +64,3 @@ ``` | ||
position: '51.484463,-0.195405' | ||
}, function(response) { | ||
}).then(function(response) { | ||
@@ -78,3 +80,3 @@ console.log(response); //prom.cape.pump | ||
word: 'nestoria' | ||
}, function(response) { | ||
}).then(function(response) { | ||
@@ -93,3 +95,3 @@ console.log(response); // 1 | ||
```javascript | ||
w3w.getLanguages({}, function(response) { | ||
w3w.getLanguages({}).then(function(response) { | ||
console.log(response); // [ 'de', 'en', 'es', 'fr', 'pt', 'ru', 'sv', 'tr' ] | ||
@@ -102,7 +104,11 @@ }); | ||
* _full_ returns the full response of the api | ||
* _lang_ sets a different language for the response | ||
* _lang_ sets a different language for the response | ||
### Errors ### | ||
All the methods return a [Bluebird](https://github.com/petkaantonov/bluebird/blob/master/API.md) promise. | ||
## License | ||
_JS.Geo.What3Words_ is licensed under the MIT License. (See [LICENSE](https://github.com/lokku/js-geo-what3words/blob/master/LICENCe.md)) |
@@ -19,3 +19,3 @@ var should = require('should'), | ||
w3w = new What3Words(API_KEY); | ||
w3w.getLanguages({}, function(r) {}); | ||
w3w.getLanguages({}); | ||
}).should.not.throw(); | ||
@@ -46,3 +46,3 @@ }); | ||
it('should return all languages', function(done) { | ||
w3w.getLanguages({}, function(res) { | ||
w3w.getLanguages({}).then(function(res) { | ||
res.should.eql(['de', 'en', 'es', 'fr', 'pt', 'ru', 'sv', 'tr']); | ||
@@ -56,3 +56,3 @@ done(); | ||
words: 'prom.cape.pump' | ||
}, function(res) { | ||
}).then(function(res) { | ||
res.should.eql('51.484463,-0.195405').and.be.a.String; | ||
@@ -66,4 +66,4 @@ done(); | ||
position: '51.484463,-0.195405' | ||
}, function(res) { | ||
res.should.eql('prom.cape.pump').and.be.a.String; | ||
}).then(function(res) { | ||
res.should.eql('prom,cape,pump').and.be.a.String; | ||
done(); | ||
@@ -76,3 +76,3 @@ }); | ||
word: 'nestoria' | ||
}, function(res) { | ||
}).then(function(res) { | ||
res.should.eql('1').and.be.a.Number; | ||
@@ -79,0 +79,0 @@ done(); |
12088
218
107
3
+ Addedbluebird@^2.5.3
+ Addedbluebird@2.11.0(transitive)