Created
November 19, 2011 06:26
-
-
Save melborne/1378532 to your computer and use it in GitHub Desktop.
em-http
This file contains hidden or 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 "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