Class: TL1::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/tl1/session.rb

Overview

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

Defined Under Namespace

Classes: WaitforWrapper

Instance Method Summary collapse

Constructor Details

#initialize(io, timeout = 10) ⇒ Session

Returns a new instance of Session.

Parameters:

  • 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.

  • timeout (Integer) (defaults to: 10)

    How long to wait for responses, by default.



17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#cmd(command, **kwargs) ⇒ TL1::AST::Node

Execute a TL1::Command

Parameters:

Returns:



34
35
36
37
# 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) ⇒ Object

Receive data until the given pattern is matched.

Parameters:

  • pattern (Regexp)
  • timeout (Integer) (defaults to: nil)


43
44
45
46
# File 'lib/tl1/session.rb', line 43

def expect(pattern, timeout = nil)
  timeout ||= @timeout
  @io.expect(pattern, timeout)
end

#raw_cmd(message, timeout = nil) ⇒ String

Send a string and receive a string back.

Parameters:

  • message (String)
  • timeout (Integer) (defaults to: nil)

Returns:

  • (String)


53
54
55
56
# File 'lib/tl1/session.rb', line 53

def raw_cmd(message, timeout = nil)
  write(message)
  expect(COMPLD, timeout)
end

#write(message) ⇒ Boolean

Send a string.

Parameters:

  • message (String)

Returns:

  • (Boolean)


62
63
64
# File 'lib/tl1/session.rb', line 62

def write(message)
  @io.write(message)
end