class PowerTrack::DataBuffer

A buffer of data received from PowerTrack. Useful for managing the sequential chunk of bytes sent of the stream by GNIP and slice them into well-formatted messages.

Constants

MESSAGE_PATTERN

The pattern used by GNIP PowerTrack to delimitate a single message.

Public Class Methods

new() click to toggle source

Builds a new data buffer.

# File lib/powertrack/streaming/data_buffer.rb, line 11
def initialize
  @buffer = ''
end

Public Instance Methods

process(chunk) { |$1| ... } click to toggle source

Add a chunk of bytes to the buffer and pass the new message(s) extracted to the block provided.

# File lib/powertrack/streaming/data_buffer.rb, line 17
def process(chunk, &block)
  @buffer.concat(chunk)
  @buffer.gsub!(MESSAGE_PATTERN) do |match|
    yield($1.to_s) if block_given?
    # erase the message
    ''
  end
end
reset!() click to toggle source

Resets the buffer, therefore losing any bytes received from PowerTrack.

# File lib/powertrack/streaming/data_buffer.rb, line 32
def reset!
  @buffer = ''
end
size() click to toggle source

The current size of the buffer.

# File lib/powertrack/streaming/data_buffer.rb, line 27
def size
  @buffer.size
end