class TL1::Session

A wrapper around an IO-like object representing a connection to a TL1-capable network element.

Public Class Methods

new(io, timeout = 10) click to toggle source

@param io [IO, Net::SSH::Telnet, Net::Telnet]

An established connection to a TL1 server.

The connection object must have a `#write` method, and one of two read
methods: `#expect` or `#waitfor`. If you are using Net::Telnet or
Net::SSH::Telnet, `#waitfor` will be used. Otherwise, you should make
sure that your connection object has an `#expect` method that behaves
like `IO#expect` from the standard library.

@param timeout [Integer]

How long to wait for responses, by default.
# File lib/tl1/session.rb, line 17
def initialize(io, timeout = 10)
  @timeout = timeout
  @io =
    if io.respond_to?(:expect)
      io
    elsif io.respond_to?(:waitfor)
      WaitforWrapper.new(io)
    else
      raise UnsupportedIOError,
            "the given IO doesn't respond to expect or waitfor"
    end
end

Public Instance Methods

cmd(command, **kwargs) click to toggle source

Execute a TL1::Command

@param [TL1::Command] @return [TL1::AST::Node]

# File lib/tl1/session.rb, line 34
def cmd(command, **kwargs)
  output = raw_cmd(command.input(**kwargs))
  command.parse_output(output)
end
expect(pattern, timeout = nil) click to toggle source

Receive data until the given pattern is matched.

@param pattern [Regexp] @param timeout [Integer]

# File lib/tl1/session.rb, line 43
def expect(pattern, timeout = nil)
  timeout ||= @timeout
  @io.expect(pattern, timeout)
end
raw_cmd(message, timeout = nil) click to toggle source

Send a string and receive a string back.

@param message [String] @param timeout [Integer] @return [String]

# File lib/tl1/session.rb, line 53
def raw_cmd(message, timeout = nil)
  write(message)
  expect(COMPLD, timeout)
end
write(message) click to toggle source

Send a string.

@param message [String] @return [Boolean]

# File lib/tl1/session.rb, line 62
def write(message)
  @io.write(message)
end