Class: Pebblebed::NdjsonBuffer

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

Overview

Helper class which buffers an NDJSON input stream, parsing each complete line into a handler as JSON.

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ NdjsonBuffer

Initializes with handler. The handler must provide a method ‘call’ which will be called with each JSON payload.



9
10
11
12
13
# File 'lib/pebblebed/ndjson.rb', line 9

def initialize(handler)
  @handler = handler
  @buf = ''
  @end = false
end

Instance Method Details

#<<(data) ⇒ Object

Feeds data into the buffer.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pebblebed/ndjson.rb', line 31

def <<(data)
  return if data.empty?
  @buf << data
  begin
    if /\A(?<line>[^\n]*)\n(?<rest>.*)\z/m =~ @buf
      if line.length == 0 && rest.length == 0
        @buf.clear
        @end = true
      else
        payload, @buf = JSON.parse(line), rest
        @handler.call(payload)
      end
    else
      break
    end
  end until @end
end

#check_end!Object

Checks whether this buffer has reached its end. Raises IOError otherwise.



22
23
24
25
26
27
28
# File 'lib/pebblebed/ndjson.rb', line 22

def check_end!
  unless @end
    # We need to raise this to signal that a complete contents
    # was not returned.
    raise IOError, "End of stream expected"
  end
end

#ended?Boolean

Returns true if the end of the stream has been reached.

Returns:

  • (Boolean)


16
17
18
# File 'lib/pebblebed/ndjson.rb', line 16

def ended?
  @end
end