Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created November 12, 2010 04:05
Show Gist options
  • Save tarcieri/673712 to your computer and use it in GitHub Desktop.
Save tarcieri/673712 to your computer and use it in GitHub Desktop.
Cool.io HTTP client example
require 'rubygems'
require 'cool.io'
class MyHttpClient < Cool.io::HttpClient
def on_connect
super
STDERR.puts "Connected to #{remote_host}:#{remote_port}"
end
def on_connect_failed
super
STDERR.puts "Connection failed"
end
def on_response_header(header)
STDERR.puts "Response: #{header.http_version} #{header.status} #{header.http_reason}"
end
def on_body_data(data)
STDOUT.write data
STDOUT.flush
end
def on_request_complete
STDERR.puts "Request complete!"
end
def on_error(reason)
STDERR.puts "Error: #{reason}"
end
end
loop = Cool.io::Loop.default
client = MyHttpClient.connect("www.google.com", 80).attach(l)
client.request('GET', '/search', :query => { :q => 'foobar' })
loop.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment