Created
October 16, 2012 19:55
-
-
Save marano/3901584 to your computer and use it in GitHub Desktop.
Quick and dirty way to get link content in ruby that handles redirects and https.
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 'net/http' | |
module LinkOpener | |
def content_from_link(link) | |
uri = URI.parse(link) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if link =~ /https:/ | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
if response.code == '301' | |
new_location = response.header['Location'] | |
return content_from_link(new_location) | |
else | |
return response.body | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment