Last active
August 29, 2015 14:10
-
-
Save samsondav/d8c242050c4b3bdb2566 to your computer and use it in GitHub Desktop.
Recursive URL looker-upper
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
# recursively looks up a URL and returns a unicode | |
def self.lookup_url(uri_str, limit = 10, old_uri = nil) | |
raise ArgumentError, 'too many HTTP redirects' if limit == 0 | |
response = Net::HTTP.get_response(URI(uri_str)) | |
case response | |
when Net::HTTPSuccess then | |
return self.unicode_parse(old_uri) | |
when Net::HTTPRedirection then | |
location = response['location'] | |
return self.lookup_url(location, limit - 1, location) | |
else | |
raise StandardError, "Could not determine qualified url, error: #{response.value}" | |
end | |
end | |
# converts punycode to unicode if necessary | |
def self.unicode_parse(uri) | |
if uri.match(/xn--/) | |
if uri.match(/https?:\/\//) | |
split_url = uri.split('://') | |
split_url[1] = SimpleIDN.to_unicode(split_url.second) | |
decoded_uri = split_url.join('://') | |
else | |
decoded_uri = SimpleIDN.to_unicode(uri) | |
end | |
return decoded_uri | |
else | |
# not punycoded | |
return uri | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment