Class: Tox::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/tox/node.rb

Overview

Tox node credentials.

Constant Summary collapse

PORT_RANGE =

Range of valid port numbers.

1..65_535

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Node

Returns a new instance of Node.



11
12
13
# File 'lib/tox/node.rb', line 11

def initialize(data)
  @data = data.map { |k, v| [k.to_sym, v] }.to_h.freeze
end

Instance Method Details

#ipv4Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/tox/node.rb', line 15

def ipv4
  @ipv4 ||=
    begin
      value = @data[:ipv4]
      unless value.is_a? String
        raise TypeError, "expected value to be a #{String}"
      end
      value.frozen? ? value : value.dup.freeze
    end
end

#portObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tox/node.rb', line 26

def port
  @port ||= begin
    value = @data[:port]
    unless value.is_a? Integer
      raise TypeError, "expected value to be an #{Integer}"
    end
    unless PORT_RANGE.cover? value
      raise ArgumentError, 'expected value to be between 0 and 65535'
    end
    value
  end
end

#public_keyObject



39
40
41
42
43
44
45
# File 'lib/tox/node.rb', line 39

def public_key
  @public_key ||=
    begin
      value = @data[:public_key]
      PublicKey.new value
    end
end

#resolv_ipv4Object



55
56
57
# File 'lib/tox/node.rb', line 55

def resolv_ipv4
  @resolv_ipv4 ||= Resolv.getaddress ipv4
end

#status_tcpObject



51
52
53
# File 'lib/tox/node.rb', line 51

def status_tcp
  @status_tcp ||= !!@data[:status_tcp]
end

#status_udpObject



47
48
49
# File 'lib/tox/node.rb', line 47

def status_udp
  @status_udp ||= !!@data[:status_udp]
end