Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created August 20, 2008 08:48
Show Gist options
  • Save tmm1/6344 to your computer and use it in GitHub Desktop.
Save tmm1/6344 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rev'
require 'pp'
class Google < Rev::HttpClient
def self.connect loop = Rev::Loop.default
g = super('google.com')
g.attach(loop) if loop
g
end
ALLOWED_METHODS.each do |meth|
class_eval %[
def #{meth} url, opts = {}, &blk
opts[:head] ||= {}
opts[:head]['Connection'] ||= 'keep-alive'
request :#{meth}, url, opts, &blk
end
]
end
def request *args, &blk
if @processing
(@queue ||= []) << [args, blk]
else
@processing = true
args, blk = (@queue ||= []).shift if args.empty?
@request_callback = blk
super *args if args
end
end
def on_response_header header
if @request_callback
@request_callback.call(:header, header)
else
puts
pp [:header, header]
end
end
def on_body_data data
if @request_callback
@request_callback.call(:body, data)
else
pp [:data, data.split("\n")]
end
end
def on_request_complete
if @request_callback
@request_callback.call(:complete, nil)
else
pp [:request_complete]
puts
end
end
def process_next_request
request
end
def dispatch
super
if [:finished, :invalid].include? @state
@processing = @requested = false
@method = @path = @options = nil
@request_callback = nil
@state = :response_header
@response_header = Rev::HttpResponseHeader.new
process_next_request
end
end
# def write data
# p [:write, data]
# super
# end
#
# def on_read data
# p [:read, data]
# super
# end
end
# loop = Rev::Loop.default
# client = Rev::HttpClient.connect("www.google.com").attach(loop)
# client.request(:get, '/search', :query => {:q => 'foobar'})
# loop.run
google = Google.connect
google.get '/'
google.get '/search', :query => {:q=>'ruby eventmachine'}
google.get '/search', :query => {:q=>'ruby rev'} do |type, data|
case type
when :header
puts
pp [:status=, data.status]
when :body
pp [:title=, data[/TITLE>(.+?)<\/TITLE/,1]]
end
end
Rev::Loop.default.run
__END__
[:header,
{"CACHE_CONTROL"=>"public, max-age=2592000",
"CONTENT_LENGTH"=>"219",
"EXPIRES"=>"Fri, 19 Sep 2008 09:35:58 GMT",
"DATE"=>"Wed, 20 Aug 2008 09:35:58 GMT",
"LOCATION"=>"http://www.google.com/",
"CONTENT_TYPE"=>"text/html; charset=UTF-8",
"SERVER"=>"gws"}]
[:data,
["<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">",
"<TITLE>301 Moved</TITLE></HEAD><BODY>",
"<H1>301 Moved</H1>",
"The document has moved",
"<A HREF=\"http://www.google.com/\">here</A>.\r",
"</BODY></HTML>\r"]]
[:request_complete]
[:header,
{"CACHE_CONTROL"=>"public, max-age=2592000",
"CONTENT_LENGTH"=>"245",
"EXPIRES"=>"Fri, 19 Sep 2008 09:35:59 GMT",
"DATE"=>"Wed, 20 Aug 2008 09:35:59 GMT",
"LOCATION"=>"http://www.google.com/search?q=ruby+eventmachine",
"CONTENT_TYPE"=>"text/html; charset=UTF-8",
"SERVER"=>"gws"}]
[:data,
["<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">",
"<TITLE>301 Moved</TITLE></HEAD><BODY>",
"<H1>301 Moved</H1>",
"The document has moved",
"<A HREF=\"http://www.google.com/search?q=ruby+eventmachine\">here</A>.\r",
"</BODY></HTML>\r"]]
[:request_complete]
[:status=, 301]
[:title=, "301 Moved"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment