Created
January 31, 2010 12:06
-
-
Save maxigs/291038 to your computer and use it in GitHub Desktop.
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
# Converts URIs that go beyond the set of ASCII Charset ("Umlautdomains") | |
# into usable ones for ruby | |
# | |
# EXPERIMENTAL - but seems to work pretty good so far | |
# | |
module UTF8URI | |
# needs punycode4r gem ! | |
require 'punycode' | |
def self.encode(url) | |
scheme, uri = url.split('//') | |
parts = uri.split('/') | |
domains = parts.shift.split('.') | |
parts = parts.collect do |p| | |
URI.encode(p) | |
end | |
domains = domains.collect do |d| | |
tmp = Punycode.encode(d) | |
tmp.gsub(/-$/, '') != d ? "xn--#{tmp}" : d | |
end | |
[scheme, '//', domains.join('.'), '/', parts.join('/')].join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment