Skip to content

Instantly share code, notes, and snippets.

@samsondav
Last active August 29, 2015 14:10
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 samsondav/d8c242050c4b3bdb2566 to your computer and use it in GitHub Desktop.
Save samsondav/d8c242050c4b3bdb2566 to your computer and use it in GitHub Desktop.
Recursive URL looker-upper
# 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