Sha256: e6ee00a9668a61b16ab5bdad74ff4946f677b607a2aa04ce18dc6043236e6586

Contents?: true

Size: 929 Bytes

Versions: 6

Compression:

Stored size: 929 Bytes

Contents

module PowerTrack
  # 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.
  class DataBuffer

    # The pattern used by GNIP PowerTrack to delimitate a single message.
    MESSAGE_PATTERN = /^([^\r]*)\r\n/m

    # Builds a new data buffer.
    def initialize
      @buffer = ''
    end

    # Add a chunk of bytes to the buffer and pass the new message(s) extracted
    # to the block provided.
    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

    # The current size of the buffer.
    def size
      @buffer.size
    end

    # Resets the buffer, therefore losing any bytes received from PowerTrack.
    def reset!
      @buffer = ''
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
powertrack-1.1.1 lib/powertrack/streaming/data_buffer.rb
powertrack-1.1.0 lib/powertrack/streaming/data_buffer.rb
powertrack-1.0.3 lib/powertrack/streaming/data_buffer.rb
powertrack-1.0.2 lib/powertrack/streaming/data_buffer.rb
powertrack-1.0.1 lib/powertrack/streaming/data_buffer.rb
powertrack-1.0.0 lib/powertrack/streaming/data_buffer.rb