Module: SimpleIDN
- Defined in:
- lib/simpleidn.rb
Defined Under Namespace
Modules: Punycode Classes: ConversionError
Constant Summary collapse
- VERSION =
"0.0.9"
- ACE_PREFIX =
'xn--'.encode(Encoding::UTF_8).freeze
- ASCII_MAX =
0x7F
- DOT =
0x2E.chr(Encoding::UTF_8).freeze
- LABEL_SEPERATOR_RE =
/[\u002e]/
Class Method Summary collapse
-
.to_ascii(domain) ⇒ Object
Converts a UTF-8 unicode string to a punycode ACE string.
-
.to_unicode(domain) ⇒ Object
Converts a punycode ACE string to a UTF-8 unicode string.
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"
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/simpleidn.rb', line 208 def to_ascii(domain) return nil if domain.nil? domain_array = domain.encode(Encoding::UTF_8).split(LABEL_SEPERATOR_RE) rescue [] return domain if domain_array.length == 0 out = [] domain_array.each do |s| out << (s.codepoints.any? { |cp| cp > ASCII_MAX } ? ACE_PREFIX + Punycode.encode(s) : s) end out.join(DOT).encode(domain.encoding) 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"
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/simpleidn.rb', line 223 def to_unicode(domain) return nil if domain.nil? domain_array = domain.encode(Encoding::UTF_8).split(LABEL_SEPERATOR_RE) rescue [] return domain if domain_array.length == 0 out = [] domain_array.each do |s| out << (s.downcase.start_with?(ACE_PREFIX) ? Punycode.decode(s[ACE_PREFIX.length..-1]) : s) end out.join(DOT).encode(domain.encoding) end |