Created
November 12, 2010 04:05
-
-
Save tarcieri/673712 to your computer and use it in GitHub Desktop.
Cool.io HTTP client example
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 '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