module Expect::Behavior

Add Expect behaviors to Accessor classes that have an input buffer: e.g. telnet/ssh :Required methods to be created by the class mixing Expect::Behaviors :

#exp_process - should do one iteration of handle input and append buffer
#exp_buffer - provide the current buffer contents and empty it

Constants

EXP_SLEEP_INTERVAL_SEC_DEFAULT
EXP_TIMEOUT_SEC_DEFAULT

Attributes

exp_match[R]
exp_sleep_interval_sec[RW]
exp_timeout_sec[RW]

Public Instance Methods

expect(&block) click to toggle source
# File lib/expect/behavior.rb, line 20
def expect(&block)
  #pre-action
  initialize_expect if @__exp_buffer.nil?
  @exp_match_registry = {}
  #register callbacks
  instance_eval(&block)
  #action
  result = execute_expect_loop
  #post-action
  @exp_timeout_sec_ephemeral = @exp_timeout_sec
  result
end

Private Instance Methods

clear_expect_buffer() click to toggle source
# File lib/expect/behavior.rb, line 38
def clear_expect_buffer
  @__exp_buffer = ''
end
current_time() click to toggle source
# File lib/expect/behavior.rb, line 42
def current_time
  Time.now.to_f
end
execute_expect_loop() click to toggle source
# File lib/expect/behavior.rb, line 72
def execute_expect_loop
  init_timeout
  @exp_match = nil
  result = nil
  @__exp_buffer = ''
  @__exp_full_buffer = ''
  while result.nil?
    if timeout?
      result = @exp_timeout_block.nil? ? timeout_action_default : @exp_timeout_block.call
    else
      raise unless respond_to?(:exp_buffer)
      raise unless respond_to?(:exp_process)
      # call process/buffer and then check for match
      exp_process
      newbuffertext = exp_buffer.to_s
      @__exp_buffer << newbuffertext
      @__exp_full_buffer << newbuffertext
      if exp_registered_matches
        result = @exp_match
      else
        sleep(@exp_sleep_interval_sec)
      end
    end
  end
  result
end
exp_continue()
Alias for: expect_continue
exp_registered_matches() click to toggle source
# File lib/expect/behavior.rb, line 46
def exp_registered_matches
  match_object = nil
  unless @__exp_buffer.nil?
    @exp_match_registry.each_pair do |expr, block|
      expr = expr.to_s unless expr.is_a?(Regexp)
      if @__exp_buffer.match(expr)
        result = block.call
        match_object = result.eql?('exp_continue') ? nil : Expect::Match.new(expr, @__exp_full_buffer)
        break # don't try to match more than one
      end
    end
  end
  @exp_match = match_object
  @exp_match
end
expect_continue() click to toggle source

reset timeout and continue expect loop

# File lib/expect/behavior.rb, line 64
def expect_continue
  init_timeout
  @__exp_buffer = '' # avoid matching the same text twice
  "exp_continue"
end
Also aliased as: exp_continue, reset_timeout
init_timeout() click to toggle source
# File lib/expect/behavior.rb, line 110
def init_timeout
  @start_time = current_time
end
initialize_expect() click to toggle source
# File lib/expect/behavior.rb, line 99
def initialize_expect
  @exp_match_registry ||= {}
  @exp_match = nil
  @exp_sleep_interval_sec ||= EXP_SLEEP_INTERVAL_SEC_DEFAULT
  @exp_timeout_sec ||= EXP_TIMEOUT_SEC_DEFAULT
  @exp_timeout_sec_ephemeral = @exp_timeout_sec
  @exp_timeout_block ||= nil
  @__exp_buffer ||= ''
  @__exp_full_buffer ||= ''
end
reset_timeout()
Alias for: expect_continue
timeout?() click to toggle source
# File lib/expect/behavior.rb, line 114
def timeout?
  (current_time - @start_time) > @exp_timeout_sec_ephemeral
end
timeout_action_default() click to toggle source
# File lib/expect/behavior.rb, line 118
def timeout_action_default
  raise(TimeoutError, "Expect Timeout [start_time=#{@start_time}] [time=#{current_time}]")
end
when_match(expression, &block)
Alias for: when_matching
when_matching(expression, &block) click to toggle source
# File lib/expect/behavior.rb, line 122
def when_matching(expression, &block)
  @exp_match_registry[expression] = block
end
Also aliased as: when_match
when_timeout(timeout_sec = nil, &block) click to toggle source
# File lib/expect/behavior.rb, line 127
def when_timeout(timeout_sec = nil, &block)
  @exp_timeout_sec_ephemeral = timeout_sec unless timeout_sec.nil?
  @exp_timeout_block = block
end