Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add proxy support and fix some tests

  • Loading branch information...
commit 5bc64a6e5be98da79f43ff523d1440c23f8d2db7 1 parent b392a82
@Marsup Marsup authored
View
41 lib/googlemaps.js
@@ -1,4 +1,10 @@
-var qs = require('querystring');
+var qs = require('querystring'),
+ request = require('request');
+
+var proxy;
+exports.setProxy = function(uri) {
+ proxy = uri;
+};
// http://code.google.com/apis/maps/documentation/places/
exports.places = function(latlng, radius, key, callback, sensor, types, lang, name) {
@@ -232,22 +238,23 @@ var returnObjectFromJSON = function(callback){
// Makes the request to Google Maps API.
// If secure is true, uses https. Otherwise http is used.
var makeRequest = function(path, secure, callback, encoding){
- var protocol = (secure) ? require('https') : require('http'),
- options = { host: 'maps.googleapis.com', path: path };
+ var options = {
+ uri: (secure) ? 'https' : 'http' + '://maps.googleapis.com' + path
+ };
+ if( encoding ) {
+ options.encoding = encoding;
+ }
+ if( proxy ) {
+ options.proxy = proxy;
+ }
- protocol.get(options, function (res) {
- if( encoding ){
- res.setEncoding(encoding);
+ request(options, function (error, res, data) {
+ if( error ) {
+ return callback(error);
+ }
+ if (res.statusCode === 200) {
+ return callback(null, data);
}
- var data = "";
- var httpEnd = function(){
- if (res.statusCode == 200) callback(null, data);
- else callback(new Error("Response status code: " + res.statusCode), data);
- };
- res.on('data', function (chunk) {
- data += chunk;
- });
- res.on('end', httpEnd);
- res.on('close', httpEnd);
- }).on('error', function (err) { callback(err); });
+ return callback(new Error("Response status code: " + res.statusCode), data);
+ });
};
View
20 package.json
@@ -3,11 +3,25 @@
"version": "0.1.5",
"main": "./lib/googlemaps",
"description": "A simple way to query the Google Maps API from Node.js",
- "keywords": ["map", "geo", "api"],
+ "keywords": [
+ "map",
+ "geo",
+ "api"
+ ],
"repository": {
"type": "git",
"url": "http://github.com/moshen/node-googlemaps"
},
- "devDependencies": { "vows" : "*" },
- "engines": { "node": ">=0.3.6" }
+ "devDependencies": {
+ "vows": "*"
+ },
+ "engines": {
+ "node": ">=0.3.6"
+ },
+ "dependencies": {
+ "request": "~2.2.9"
+ },
+ "scripts": {
+ "test": "NODE_ENV=test vows test/*.js --spec"
+ }
}
View
5 test/reverseGeocode-test.js
@@ -15,7 +15,10 @@ vows.describe('reverseGeocode').addBatch({
// rely on for these tests. If I have to change it one more time,
// I'm going to just comment them all out.
'returns expected name (Pilsen)': function(err, result){
- assert.equal(result.results[0].address_components[1].long_name , 'Pilsen')
+ var locality = result.results[0].address_components.filter(function(el) {
+ return el.types.indexOf('locality') !== -1;
+ })[0];
+ assert.equal(locality.long_name , 'Chicago')
}
}
}).export(module);
View
2  test/staticmaps-test.js
@@ -53,7 +53,7 @@ vows.describe('staticmaps').addBatch({
'returns the expected static map PNG data': function(err, data){
var md5 = crypto.createHash('md5');
md5.update(data);
- assert.equal(md5.digest('hex') , 'e1c4533cd8f676f2aef58d7a28d9f773');
+ assert.equal(md5.digest('hex') , 'b601ca9a90ec103b95e13595b7e04e71');
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.