Class: Aikido::Zen::OutboundConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/aikido/zen/outbound_connection.rb

Overview

Simple data object to identify connections performed to outbound servers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:) ⇒ OutboundConnection

Returns a new instance of OutboundConnection.



32
33
34
35
# File 'lib/aikido/zen/outbound_connection.rb', line 32

def initialize(host:, port:)
  @host = host.downcase
  @port = port
end

Instance Attribute Details

#hitsInteger (readonly)

Returns the number of times that this connection was seen by the hosts collector.

Returns:

  • (Integer)

    the number of times that this connection was seen by the hosts collector.



30
31
32
# File 'lib/aikido/zen/outbound_connection.rb', line 30

def hits
  @hits
end

#hostString (readonly)

Returns the hostname or IP address to which the connection was attempted.

Returns:

  • (String)

    the hostname or IP address to which the connection was attempted.



23
24
25
# File 'lib/aikido/zen/outbound_connection.rb', line 23

def host
  @host
end

#portInteger (readonly)

Returns the port number to which the connection was attempted.

Returns:

  • (Integer)

    the port number to which the connection was attempted.



26
27
28
# File 'lib/aikido/zen/outbound_connection.rb', line 26

def port
  @port
end

Class Method Details

.from_json(data) ⇒ Object



6
7
8
9
10
11
# File 'lib/aikido/zen/outbound_connection.rb', line 6

def self.from_json(data)
  new(
    host: data[:hostname],
    port: data[:port]
  )
end

.from_uri(uri) ⇒ Aikido::Zen::OutboundConnection

Convenience factory to create connection descriptions out of URI objects.

Parameters:

  • uri (URI)

Returns:



17
18
19
# File 'lib/aikido/zen/outbound_connection.rb', line 17

def self.from_uri(uri)
  new(host: uri.hostname, port: uri.port)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
50
51
# File 'lib/aikido/zen/outbound_connection.rb', line 47

def ==(other)
  other.is_a?(OutboundConnection) &&
    host == other.host &&
    port == other.port
end

#as_jsonObject



43
44
45
# File 'lib/aikido/zen/outbound_connection.rb', line 43

def as_json
  {hostname: host, port: port, hits: hits}.compact
end

#hashObject



54
55
56
# File 'lib/aikido/zen/outbound_connection.rb', line 54

def hash
  [host, port].hash
end

#hitObject



37
38
39
40
41
# File 'lib/aikido/zen/outbound_connection.rb', line 37

def hit
  # Lazy initialize @hits, so it stays nil until the connection is tracked.
  @hits ||= 0
  @hits += 1
end

#inspectObject



58
59
60
# File 'lib/aikido/zen/outbound_connection.rb', line 58

def inspect
  "#<#{self.class.name} #{host}:#{port}>"
end