Sha256: 84e5c38859c75b96e83343f70bc5df9056bf50ed885121d5b209a3085edb73ef
Contents?: true
Size: 936 Bytes
Versions: 4
Compression:
Stored size: 936 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.freeze # 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
4 entries across 4 versions & 1 rubygems