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.

Parameters:

  • config (Hash)

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

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :debug (IO, nil)

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



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

def initialize(config, options = {})
  @config = config
  @socket = nil
  @messenger = 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

Parameters:

  • config (Hash)

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

  • options (Hash) (defaults to: {})

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

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:

  • (TrueClass)


116
117
118
119
# File 'lib/web-repl/repl.rb', line 116

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

#closeObject

Close the REPL



122
123
124
125
# File 'lib/web-repl/repl.rb', line 122

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:

  • statement (Fixnum, String)

    A Javascript statement to be evaluated

Returns:

  • (String, nil)

    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)
  @messenger.out({ :statement => statement }) unless @messenger.nil?
end

#input(options = {}) ⇒ String?

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

Returns:

  • (String, nil)

    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
51
52
53
# File 'lib/web-repl/repl.rb', line 38

def input(options = {})
  line = Readline.readline('> ', true)
  if invalid_input?(line)
    Readline::HISTORY.pop
    input(options)
  else
    Readline::HISTORY.pop if repeat_input?(line)
    statement = line.strip
    case statement
    when "exit", "quit" then exit
    else
      evaluate(statement)
      wait_for_response(options)
    end
  end
end

#invalid_input?(line) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/web-repl/repl.rb', line 59

def invalid_input?(line)
  line.nil? || line =~ /^\s*$/
end

#puts_message(message) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/web-repl/repl.rb', line 72

def puts_message(message)
  keys = { :error => :red, :value => :white }
  text = nil
  keys.each do |k,v|
    text ||= message[k].to_s.send(v) unless message[k].nil?
  end
  text ||= "(void)"
  puts(text)
  text
end

#ready?Boolean

Is the socket ready?

Returns:

  • (Boolean)


85
86
87
# File 'lib/web-repl/repl.rb', line 85

def ready?
  !@socket.nil? && !@messenger.nil? && !@handshake.nil?
end

#repeat_input?(line) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/web-repl/repl.rb', line 55

def repeat_input?(line)
  line == Readline::HISTORY.to_a[-2]
end

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

Start the Websocket connection (blocking)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :background (Boolean)

    Do not wait for input, just run in the bg



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/web-repl/repl.rb', line 92

def start(options = {}, &block)
  @thread = Thread.new do
    begin
      EM::WebSocket.run(@config) do |ws|
        if @socket.nil?
          @socket = ws
          @messenger = Messenger.new(@socket)
          configure_event_handling(:background => options[:background], &block)
        end
      end
    rescue Exception => exception
      Thread.main.raise(exception)
    end
  end
  Thread.abort_on_exception = true
  acknowledge_handshake do
    yield if block_given?
    input(options) unless !!options[:background]
  end
  #@thread.join unless !!options[:background]
end

#wait_for_response(options) ⇒ Object

Wait for a response from the browser



64
65
66
67
68
69
70
# File 'lib/web-repl/repl.rb', line 64

def wait_for_response(options)
  until !@buffer.empty? do
  end
  @buffer.each { |resp| puts_message(resp) }
  @buffer.clear
  input unless !!options[:background]
end