Does Net::HTTP
support an asynchronous syntax?
I'm looking for something like the code below.
The block would get called on the main thread after Net::HTTP
has either received the response from the server (in which case,error
would be nil
) or encountered an error connecting to the server (in which case response
would be nil
).
Net::HTTP.get('http://stackoverflow.com') do |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 but I'm looking for a block-based solution.