Class: WebRepl::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/web-repl/repl.rb

Overview

The main REPL object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ REPL

Returns a new instance of REPL.

Options Hash (options):

  • :debug (IO, nil)

    A debug logger or nil if debug is not needed (default: nil)

Parameters:

  • A hash of config options to be passed to EM::WebSocket.run directly

  • (defaults to: {})


21
22
23
24
25
26
27
# File 'lib/web-repl/repl.rb', line 21

def initialize(config, options = {})
  @config = config
  @socket = nil
  @messager = nil
  @buffer = []
  @debug = options[:debug]
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/web-repl/repl.rb', line 6

def thread
  @thread
end

Class Method Details

.start(config, options = {}) ⇒ WebRepl::REPL

Start a repl connection

Options Hash (options):

  • :debug (IO, nil)

    A debug logger or nil if debug is not needed (default: nil)

  • :background (Boolean)

    Do not wait for input, just run in the bg

Parameters:

  • A hash of config options to be passed to EM::WebSocket.run directly

  • (defaults to: {})

Returns:



14
15
16
# File 'lib/web-repl/repl.rb', line 14

def self.start(config, options = {})
  new(config, options).tap { |repl| repl.start(options) }
end

Instance Method Details

#acknowledge_handshake(&block) ⇒ TrueClass

Execute a block when a connection is made

Returns:



79
80
81
82
83
84
# File 'lib/web-repl/repl.rb', line 79

def acknowledge_handshake(&block)
  Thread.new do
    loop until !@handshake.nil?
    yield
  end
end

#closeObject

Close the REPL



87
88
89
90
# File 'lib/web-repl/repl.rb', line 87

def close
  @socket.close unless @socket.nil?
  @thread.kill unless @thread.nil?
end

#evaluate(statement) ⇒ String?

Send a statement to the browser for evaluation

Parameters:

  • A Javascript statement to be evaluated

Returns:

  • The data that was sent to the browser, or nil if sending could not be completed.



32
33
34
# File 'lib/web-repl/repl.rb', line 32

def evaluate(statement)
  @messager.out({ :statement => statement }) unless @messager.nil?
end

#getsString?

Prompt the Ruby user for input and send that input to the browser for evaluation (blocking)

Returns:

  • The data that was sent to the browser, or nil if sending could not be completed



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/web-repl/repl.rb', line 38

def gets
  line = Readline.readline('> ', true)
  return nil if line.nil?
  if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
    Readline::HISTORY.pop
  end      
  statement = line.strip
  case statement
  when "exit" then exit
  else
    evaluate(statement)
  end
end

#start(options = {}, &block) ⇒ Object

Start the Websocket connection (blocking)

Options Hash (options):

  • :background (Boolean)

    Do not wait for input, just run in the bg

Parameters:

  • (defaults to: {})


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/web-repl/repl.rb', line 60

def start(options = {}, &block)
  @thread = Thread.new do
    EM::WebSocket.run(@config) do |ws|
      if @socket.nil?
        @socket = ws
        @messager = Messager.new(@socket)
        configure_event_handling(:background => options[:background], &block)
      end
    end
  end
  acknowledge_handshake do
    yield if block_given?
    gets unless !!options[:background]
  end
  @thread.join unless !!options[:background]
end

#wait_for_responseObject

Wait for a response from the browser



53
54
55
# File 'lib/web-repl/repl.rb', line 53

def wait_for_response
  loop until !@buffer.empty?
end