class RubyIdn

Constants

CHARACTER_CODE
VERSION

Attributes

name[RW]

Public Class Methods

new(name:) click to toggle source
# File lib/ruby_idn.rb, line 8
def initialize(name:)
  @name = name
end
to_ascii(domain) click to toggle source
# File lib/ruby_idn.rb, line 25
def to_ascii(domain)
  ret_domain = to_stringprep(domain)
  call_idn(domain: ret_domain, options: '--idna-to-ascii')
end
to_nameprep(domain) click to toggle source
# File lib/ruby_idn.rb, line 39
def to_nameprep(domain)
  call_idn(domain: domain, options: ['--profile', 'Nameprep'])
end
to_stringprep(domain) click to toggle source
# File lib/ruby_idn.rb, line 35
def to_stringprep(domain)
  call_idn(domain: domain, options: '--stringprep')
end
to_unicode(domain) click to toggle source
# File lib/ruby_idn.rb, line 30
def to_unicode(domain)
  ret_domain = to_stringprep(domain)
  call_idn(domain: ret_domain, options: '--idna-to-unicode')
end

Private Class Methods

call_idn(domain:, options:) click to toggle source
# File lib/ruby_idn.rb, line 45
def call_idn(domain:, options:)
  return domain if domain.nil? || domain.empty?
  command = ["idn", options, "--quiet", domain]
  command.flatten!

  stdout, stderr, _ = Open3.capture3({'LANG' => CHARACTER_CODE}, command.shelljoin )
  unless stderr.empty?
    error = delete_unnecessary_char_for_error(stderr)
    raise IdnError, error
  end
  domain = stdout.chomp.downcase
end
delete_unnecessary_char_for_error(error_message) click to toggle source
# File lib/ruby_idn.rb, line 58
def delete_unnecessary_char_for_error(error_message)
  if error_message =~ /idn: (.*)\n/
    $1
  else
    raise UnknownIdnError, error_message
  end
end

Public Instance Methods

ascii_name() click to toggle source
# File lib/ruby_idn.rb, line 16
def ascii_name
  self.class.to_ascii(name)
end
name=(str) click to toggle source
# File lib/ruby_idn.rb, line 12
def name=(str)
  @name = str
end
unicode_name() click to toggle source
# File lib/ruby_idn.rb, line 20
def unicode_name
  self.class.to_unicode(name)
end