Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/google-places.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,25 @@ GooglePlaces.prototype._doRequest = function(request_query, cb) {
// Pass the requested URL as an object to the get request
https.get(request_query, function(res) {
var data = [];

if (res.statusCode >= 400) {
var e = new Error('Unexpected Google Places status code.');
e.statusCode = res.statusCode;
return cb(e);
}

res
.on('data', function(chunk) { data.push(chunk); })
.on('end', function() {
var dataBuff = data.join('').trim();
var result;
var err, result;
try {
result = JSON.parse(dataBuff);
result = JSON.parse(Buffer.concat(data).toString('utf8'));
} catch (exp) {
result = {'status_code': 500, 'status_text': 'JSON Parse Failed'};
err = new Error('Failed to parse Google Places response.');
err.err = exp;
err.data = Buffer.concat(data);
}
cb(null, result);
cb(err, result);
});
})
.on('error', function(e) {
Expand All @@ -97,12 +105,12 @@ GooglePlaces.prototype._generateUrl = function(query, method) {
//https://maps.googleapis.com/maps/api/place/search/json?
_.compact(query);
query.key = this.config.key;
return url.parse(url.format({
return url.format({
protocol: 'https',
hostname: this.config.host,
pathname: this.config.path + method + '/' + this.config.format,
query: query
}));
});
};

module.exports = GooglePlaces;
16 changes: 13 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var GooglePlaces = require('../lib/google-places'),
vows = require('vows'),
url = require('url'),
fakeweb = require('node-fakeweb'),
assert = require('assert');

Expand All @@ -21,20 +22,29 @@ fakeweb.registerUri({
body: '{"result" : {"rating": 2.5}, "status" : "OK"}'
});

fakeweb.registerUri({
uri: 'https://maps.googleapis.com/maps/api/place/details/json?reference=ABC123&sensor=false&language=en&key=bad_key',
statusCode: 200,
body: '{"error_message" : "The provided API key is invalid.", "html_attributions" : [], "status" : "REQUEST_DENIED" }'
});

vows.describe('Url generation').addBatch({
'default url': {
topic: new GooglePlaces('fake_key'),

'should have a default url for place search': function(topic) {
assert.equal(topic._generateUrl({}, 'search').href, 'https://maps.googleapis.com/maps/api/place/search/json?key=fake_key');
var targetUrl = url.parse(topic._generateUrl({}, 'search'));
assert.equal(targetUrl.href, 'https://maps.googleapis.com/maps/api/place/search/json?key=fake_key');
},

'should have a default url for place autocomplete': function(topic) {
assert.equal(topic._generateUrl({}, 'autocomplete').href, 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key=fake_key');
var targetUrl = url.parse(topic._generateUrl({}, 'autocomplete'));
assert.equal(targetUrl.href, 'https://maps.googleapis.com/maps/api/place/autocomplete/json?key=fake_key');
},

'should have my key as a query param': function(topic) {
assert.equal(topic._generateUrl({key: 'fake_key'}, 'search').query, 'key=fake_key');
var targetUrl = url.parse(topic._generateUrl({key: 'fake_key'}, 'search'));
assert.equal(targetUrl.query, 'key=fake_key');
}
}
}).export(module);
Expand Down