Class: TCPClient::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/tcp-client/address.rb

Overview

Note:

An Address does not resolve the required TCP information until it is needed.

This means that address resolution only occurs when an instance attribute is accessed or the address is frozen. To force the address resolution at a certain time, #freeze can be called.

The address used by a TCPClient.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr) ⇒ Address #initialize(addrinfo) ⇒ Address #initialize(port) ⇒ Address

Initializes an address

Overloads:

  • #initialize(addr) ⇒ Address

    The addr can be specified as

    • a valid named address containing the port like "my.host.test:80"
    • a valid TCPv4 address like "142.250.181.206:80"
    • a valid TCPv6 address like "[2001:16b8:5093:3500:ad77:abe6:eb88:47b6]:80"

    Examples:

    create an Address instance with a host name and port

    Address.new('www.google.com:80')

    Parameters:

    • addr (String)

      address containing host and port name

  • #initialize(addrinfo) ⇒ Address

    Examples:

    create an Address with an Addrinfo

    Address.new(Addrinfo.tcp('www.google.com', 'http'))

    Parameters:

    • addrinfo (Addrinfo)

      containing the addressed host and port

  • #initialize(port) ⇒ Address

    Addresses the port on the local machine.

    Examples:

    create an Address for localhost on port 80

    Address.new(80)

    Parameters:

    • port (Integer)

      the addressed port



74
# File 'lib/tcp-client/address.rb', line 74

def initialize(addr) = (@addr = addr)

Instance Attribute Details

#addrinfoAddrinfo (readonly)

Returns the address info.

Returns:

  • (Addrinfo)

    the address info



21
22
23
24
# File 'lib/tcp-client/address.rb', line 21

def addrinfo
  freeze unless @addrinfo
  @addrinfo
end

#hostString (readonly) Also known as: hostname

Returns the host name.

Returns:

  • (String)

    the host name



30
31
32
33
# File 'lib/tcp-client/address.rb', line 30

def host
  freeze unless @host
  @host
end

#portInteger (readonly)

Returns the port number.

Returns:

  • (Integer)

    the port number



40
# File 'lib/tcp-client/address.rb', line 40

def port = addrinfo.ip_port

Instance Method Details

#freezeAddress

Force the address resolution and prevents further modifications of itself.

Returns:



102
103
104
105
106
107
108
109
# File 'lib/tcp-client/address.rb', line 102

def freeze
  unless frozen?
    solve
    @addrinfo.freeze
    @host.freeze
  end
  super
end

#to_hHash #to_h(&block) ⇒ Hash

Convert self to a Hash containing host and port attribute.

Returns:

  • (Hash)

    host and port



90
# File 'lib/tcp-client/address.rb', line 90

def to_h(&block) = block ? to_hash.to_h(&block) : to_hash

#to_hashHash

Convert self to a Hash containing host and port attribute.

Returns:

  • (Hash)

    host and port



81
# File 'lib/tcp-client/address.rb', line 81

def to_hash = { host: host, port: port }

#to_sString

Returns text representation of self as "host:port".

Returns:

  • (String)

    text representation of self as "host:port"



95
# File 'lib/tcp-client/address.rb', line 95

def to_s = host.index(':') ? "[#{host}]:#{port}" : "#{host}:#{port}"