Class: AgentClientProtocol::Transport::NdjsonReader

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

Instance Method Summary collapse

Constructor Details

#initialize(io, max_line_bytes: MAX_LINE_BYTES) ⇒ NdjsonReader

Returns a new instance of NdjsonReader.



32
33
34
35
# File 'lib/agent_client_protocol/transport.rb', line 32

def initialize(io, max_line_bytes: MAX_LINE_BYTES)
  @io = io
  @max_line_bytes = max_line_bytes
end

Instance Method Details

#closeObject



55
56
57
58
59
# File 'lib/agent_client_protocol/transport.rb', line 55

def close
  @io.close unless @io.closed?
rescue IOError
  # already closed
end

#eachObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/agent_client_protocol/transport.rb', line 37

def each
  return enum_for(:each) unless block_given?

  loop do
    line = read_line
    break if line.nil?

    line = line.strip
    next if line.empty?

    begin
      yield JSON.parse(line)
    rescue JSON::ParserError => e
      raise AgentClientProtocol::RequestError.parse_error(e.message)
    end
  end
end