Class: TCPSocket

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

Instance Method Summary collapse

Instance Method Details

#accept_loopObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rapel.rb', line 18

def accept_loop
  loop do
    message = JSON.parse(self.gets.chomp, symbolize_names: true)
    $stdout.puts("Message received: #{message} from: #{self}")
    case message[:op]
    when "eval"
      $stdout.puts("Eval message recieved: #{message[:code].inspect}")
      begin
        Rapel::REPLServer::Input.new("qer")
      rescue
        puts $!
      end
      yield Rapel::REPLServer::Input.new(message)
    else
      break
    end
  end
  close
end

#replObject



38
39
40
41
42
43
44
45
46
# File 'lib/rapel.rb', line 38

def repl
  accept_loop do |input|
    input.read do |expression, context|
      expression.evaluate(context) do |result|
        respond_with(result)
      end
    end
  end
end

#respond_with(result) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rapel.rb', line 48

def respond_with(result)
  response = result.to_json
  self.puts response
  $stdout.puts "returned #{response} to #{self}"
rescue Exception => e
  $stdout.puts e.inspect
end