Skip to content

Instantly share code, notes, and snippets.

@buren
Last active August 8, 2016 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buren/0f3f8ede6bc64cd9c8f5 to your computer and use it in GitHub Desktop.
Save buren/0f3f8ede6bc64cd9c8f5 to your computer and use it in GitHub Desktop.
Get response given an URL and follow redirects
require 'uri'
require 'net/http'
require 'openssl'
MAX_ATTEMPTS = 2
# Get response given an URL and follow redirects
def self.url_response(uri)
found = false
url = URI.parse(uri)
attempts = 0
response = nil
until found || attempts >= MAX_ATTEMPTS
attempts += 1
http = Net::HTTP.new(url.host, url.port)
http.open_timeout = 10
http.read_timeout = 10
path = url.path
path = '/' if path.eql?('')
request = Net::HTTP::Get.new(path)
if url.instance_of?(URI::HTTPS)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
response = http.request(request)
break if response.code.eql?('200')
if not response.header['location'].nil?
new_url = URI.parse(response.header['location'])
new_url = "#{url}#{response.header['location']}" if new_url.relative?
url = new_url
else
found = true # response was 404, etc
end
end
{
url: url,
body: response.read_body
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment