Skip to content

Instantly share code, notes, and snippets.

@maxigs
Created January 31, 2010 12:06
Show Gist options
  • Save maxigs/291038 to your computer and use it in GitHub Desktop.
Save maxigs/291038 to your computer and use it in GitHub Desktop.
# 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