Created
July 7, 2011 05:56
-
-
Save nolman/1068959 to your computer and use it in GitHub Desktop.
Async IO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'em-http-request' | |
require 'fiber' | |
EventMachine.run do | |
http = EM::HttpRequest.new('http://www.google.com').get | |
begin | |
puts "Before" | |
http.callback { | |
raise "Stuff Happens" | |
} | |
puts "After" | |
rescue Exception => ex | |
puts "Rescued!" | |
end | |
puts "All Done?" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'em-http-request' | |
require 'fiber' | |
EventMachine.run do | |
def syncify(url) | |
fiber = Fiber.current | |
http = EM::HttpRequest.new(url).get | |
http.callback { fiber.resume(http) } | |
Fiber.yield | |
end | |
Fiber.new { | |
begin | |
puts "Before" | |
http = syncify("http://www.google.com") | |
puts "After" | |
raise "Stuff Happens" | |
rescue Exception => ex | |
puts "Rescued!" | |
end | |
puts "All Done?" | |
}.resume | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Craigslist google maps mashup</title> | |
<script type="text/javascript" src="javascripts/jquery-1.4.4.min.js"></script> | |
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
var latlng = new google.maps.LatLng(-34.397, 150.644); | |
var reverse_geocoder = new google.maps.Geocoder(); | |
reverse_geocoder.geocode({'address': 'San Francisco CA'}, function(result){ | |
var myOptions = { | |
zoom: 8, | |
center: result[0].geometry.location, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); | |
map.setZoom(13); | |
var count = 0; | |
var add_marker = function(location, address_info, address_string){ | |
console.log(address_info) | |
var header = "<a target='new_window' href='" + address_info.requested_url + "'>" + address_string + "</a>"; | |
var marker = new google.maps.Marker({ | |
position: location, | |
map: map | |
}); | |
var infowindow = new google.maps.InfoWindow({ | |
content: header | |
}); | |
google.maps.event.addListener(marker, 'click', function(){ | |
infowindow.open(map,marker); | |
}); | |
} | |
var add_pin_points = function(new_address, address_info){ | |
count +=1; | |
setTimeout(function(){ | |
reverse_geocoder.geocode({'address': new_address}, function(address_geocoded, status){ | |
if(address_geocoded && address_geocoded[0]){ | |
var location = address_geocoded[0].geometry.location; | |
$.post("/key_value_pairs", { key: new_address, value: location.toUrlValue() }); | |
add_marker.call(this, location, address_info, new_address); | |
} | |
}); | |
}, count * 1000); | |
} | |
$.getJSON("/proxies/as_json?url=http://sfbay.craigslist.org/sfc/apa/", {'parse':{'link[]':{'path':'.//blockquote/p/a', 'attribute': 'href'}}}, function(link_response){ | |
$.each(link_response.data.link, function(index, value){ | |
$.getJSON("/proxies/as_json", {'url':value, 'parse':{'address[]':{'path':'.//comment()'}}}, function(address_info){ | |
var address_string = $.map(address_info.data.address, function(n,i){ | |
var address_portion = $.trim(n.split("=")[1]).replace(/\./, "").replace(/\//,""); | |
if (address_portion == "on"){ | |
return null; | |
} | |
return address_portion; | |
}).join(" "); | |
if(address_string && $.trim(address_string) != ""){ | |
var key = address_string; | |
$.getJSON('/key_value_pairs/' + escape(address_string), function(data){ | |
if(data['key']){ | |
var location = new google.maps.LatLng(data['value'].split(',')[0], data['value'].split(',')[1]); | |
add_marker.call(this, location, address_info, address_string) | |
} | |
else{ | |
add_pin_points.call(this, address_string, address_info); | |
} | |
}); | |
} | |
}); | |
}); | |
}); | |
}); | |
}); | |
</script> | |
<style type="text/css"> | |
html { height: 100% } | |
body { height: 100%; margin: 0px; padding: 0px } | |
#map_canvas { height: 100% } | |
</style> | |
</head> | |
<body> | |
<div id='map_canvas' style="width:100%; height:100%"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment