Ruby Asynchronous Net::HTTP? -


does net::http support asynchronous syntax?

i'm looking code below.

the block called on main thread after net::http has either received response server (in case,error nil) or encountered error connecting server (in case response nil).

net::http.get('http://stackoverflow.com') |response, error| if error puts "connection error: #{error.message}" elsif response.status_code != httpok puts "unexpected status code: #{response.status_code}" puts response.body else puts "success!" puts response.body end end 

the following questions provide answers i'm looking block-based solution.

have @ eventmachine , em-http-request gems. equivalent above code be

require 'rubygems' require 'eventmachine' require 'em-http' http_ok = 200 em.run http = em::httprequest.new('http://example.com').get http.errback puts "connection error: #{http.error}" em.stop end http.callback if http.response_header.status == http_ok puts "success!" puts http.response else puts "unexpected status code: #{http.response_header.status}" end em.stop end end 

there example using fibers on em-http-request github page.

edit: recommend reading http://www.igvita.com/2009/05/13/fibers-cooperative-scheduling-in-ruby/


Comments

Popular posts from this blog

JQuery Autocomplete without using label, value, id -

c++ - Accessing inactive union member and undefined behavior? -

JAVA - what is the difference between void and boolean methods? -