Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RedirectResolver
require 'uri'
require 'net/http'
class RedirectResolver
def self.resolve(uri)
uri_input = uri
if uri.is_a? String
uri = URI.parse(uri)
end
response = Net::HTTP.get_response(uri)
if response.is_a? Net::HTTPRedirection
redirected_uri = URI.parse(response['location'])
if redirected_uri.is_a? URI::Generic
redirected_uri = uri.merge(redirected_uri)
end
resolve(redirected_uri).to_s
else
uri_input
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment