How can I handle 503 errors with open-uri? How can I handle 503 errors with open-uri? ruby ruby

How can I handle 503 errors with open-uri?


OpenURI::HTTPError have an io attribute you can inspect to get what you want. io is a StringIO object with several singleton methods defined on it (status for example):

require 'open-uri'begin  open('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8')rescue OpenURI::HTTPError => error  response = error.io  response.status  # => ["503", "Service Unavailable"]   response.string  # => <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html DIR=\"LTR\">\n<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><meta name=\"viewport\" content=\"initial-scale=1\">...end    

However for this task the Net::HTTP module is probably a better alternative:

require 'net/http'response = Net::HTTP.get_response(URI.parse('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8'))response.code# => "503"response.body# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html DIR=\"LTR\">\n<head><meta http-equiv=\"content-type\" content=\"text/html; ...