Skip to content

Instantly share code, notes, and snippets.

@melborne
Created November 19, 2011 06:26
Show Gist options
  • Select an option

  • Save melborne/1378532 to your computer and use it in GitHub Desktop.

Select an option

Save melborne/1378532 to your computer and use it in GitHub Desktop.
em-http
require "em-http"
require "pp"
$stdout.sync = true
class KeyboardHandler < EM::Connection
include EM::Protocols::LineText2
def post_init
print "> "
end
def receive_line(line)
case line.strip
when /^get (.*)$/
sites = $1.chomp.split(',')
multi = EM::MultiRequest.new
sites.each_with_index do |s, i|
multi.add(i, EM::HttpRequest.new(s).get)
end
multi.callback {
puts ""
multi.responses[:callback].each do |i, h|
pp h.response_header.status
pp h.response_header
end
multi.responses[:errback].each do |h|
puts "#{h.inspect} failed"
end
print "> "
}
print "> "
when /^exit$/
EM.stop
when /^help$/
puts "get URL - get a URL"
puts "exit - exits the app"
puts "help - this help"
end
end
end
EM.run do
EM.open_keyboard(KeyboardHandler)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment