Skip to content

Commit 0f6eb5f

Browse files
committed
Merge pull request #2 from tiramizoo/master
Cleanup, tests and features from @gregormelhorn
2 parents 33e4224 + f8e8192 commit 0f6eb5f

File tree

5 files changed

+55
-71
lines changed

5 files changed

+55
-71
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.*.sw*
2+
node_modules
23
scrap.js

README.markdown

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ geocoder.reverseGeocode( 33.7489, -84.3789, function ( err, data ) {
2727
geocoder.reverseGeocode( 33.7489, -84.3789, function ( err, data ) {
2828
// do something with data
2929
}, { sensor: true });
30+
31+
// Setting language to German
32+
geocoder.reverseGeocode( 33.7489, -84.3789, function ( err, data ) {
33+
// do something with data
34+
}, { language: 'de' });
35+
36+
37+
3038
```
3139

3240
Results will look like standard [Google JSON Output](http://code.google.com/apis/maps/documentation/geocoding/#JSON)
3341

34-
You can pass in an optional options hash as a last argument, useful for setting sensor to true (it defaults to false).
42+
You can pass in an optional options hash as a last argument, useful for setting sensor to true (it defaults to false) and the language (default is empty which means that google geocoder will guess it by geo ip data). For details see the [Google Geocoding API Docs](http://code.google.com/intl/en-US/apis/maps/documentation/geocoding/#GeocodingRequests)
3543

3644
###Testing:
3745
Tests are written with [Vows](http://vowsjs.org/) and can be run from project root with `node test/*`.

index.js

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,15 @@
77
*/
88

99
var http = require( 'http' );
10+
var Hash = require('hashish');
11+
var querystring = require('querystring');
1012

1113
/**
1214
* Version
1315
*/
1416

1517
var version = '0.0.5';
1618

17-
/**
18-
* Formats a given `loc` to submit to Google
19-
*
20-
* @param {String} loc, required
21-
* @api private
22-
*/
23-
24-
function formatLoc ( loc ) {
25-
return loc.replace( /\s/g, '+' );
26-
}
27-
28-
/**
29-
* Combines given `objects` and returns the result
30-
*
31-
* @param objects, required
32-
* @api private
33-
*/
34-
35-
function merge ( objects ) {
36-
var result = {},
37-
args = Array.prototype.slice.call( arguments );
38-
39-
args.forEach(function ( item ) {
40-
for ( var prop in item ) {
41-
if ( item.hasOwnProperty( prop ) ) {
42-
result[prop] = item[prop];
43-
}
44-
}
45-
});
46-
47-
return result;
48-
}
49-
5019
/**
5120
* Makes request to Google API and passes result to a callback
5221
*
@@ -105,42 +74,34 @@ Geocoder.prototype = {
10574
if ( ! loc ) {
10675
return cbk( new Error( "Geocoder.geocode requires a location.") );
10776
}
108-
var sensor, defaults, options;
10977

110-
sensor = opts && opts.sensor ? opts.sensor : false;
78+
var options = Hash.merge({sensor: false, address: loc}, opts || {});
11179

112-
defaults = {
80+
var params = {
11381
host: 'maps.googleapis.com',
11482
port: 80,
115-
path: '/maps/api/geocode/json?address=' + formatLoc( loc ) + '&sensor=' + sensor,
83+
path: '/maps/api/geocode/json?' + querystring.stringify(options),
11684
headers: {}
11785
};
11886

119-
options = merge( defaults, opts || {} );
120-
121-
return request( options, cbk );
122-
87+
return request( params, cbk );
12388
},
12489

12590
reverseGeocode: function ( lat, lng, cbk, opts ) {
12691
if ( !lat || !lng ) {
12792
return cbk( new Error( "Geocoder.reverseGeocode requires a latitude and longitude." ) );
12893
}
12994

130-
var sensor, defaults, options;
95+
var options = Hash.merge({sensor: false, latlng: lat + ',' + lng}, opts || {});
13196

132-
sensor = opts && opts.sensor ? opts.sensor : false;
133-
134-
defaults = {
97+
var params = {
13598
host: 'maps.googleapis.com',
13699
port: 80,
137-
path: '/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&sensor=' + sensor,
100+
path: '/maps/api/geocode/json?' + querystring.stringify(options),
138101
headers: {}
139102
};
140103

141-
options = merge( defaults, opts || {} );
142-
143-
return request( options, cbk );
104+
return request( params, cbk );
144105

145106
},
146107

@@ -154,14 +115,8 @@ Geocoder.prototype = {
154115

155116
};
156117

157-
/**
158-
* Expose the geocoder
159-
*/
160-
161-
exports = new Geocoder();
162-
163118
/**
164119
* Export
165120
*/
166121

167-
module.exports = exports;
122+
module.exports = new Geocoder();

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"license": {
1212
"type": "Apachev2",
1313
"url": "http://www.apache.org/licenses/LICENSE-2.0"
14+
},
15+
"dependencies" : {
16+
"hashish" : ">= 0.0.4"
1417
}
1518
}

test/geocoder-test.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1-
var vows = require('vows'),
2-
assert = require('assert'),
3-
geocoder = require('../index.js');
1+
geocoder = require('../index.js');
42

3+
module.exports = {
4+
testExposeGeocodeFunction: function(test){
5+
test.equal(typeof geocoder.geocode, 'function');
6+
test.done()
7+
},
58

6-
vows.describe('Geocoder Public Interface').addBatch({
7-
'after requiring geocoder': {
8-
topic: geocoder,
9-
'geocoder exposes a geocode method': function (topic) {
10-
assert.equal(typeof geocoder.geocode, 'function');
11-
},
12-
'geocoder exposes a reverseGeocode method': function (topic) {
13-
assert.equal(typeof geocoder.reverseGeocode, 'function');
14-
}
15-
}
16-
}).run();
9+
testExposeGeocodeFunction: function(test){
10+
test.equal(typeof geocoder.geocode, 'function');
11+
test.done()
12+
},
13+
14+
testGeocode: function(test){
15+
test.expect(3);
16+
geocoder.geocode("Plattlinger Str. 10, 81479 Munich, Germany", function(err, result){
17+
test.ok(!err);
18+
test.equals('OK', result.status);
19+
test.ok(result.results[0].formatted_address.match(/Munich/));
20+
test.done();
21+
});
22+
},
23+
24+
testLanguage: function(test){
25+
test.expect(3);
26+
geocoder.geocode("Plattlinger Str. 10, 81479 München, Deutschland", function(err, result){
27+
test.ok(!err);
28+
test.equals('OK', result.status);
29+
test.ok(result.results[0].formatted_address.match(/München/));
30+
test.done();
31+
}, {language: 'de'});
32+
}
33+
}

0 commit comments

Comments
 (0)