Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby convert IDN domain from Punycode to Unicode

I'm writing a Rails app that needs to convert an IDN domain name from Punycode into its Unicode equivalent. I tried installing the idn gem that has bindings to GNU LibIDN, but it won't compile the native code. Apparently others have the same issue with Ruby 1.9.x.

I also tried the pure Ruby SimpleIDN gem, but I would prefer something native.

like image 840
Abe Voelker Avatar asked Jun 01 '11 04:06

Abe Voelker


People also ask

Is IDN domain please convert to Punycode first?

You can currently register a .com or . net internationalized domain name. However, to be able to register an IDN, you must first convert it to Punycode. Punycode is a method for transcribing multi-byte Unicode characters into standard ASCII characters.

Where is Punycode used?

By default, many web browsers use Punycode encoding to represent unicode characters in the URL to defend against Homograph phishing attacks (where the website address looks legitimate, but is not, because a character or characters have been replaced deceptively with Unicode characters).

What is the Punycode URL of the domain?

What is punycode? Punycode is a way to represent International Domain Names (IDNs) with the limited character set (A-Z, 0-9) supported by the domain name system. For example, "münich" would be encoded as "mnich-kva". An IDN takes the punycode encoding, and adds a "xn--" in front of it.


2 Answers

Try the simpleidn gem. It works with Ruby 1.8.7 and 1.9.2.

Edit your Gemfile:

gem 'simpleidn'

then you can enter the command as follows:

SimpleIDN.to_unicode("xn--mllerriis-l8a.com")
=> "møllerriis.com"

SimpleIDN.to_ascii("møllerriis.com")
=> "xn--mllerriis-l8a.com"
like image 115
Leo Grove Avatar answered Sep 22 '22 11:09

Leo Grove


Whoops - looks like I found a capable answer shortly after posting (sorry). There is a subtly placed patch from 09/2010 in the bug reports section of the project's RubyForge page. Adding this to my Gemfile now allows me to use the idn library:

gem 'idn', '~> 0.0.2', :git => 'git://github.com/mihu/idn'

Too bad that the gem is apparently abandoned :/

like image 36
Abe Voelker Avatar answered Sep 21 '22 11:09

Abe Voelker