diff --git a/domain_name.gemspec b/domain_name.gemspec index 7ac4c82..3772f14 100644 --- a/domain_name.gemspec +++ b/domain_name.gemspec @@ -28,7 +28,7 @@ Suffix List. "README.md" ] - gem.add_runtime_dependency("unf", ["< 1.0.0", ">= 0.0.5"]) + gem.add_runtime_dependency("unf", ["< 1.0.0", ">= 0.0.5"]) if RUBY_VERSION < "2.2" gem.add_development_dependency("test-unit", "~> 2.5.5") gem.add_development_dependency("bundler", [">= 1.2.0"]) gem.add_development_dependency("rake", [">= 0.9.2.2", *("< 11" if RUBY_VERSION < "1.9")]) diff --git a/lib/domain_name.rb b/lib/domain_name.rb index a22b028..3f4ce79 100644 --- a/lib/domain_name.rb +++ b/lib/domain_name.rb @@ -8,8 +8,12 @@ require 'domain_name/version' require 'domain_name/punycode' require 'domain_name/etld_data' -require 'unf' require 'ipaddr' +if RUBY_VERSION < '2.2' + require 'unf' +else + require 'unicode_normalize/normalize' +end # Represents a domain name ready for extracting its registered domain # and TLD. @@ -285,8 +289,14 @@ def inspect class << self # Normalizes a _domain_ using the Punycode algorithm as necessary. # The result will be a downcased, ASCII-only string. - def normalize(domain) - DomainName::Punycode.encode_hostname(domain.chomp(DOT).to_nfc).downcase + if RUBY_VERSION >= '2.2' + def normalize(domain) + DomainName::Punycode.encode_hostname(domain.chomp(DOT).unicode_normalize).downcase + end + else + def normalize(domain) + DomainName::Punycode.encode_hostname(domain.chomp(DOT).to_nfc).downcase + end end end end diff --git a/test/test_domain_name-punycode.rb b/test/test_domain_name-punycode.rb index 5e601f2..ca65f91 100644 --- a/test/test_domain_name-punycode.rb +++ b/test/test_domain_name-punycode.rb @@ -91,7 +91,12 @@ class TestDomainName < Test::Unit::TestCase '-> $1.00 <--'] ].each { |title, cps, punycode| assert_equal punycode, DomainName::Punycode.encode(cps.pack('U*')), title - assert_equal cps.pack('U*').to_nfc, DomainName::Punycode.decode(punycode), title + cps_norm = if RUBY_VERSION >= '2.2' + cps.pack('U*').unicode_normalize + else + cps.pack('U*').to_nfc + end + assert_equal cps_norm, DomainName::Punycode.decode(punycode), title } end end