class TCPClient::Address

Attributes

addrinfo[R]
hostname[R]

Public Class Methods

new(addr) click to toggle source
# File lib/tcp-client/address.rb, line 9
def initialize(addr)
  case addr
  when self.class
    init_from_selfclass(addr)
  when Addrinfo
    init_from_addrinfo(addr)
  when Integer
    init_from_addrinfo(Addrinfo.tcp(nil, addr))
  else
    init_from_string(addr)
  end
  @addrinfo.freeze
end

Public Instance Methods

==(other) click to toggle source
# File lib/tcp-client/address.rb, line 32
def ==(other)
  to_h == other.to_h
end
Also aliased as: eql?
eql?(other)
Alias for: ==
equal?(other) click to toggle source
# File lib/tcp-client/address.rb, line 37
def equal?(other)
  self.class == other.class && self == other
end
to_h() click to toggle source
# File lib/tcp-client/address.rb, line 28
def to_h
  { host: @hostname, port: @addrinfo.ip_port }
end
to_s() click to toggle source
# File lib/tcp-client/address.rb, line 23
def to_s
  return "[#{@hostname}]:#{@addrinfo.ip_port}" if @hostname.index(':') # IP6
  "#{@hostname}:#{@addrinfo.ip_port}"
end

Private Instance Methods

from_string(str) click to toggle source
# File lib/tcp-client/address.rb, line 59
def from_string(str)
  idx = str.rindex(':') or return nil, str.to_i
  name = str[0, idx].delete_prefix('[').delete_suffix(']')
  [name, str[idx + 1, str.size - idx].to_i]
end
init_from_addrinfo(addrinfo) click to toggle source
# File lib/tcp-client/address.rb, line 48
def init_from_addrinfo(addrinfo)
  @hostname, _port = addrinfo.getnameinfo(Socket::NI_NUMERICSERV)
  @addrinfo = addrinfo
end
init_from_selfclass(address) click to toggle source
# File lib/tcp-client/address.rb, line 43
def init_from_selfclass(address)
  @hostname = address.hostname
  @addrinfo = address.addrinfo
end
init_from_string(str) click to toggle source
# File lib/tcp-client/address.rb, line 53
def init_from_string(str)
  @hostname, port = from_string(str.to_s)
  return init_from_addrinfo(Addrinfo.tcp(nil, port)) unless @hostname
  @addrinfo = Addrinfo.tcp(@hostname, port)
end