Skip to content

Commit

Permalink
Switch to use Shred to support setting timeouts on requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyburke committed Apr 19, 2012
1 parent dde6729 commit 521b782
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
50 changes: 23 additions & 27 deletions lib/geo.js
Expand Up @@ -11,8 +11,9 @@
this.version = [0, 0, 1]; this.version = [0, 0, 1];


// Import HTTP to Request Geocode // Import HTTP to Request Geocode
var http = require('http');
var geohash = require('geohash'); var geohash = require('geohash');
var Shred = require( 'shred' );
var shred = new Shred();


// Google Geocode Provider // Google Geocode Provider
function GoogleGeocoder() {}; function GoogleGeocoder() {};
Expand All @@ -36,9 +37,7 @@ GoogleGeocoder.prototype.request = function(location, sensor) {
}; };


// Google Geocode Provider - Response // Google Geocode Provider - Response
GoogleGeocoder.prototype.responseHandler = function(response, callback) { GoogleGeocoder.prototype.responseHandler = function(json, callback ) {
var json = JSON.parse(response);

var _details = function (data) { var _details = function (data) {
var details = {}; var details = {};
data.address_components.map(function (d) { data.address_components.map(function (d) {
Expand All @@ -50,7 +49,7 @@ GoogleGeocoder.prototype.responseHandler = function(response, callback) {
return details; return details;
}; };


if ( json.results == null || json.results[0] == null ) { if ( json == null || json.results == null || json.results[0] == null ) {
callback(null, null, null, null); callback(null, null, null, null);
} else { } else {
callback( json.results[0].formatted_address, json.results[0].geometry.location.lat, json.results[0].geometry.location.lng, _details(json.results[0])); callback( json.results[0].formatted_address, json.results[0].geometry.location.lat, json.results[0].geometry.location.lng, _details(json.results[0]));
Expand All @@ -75,10 +74,8 @@ OSMnominatim.prototype.request = function(location, sensor) {
}; };


// OSM nominatim Provider - Response // OSM nominatim Provider - Response
OSMnominatim.prototype.responseHandler = function(response, callback) { OSMnominatim.prototype.responseHandler = function(json, callback) {
var json = JSON.parse(response); if ( json == null || json.length == null || json[0] == null ) {

if ( json.length == null || json[0] == null ) {
callback(null, null, null, null); callback(null, null, null, null);
} else { } else {
callback( callback(
Expand Down Expand Up @@ -113,7 +110,7 @@ Geo.prototype.init = function() {
}; };


// Get Geocode // Get Geocode
Geo.prototype.geocoder = function(geocoder, location, sensor, callback) { Geo.prototype.geocoder = function(geocoder, location, sensor, callback, timeout) {
// Check Input // Check Input
if ( geocoder == null ) { if ( geocoder == null ) {
throw new Error("Invalid Geocoder"); throw new Error("Invalid Geocoder");
Expand All @@ -132,23 +129,22 @@ Geo.prototype.geocoder = function(geocoder, location, sensor, callback) {
var options = geocoder.request(location, sensor); var options = geocoder.request(location, sensor);


// Make Request // Make Request
var connection = http.createClient(options.port, options.host); shred.get({
var request = connection.request("GET", options.path); timeout: timeout ? timeout : ( 60 * 1000 ),

url: "http://" + options.host + ":" + options.port + options.path,
// Handle Response on: {
request.addListener("response", function (response) { response: function( response ) {
var responseBody = ""; geocoder.responseHandler( response.content.data, callback );
response.addListener("data", function(chunk) { },
responseBody += chunk; error: function( response ) {
}); geocoder.responseHandler( null, callback );
response.addListener("end", function() { },
geocoder.responseHandler(responseBody, callback); timeout: function() {
}); geocoder.responseHandler( null, callback );
}); }

}
// End Request - Listener will take care of Response });
request.end();

}; };


// Get GeoHash // Get GeoHash
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,10 @@
"url": "git@github.com:feliperazeek/geonode.git" "url": "git@github.com:feliperazeek/geonode.git"
}, },
"licenses" : ["MIT"], "licenses" : ["MIT"],
"dependencies" : ["geohash"], "dependencies": {
"geohash": "0.0.x",
"shred": "0.7.x"
},
"lib" : "lib", "lib" : "lib",
"main" : "./lib/geo", "main" : "./lib/geo",
"version" : "0.0.3", "version" : "0.0.3",
Expand Down

0 comments on commit 521b782

Please sign in to comment.