Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add *_idn methods that do ToUnicode conversion.

  • Loading branch information...
commit 212b08e432cd28f7706fd122c2d5a1da7bc4ad61 1 parent 92316a9
Akinori MUSHA authored April 17, 2013
41  lib/domain_name.rb
@@ -20,6 +20,10 @@ class DomainName
20 20
   # suitable for opening a connection to.
21 21
   attr_reader :hostname
22 22
 
  23
+  # The Unicode representation of the #hostname property.
  24
+  #
  25
+  # :attr_reader: hostname_idn
  26
+
23 27
   # The least "universally original" domain part of this domain name.
24 28
   # For example, "example.co.uk" for "www.sub.example.co.uk".  This
25 29
   # may be nil if the hostname does not have one, like when it is an
@@ -27,6 +31,10 @@ class DomainName
27 31
   # non-canonical domain.
28 32
   attr_reader :domain
29 33
 
  34
+  # The Unicode representation of the #domain property.
  35
+  #
  36
+  # :attr_reader: domain_idn
  37
+
30 38
   # The TLD part of this domain name.  For example, if the hostname is
31 39
   # "www.sub.example.co.uk", the TLD part is "uk".  This property is
32 40
   # nil only if +ipaddr?+ is true.  This may be nil if the hostname
@@ -34,6 +42,10 @@ class DomainName
34 42
   # non-canonical domain.
35 43
   attr_reader :tld
36 44
 
  45
+  # The Unicode representation of the #tld property.
  46
+  #
  47
+  # :attr_reader: tld_idn
  48
+
37 49
   # Returns an IPAddr object if this is an IP address.
38 50
   attr_reader :ipaddr
39 51
 
@@ -230,6 +242,35 @@ def to_s
230 242
 
231 243
   alias to_str to_s
232 244
 
  245
+  def hostname_idn
  246
+    @hostname_idn ||=
  247
+      if @ipaddr
  248
+        @hostname
  249
+      else
  250
+        DomainName::Punycode.decode_hostname(@hostname)
  251
+      end
  252
+  end
  253
+
  254
+  alias idn hostname_idn
  255
+
  256
+  def domain_idn
  257
+    @domain_idn ||=
  258
+      if @ipaddr
  259
+        @domain
  260
+      else
  261
+        DomainName::Punycode.decode_hostname(@domain)
  262
+      end
  263
+  end
  264
+
  265
+  def tld_idn
  266
+    @tld_idn ||=
  267
+      if @ipaddr
  268
+        @tld
  269
+      else
  270
+        DomainName::Punycode.decode_hostname(@tld)
  271
+      end
  272
+  end
  273
+
233 274
   def inspect
234 275
     str = '#<%s:%s' % [self.class.name, @hostname]
235 276
     if @ipaddr
12  test/test_domain_name.rb
... ...
@@ -1,3 +1,4 @@
  1
+# -*- coding: utf-8 -*-
1 2
 require 'helper'
2 3
 require 'ipaddr'
3 4
 
@@ -282,4 +283,15 @@ def obj.to_str
282 283
       assert_equal nil, dn.superdomain
283 284
     }
284 285
   end
  286
+
  287
+  should "have idn methods" do
  288
+    dn = DomainName("金八先生.B組.3年.日本語ドメイン名Example.日本")
  289
+
  290
+    assert_equal "xn--44q1cv48kq8x.xn--b-gf6c.xn--3-pj3b.xn--example-6q4fyliikhk162btq3b2zd4y2o.xn--wgv71a", dn.hostname
  291
+    assert_equal "金八先生.b組.3年.日本語ドメイン名example.日本", dn.hostname_idn
  292
+    assert_equal "xn--example-6q4fyliikhk162btq3b2zd4y2o.xn--wgv71a", dn.domain
  293
+    assert_equal "日本語ドメイン名example.日本", dn.domain_idn
  294
+    assert_equal "xn--wgv71a", dn.tld
  295
+    assert_equal "日本", dn.tld_idn
  296
+  end
285 297
 end

0 notes on commit 212b08e

Please sign in to comment.
Something went wrong with that request. Please try again.