Skip to content

Instantly share code, notes, and snippets.

@justincaldwell
Created May 15, 2012 17:43
Show Gist options
  • Save justincaldwell/2703611 to your computer and use it in GitHub Desktop.
Save justincaldwell/2703611 to your computer and use it in GitHub Desktop.
Reactor Multi
require 'goliath'
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'nokogiri'
class ReactorMulti < Goliath::API
def response(env)
sites = {
:breakfast => "http://www.breakfastsaltlakecity.com/",
:coupons => "http://www.coupons.com/"
}
# Headers for coupons.com
headers = {"user-agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19"}
multi = EventMachine::Synchrony::Multi.new
sites.each do |key, site|
multi.add key, EM::HttpRequest.new(site).aget(:head => headers)
end
multi.perform
coupon = Nokogiri::HTML(multi.responses[:callback][:coupons].response)
breakfast = Nokogiri::HTML(multi.responses[:callback][:breakfast].response)
coupon_chunk = coupon.at_css('.pod')
breakfast.at_css(".sidebar").add_child(coupon_chunk)
[200, {}, breakfast.to_html]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment