Class: Lightstreamer::LineBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/lightstreamer/line_buffer.rb

Overview

Helper class that takes an incoming stream of ASCII data and yields back individual lines as they become complete.

Instance Method Summary collapse

Constructor Details

#initializeLineBuffer

Returns a new instance of LineBuffer.



4
5
6
# File 'lib/lightstreamer/line_buffer.rb', line 4

def initialize
  @buffer = ''
end

Instance Method Details

#process(data) ⇒ Object

Appends a new piece of ASCII data to this buffer. Any lines that are now complete will be yielded back.

Parameters:

  • data (String)

    The new piece of ASCII data.



11
12
13
14
15
16
17
18
19
20
# File 'lib/lightstreamer/line_buffer.rb', line 11

def process(data)
  @buffer << data

  lines = @buffer.split "\n"
  @buffer = @buffer.end_with?("\n") ? '' : lines.pop

  lines.each do |line|
    yield line.strip
  end
end