everburning

home quotes

EventMachine Screencast -- EM-HTTP-Request

15 Jul 2009

IMG_6037I decided to try my hand at creating a screencast the other day. I took a look at the EventMachine EM-HTTP-Request library and created a simple shell program to do single and multi requests. Take a look and let me know what you think.

require 'rubygems'
require 'eventmachine'
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)
    line.chomp!
    line.gsub!(/^\s+/, '')

    case(line)
    when /^get (.*)$/ then
      site = $1.chomp
      sites = site.split(',')

      multi = EM::MultiRequest.new
      sites.each do |s|
        multi.add(EM::HttpRequest.new(s).get)
      end
      multi.callback {
        puts ""
        multi.responses[:succeeded].each do |h|
          pp h.response_header.status
          pp h.response_header
        end
        multi.responses[:failed].each do |h|
          puts "#{h.inspect} failed"
        end
        print "> "
      }
      print "> "

    when /^exit$/ then
      EM.stop

    when /^help$/ then
      puts "get URL[,URL]*   - gets a URL"
      puts "exit      - exits the app"
      puts "help      - this help"
      print "> "
    end
  end
end

EM::run {
  EM.open_keyboard(KeyboardHandler)
}
puts "Finished"

Tags — Computers, EventMachine, Programming, Ruby, Screencasts, and em-http-request