Class: Utils::SshTunnelSpecification

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/ssh_tunnel_specification.rb

Overview

A class that represents an SSH tunnel specification for configuring network connections.

This class parses and stores the configuration details for SSH tunnels, including local and remote address/port combinations. It provides methods to validate the specification, convert it to string or array representations, and access individual components of the tunnel configuration.

Examples:

spec = Utils::SshTunnelSpecification.new('localhost:8080:remote.host:22')
spec.local_addr  # => 'localhost'
spec.local_port  # => 8080
spec.remote_addr # => 'remote.host'
spec.remote_port # => 22

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_string) ⇒ SshTunnelSpecification

Initializes a new SshTunnelSpecification instance by parsing the provided specification string.

This method takes a specification string and extracts local and remote address/port combinations to configure the SSH tunnel parameters. The specification can take various formats including port-only specifications, localhost mappings, and full address:port combinations.

tunnel configuration



28
29
30
# File 'lib/utils/ssh_tunnel_specification.rb', line 28

def initialize(spec_string)
  interpret_spec(spec_string)
end

Instance Attribute Details

#local_addrString? (readonly)

Returns the local address component of the SSH tunnel specification.



35
36
37
# File 'lib/utils/ssh_tunnel_specification.rb', line 35

def local_addr
  @local_addr
end

#local_portInteger? (readonly)

Returns the local port component of the SSH tunnel specification.



40
41
42
# File 'lib/utils/ssh_tunnel_specification.rb', line 40

def local_port
  @local_port
end

#remote_addrString? (readonly)

Returns the remote address component of the SSH tunnel specification.



45
46
47
# File 'lib/utils/ssh_tunnel_specification.rb', line 45

def remote_addr
  @remote_addr
end

#remote_portInteger? (readonly)

Returns the remote port component of the SSH tunnel specification.



50
51
52
# File 'lib/utils/ssh_tunnel_specification.rb', line 50

def remote_port
  @remote_port
end

Instance Method Details

#to_aArray<String, Integer, String, Integer>

Returns an array representation of the SSH tunnel specification.

This method combines the local and remote address/port components into a four-element array in the order: [local_addr, local_port, remote_addr, remote_port].



59
60
61
# File 'lib/utils/ssh_tunnel_specification.rb', line 59

def to_a
  [ local_addr, local_port, remote_addr, remote_port ]
end

#to_sString

Returns a string representation of the SSH tunnel specification.

This method combines the local address, local port, remote address, and remote port components into a single colon-separated string format.



86
87
88
# File 'lib/utils/ssh_tunnel_specification.rb', line 86

def to_s
  to_a * ':'
end

#valid?String?

Checks if all components of the SSH tunnel specification are present and valid.

This method verifies that all address and port components of the tunnel configuration have been set. If all components are present, it returns the string representation of the specification; otherwise, it returns nil.



73
74
75
76
77
# File 'lib/utils/ssh_tunnel_specification.rb', line 73

def valid?
  if to_a.all?
    to_s
  end
end