Class: Coatepec::Protocol

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

Overview

Reads and writes the newline-delimited JSON (NDJSON) messages exchanged with the test worker over its private stdio pipe.

Defined Under Namespace

Classes: FramingError

Instance Method Summary collapse

Constructor Details

#initialize(input:, output:) ⇒ Protocol

Returns a new instance of Protocol.



11
12
13
14
# File 'lib/coatepec/protocol.rb', line 11

def initialize(input:, output:)
  @input = input
  @output = output
end

Instance Method Details

#readObject



21
22
23
24
25
26
27
28
# File 'lib/coatepec/protocol.rb', line 21

def read
  line = @input.gets
  return nil if line.nil?

  JSON.parse(line, symbolize_names: true)
rescue JSON::ParserError => e
  raise FramingError, "Malformed NDJSON message: #{e.message}"
end

#write(message) ⇒ Object



16
17
18
19
# File 'lib/coatepec/protocol.rb', line 16

def write(message)
  @output.puts(JSON.generate(message))
  @output.flush
end