Last active
August 8, 2016 15:14
-
-
Save buren/0f3f8ede6bc64cd9c8f5 to your computer and use it in GitHub Desktop.
Get response given an URL and follow redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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