Module: SimpleIDN

Defined in:
lib/simpleidn.rb

Defined Under Namespace

Modules: Punycode Classes: ConversionError

Constant Summary collapse

VERSION =
"0.0.7"

Class Method Summary collapse

Class Method Details

.to_ascii(domain) ⇒ Object

Converts a UTF-8 unicode string to a punycode ACE string.

Example

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


231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/simpleidn.rb', line 231

def to_ascii(domain)
  domain_array = domain.split(".") rescue []
  return domain if domain_array.length == 0
  out = []
  i = 0
  while i < domain_array.length
    s = domain_array[i]
    out << (s =~ /[^A-Z0-9@\-*_]/i ? "xn--" + Punycode.encode(s) : s)
    i += 1
  end
  return out.join(".")
end

.to_unicode(domain) ⇒ Object

Converts a punycode ACE string to a UTF-8 unicode string.

Example

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


248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/simpleidn.rb', line 248

def to_unicode(domain)
  domain_array = domain.split(".") rescue []
  return domain if domain_array.length == 0
  out = []
  i = 0
  while i < domain_array.length
    s = domain_array[i]
    out << (s =~ /^xn\-\-/i ? Punycode.decode(s.gsub('xn--','')) : s)
    i += 1
  end
  return out.join(".")
end