Class: Spx::Runner

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

Constant Summary collapse

HOST =
"localhost"
MESSAGES =
{
  run_code: "/run-code",
  start_recording: "/start-recording",
  stop_recording: "/stop-recording",
  save_recording: "/save-recording",
  test_callback: "/test-callback",
  record_callback: "/record-callback"
}.freeze
TIMES =
{
  test_timeout: 1,
  stop_margin: 1
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(port, token, callback_port) ⇒ Runner

Returns a new instance of Runner.



19
20
21
22
23
# File 'lib/spx/runner.rb', line 19

def initialize(port, token, callback_port)
  @port = port
  @token = token
  @callback_port = callback_port
end

Instance Method Details

#play(code) ⇒ Object



44
45
46
# File 'lib/spx/runner.rb', line 44

def play(code)
  send_message(MESSAGES[:run_code], code)
end

#record(code, output_file) ⇒ Object

rubocop:disable Metrics/MethodLength



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spx/runner.rb', line 48

def record(code, output_file) # rubocop:disable Metrics/MethodLength
  start_recording

  recorded = false

  run_with_callback(code, MESSAGES[:record_callback]) do
    sleep TIMES[:stop_margin]
    stop_recording
    save_recording(output_file)
    recorded = true
  end

  loop do
    sleep 0.1
    break if recorded
  end
end

#test_connectionObject

rubocop:disable Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/spx/runner.rb', line 25

def test_connection # rubocop:disable Metrics/MethodLength
  connected = false

  run_with_callback("", MESSAGES[:test_callback]) do
    connected = true
  end

  Timeout.timeout(TIMES[:test_timeout]) do
    loop do
      sleep 0.1
      break if connected
    end
  end

  true
rescue Timeout::Error
  false
end