Class: K8s::JSONParser::Default::LineBufferedIO

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

Overview

A simple buffer that accepts data through the ‘<<` method and yields its buffer when a newline character is encountered.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ LineBufferedIO

Returns a new instance of LineBufferedIO.



12
13
14
15
# File 'lib/k8s/json_parser.rb', line 12

def initialize(&block)
  @buffer = +''
  @parser = block
end

Instance Method Details

#<<(data) ⇒ Object

Parameters:

  • data (String)

    partial JSON content



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/k8s/json_parser.rb', line 18

def <<(data)
  chunks = data.rpartition("\n")
  tail = chunks.pop
  @buffer << chunks.join

  if @buffer.include?("\n")
    @buffer.each_line do |line|
      @parser.call(line) unless line.strip.empty?
    end

    @buffer.clear
  end

  @buffer << tail
end