node-geocoder
Advanced tools
Comparing version 0.0.5 to 0.1.0
(function () { | ||
var Geocoder = function(geocoder) { | ||
this.geocoder = geocoder; | ||
var Geocoder = function (geocoder, formatter) { | ||
this._geocoder = geocoder; | ||
this._formatter = formatter; | ||
}; | ||
Geocoder.prototype.geocode = function(value, callback) { | ||
this.geocoder.geocode(value, callback); | ||
Geocoder.prototype.geocode = function (value, callback) { | ||
var _this = this; | ||
this._geocoder.geocode(value, function(err, data) { _this._format(err, data, callback); }); | ||
}; | ||
Geocoder.prototype.reverse = function(lat, long, callback) { | ||
this.geocoder.reverse(lat, long, callback); | ||
Geocoder.prototype.reverse = function (lat, long, callback) { | ||
var _this = this; | ||
this._geocoder.reverse(lat, long, function(err, data) { _this._format(err, data, callback); }); | ||
}; | ||
Geocoder.prototype._format = function (err, data, callback) { | ||
if (err) { | ||
callback(err, data); | ||
return; | ||
} | ||
if (this._formatter && this._formatter !== 'undefined') { | ||
data = this._formatter.format(data); | ||
} | ||
callback(err, data); | ||
}; | ||
module.exports = Geocoder; | ||
}()); |
@@ -45,4 +45,11 @@ (function() { | ||
}, | ||
getGeocoder: function(geocoderAdapter, httpAdapter, geocoderOpts) { | ||
_getFormatter: function(formatterName) { | ||
if (formatterName === 'gpx') { | ||
var GpxFormatter = new require('./formatter/gpxformatter.js'); | ||
return new GpxFormatter(); | ||
} | ||
}, | ||
getGeocoder: function(geocoderAdapter, httpAdapter, formatter) { | ||
if (!httpAdapter || httpAdapter === 'undefined') { | ||
@@ -59,4 +66,8 @@ httpAdapter = 'http'; | ||
} | ||
if (Helper.isString(formatter)) { | ||
formatter = this._getFormatter(formatter); | ||
} | ||
var geocoder = new Geocoder(geocoderAdapter); | ||
var geocoder = new Geocoder(geocoderAdapter, formatter); | ||
@@ -63,0 +74,0 @@ return geocoder; |
{ | ||
"name": "node-geocoder", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "node geocoding library", | ||
@@ -5,0 +5,0 @@ "main": "lib/geocoderfactory.js", |
@@ -26,3 +26,6 @@ # node-geocoder | ||
## Formatter | ||
* `gpx` : format result using GPX format | ||
## Usage example | ||
@@ -32,4 +35,5 @@ | ||
var httpAdapter = 'http'; | ||
var formatter = null; | ||
var geocoder = require('node-geocoder')(geocoderProvider, httpAdapter); | ||
var geocoder = require('node-geocoder')(geocoderProvider, httpAdapter, formatter); | ||
@@ -36,0 +40,0 @@ geocoder.geocode('29 champs elysée paris', function(err, res) { |
@@ -7,7 +7,6 @@ var GoogleAdapter = require ('./lib/geocoder/googlegeocoder.js'); | ||
var adapter = GeocoderFactory.getGeocoder('google', 'http'); | ||
var adapter = GeocoderFactory.getGeocoder('google', 'http', 'gpx'); | ||
//var adapter = new GoogleAdapter(new RequestifyAdapter()); | ||
var adapter = require ('./index.js')('openstreetmap', 'requestify'); | ||
adapter.reverse(48.8698679,2.3072976, function (err, res) { | ||
@@ -14,0 +13,0 @@ console.log(err); |
@@ -8,3 +8,2 @@ (function() { | ||
var DataScienceToolkitGeocoder = require('../../lib/geocoder/datasciencetoolkitgeocoder.js'); | ||
var HttpAdapter = require('../../lib/httpadapter/httpadapter.js'); | ||
@@ -59,3 +58,14 @@ var mockedHttpAdapter = { | ||
var geocoder = new DataScienceToolkitGeocoder(new HttpAdapter()); | ||
var mock = sinon.mock(mockedHttpAdapter); | ||
mock.expects('get').once().callsArgWith(2, false, { | ||
'66.249.64.0' : { | ||
latitude: 37.386, | ||
longitude: -122.0838, | ||
country_code: 'US', | ||
country_name: 'United States', | ||
locality: 'Mountain View', | ||
} | ||
} | ||
); | ||
var geocoder = new DataScienceToolkitGeocoder(mockedHttpAdapter); | ||
@@ -65,4 +75,4 @@ geocoder.geocode('66.249.64.0', function(err, results) { | ||
results[0].should.to.deep.equal({ | ||
"latitude": 37.4192008972168, | ||
"longitude": -122.057403564453, | ||
"latitude": 37.386, | ||
"longitude": -122.0838, | ||
"country": "United States", | ||
@@ -75,3 +85,3 @@ "city": "Mountain View", | ||
}); | ||
mock.verify(); | ||
done(); | ||
@@ -85,4 +95,3 @@ }); | ||
it('Should throw an error', function() { | ||
var geocoder = new DataScienceToolkitGeocoder(mockedHttpAdapter); | ||
var geocoder = new DataScienceToolkitGeocoder(mockedHttpAdapter); | ||
expect(function() {geocoder.reverse(10.0235,-2.3662);}) | ||
@@ -89,0 +98,0 @@ .to |
@@ -8,3 +8,2 @@ (function() { | ||
var FreegeoipGeocoder = require('../../lib/geocoder/freegeoipgeocoder.js'); | ||
var HttpAdapter = require('../../lib/httpadapter/httpadapter.js'); | ||
@@ -58,6 +57,14 @@ var mockedHttpAdapter = { | ||
it('Should return a geocoded adress', function(done) { | ||
this.timeout(100000); | ||
var mock = sinon.mock(mockedHttpAdapter); | ||
mock.expects('get').once().callsArgWith(2, false, { | ||
latitude: 37.386, | ||
longitude: -122.0838, | ||
country_code: 'US', | ||
country_name: 'United States', | ||
city: 'Mountain View', | ||
} | ||
); | ||
var freegeoipgeocoder = new FreegeoipGeocoder(mockedHttpAdapter); | ||
var freegeoipgeocoder = new FreegeoipGeocoder(new HttpAdapter()); | ||
freegeoipgeocoder.geocode('66.249.64.0', function(err, results) { | ||
@@ -75,3 +82,3 @@ err.should.to.equal(false); | ||
}); | ||
mock.verify(); | ||
done(); | ||
@@ -78,0 +85,0 @@ }); |
@@ -67,9 +67,28 @@ (function() { | ||
it('Should return geocoded adress', function(done) { | ||
var googleAdapter = new GoogleGeocoder(new HttpAdapter()); | ||
var mock = sinon.mock(mockedHttpAdapter); | ||
mock.expects('get').once().callsArgWith(2, false, { results: [{ | ||
geometry: {location : { | ||
lat: 37.386, | ||
lng: -122.0838 | ||
}}, | ||
address_components: [ | ||
{types: ['country'], long_name: 'France', short_name: 'Fr' }, | ||
{types: ['locality'], long_name: 'Paris' }, | ||
{types: ['postal_code'], long_name: '75008' }, | ||
{types: ['route'], long_name: 'Champs-Élysées' }, | ||
{types: ['street_number'], long_name: '1' } | ||
], | ||
country_code: 'US', | ||
country_name: 'United States', | ||
locality: 'Mountain View', | ||
}]} | ||
); | ||
var googleAdapter = new GoogleGeocoder(mockedHttpAdapter); | ||
googleAdapter.geocode('1 champs élysées Paris', function(err, results) { | ||
err.should.to.equal(false); | ||
results[0].should.to.deep.equal({ | ||
"latitude": 48.869261, | ||
"longitude": 2.3091644, | ||
"latitude": 37.386, | ||
"longitude": -122.0838, | ||
"country": "France", | ||
@@ -80,5 +99,5 @@ "city": "Paris", | ||
"streetNumber": "1", | ||
"countryCode": "FR" | ||
"countryCode": "Fr" | ||
}); | ||
mock.verify(); | ||
done(); | ||
@@ -108,3 +127,22 @@ }); | ||
it('Should return geocoded adress', function(done) { | ||
var googleAdapter = new GoogleGeocoder(new HttpAdapter()); | ||
var mock = sinon.mock(mockedHttpAdapter); | ||
mock.expects('get').once().callsArgWith(2, false, { results: [{ | ||
geometry: {location : { | ||
lat: 40.714232, | ||
lng: -73.9612889 | ||
}}, | ||
address_components: [ | ||
{types: ['country'], long_name: 'United States', short_name: 'US' }, | ||
{types: ['locality'], long_name: 'Brooklyn' }, | ||
{types: ['postal_code'], long_name: '11211' }, | ||
{types: ['route'], long_name: 'Bedford Avenue' }, | ||
{types: ['street_number'], long_name: '277' } | ||
], | ||
country_code: 'US', | ||
country_name: 'United States', | ||
locality: 'Mountain View', | ||
}]} | ||
); | ||
var googleAdapter = new GoogleGeocoder(mockedHttpAdapter); | ||
googleAdapter.reverse(40.714232,-73.9612889, function(err, results) { | ||
@@ -122,3 +160,3 @@ err.should.to.equal(false); | ||
}); | ||
mock.verify(); | ||
done(); | ||
@@ -125,0 +163,0 @@ }); |
@@ -10,2 +10,3 @@ (function() { | ||
var DataScienceToolkitGeocoder = require('../lib/geocoder/datasciencetoolkitgeocoder.js'); | ||
var OpenStreetMapGeocoder = require('../lib/geocoder/openstreetmapgeocoder.js'); | ||
@@ -15,2 +16,4 @@ var RequestifyAdapter = require('../lib/httpadapter/requestifyadapter.js'); | ||
var GpxFormatter = require('../lib/formatter/gpxformatter.js'); | ||
describe('GeocoderFactory', function() { | ||
@@ -22,3 +25,3 @@ | ||
var geocoderAdapter = geocoder.geocoder; | ||
var geocoderAdapter = geocoder._geocoder; | ||
@@ -29,9 +32,11 @@ geocoderAdapter.should.be.instanceof(GoogleGeocoder); | ||
it('called with "google" and "http" must return google geocoder with http adapter', function() { | ||
var geocoder = GeocoderFactory.getGeocoder('google', 'http'); | ||
it('called with "google" and "http" and "gpx" must return google geocoder with http adapter and gpx formatter', function() { | ||
var geocoder = GeocoderFactory.getGeocoder('google', 'http', 'gpx'); | ||
var geocoderAdapter = geocoder.geocoder; | ||
var geocoderAdapter = geocoder._geocoder; | ||
var formatter = geocoder._formatter; | ||
geocoderAdapter.should.be.instanceof(GoogleGeocoder); | ||
geocoderAdapter.httpAdapter.should.be.instanceof(HttpAdapter); | ||
formatter.should.be.instanceof(GpxFormatter); | ||
}); | ||
@@ -42,3 +47,3 @@ | ||
var geocoderAdapter = geocoder.geocoder; | ||
var geocoderAdapter = geocoder._geocoder; | ||
@@ -52,3 +57,3 @@ geocoderAdapter.should.be.instanceof(GoogleGeocoder); | ||
var geocoderAdapter = geocoder.geocoder; | ||
var geocoderAdapter = geocoder._geocoder; | ||
@@ -58,5 +63,14 @@ geocoderAdapter.should.be.instanceof(DataScienceToolkitGeocoder); | ||
}); | ||
it('called with "openstreetmap" and "http" must return openstreetmap geocoder with http adapter', function() { | ||
var geocoder = GeocoderFactory.getGeocoder('openstreetmap', 'http'); | ||
var geocoderAdapter = geocoder._geocoder; | ||
geocoderAdapter.should.be.instanceof(OpenStreetMapGeocoder); | ||
geocoderAdapter.httpAdapter.should.be.instanceof(HttpAdapter); | ||
}); | ||
}); | ||
}); | ||
})(); | ||
})(); |
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
42892
26
919
78
7