Class: GnipApi::PowerTrack::Buffer
- Inherits:
-
Object
- Object
- GnipApi::PowerTrack::Buffer
- Defined in:
- lib/gnip_api/power_track/buffer.rb
Overview
Collects raw data from the stream received in chunks and splits it using the ‘\r\n’ character to return complete items in JSON format.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#terminator ⇒ Object
readonly
Returns the value of attribute terminator.
Instance Method Summary collapse
-
#consume!(char_count) ⇒ Object
Reads
char_countcharacters fromdataremoving them, and returns the extracted characters. -
#initialize(options = {}) ⇒ Buffer
constructor
A new instance of Buffer.
-
#insert!(chunk) ⇒ Object
Inserts a
chunkinto the buffer. -
#read! ⇒ Object
Splits the data and gets completed items from the buffer to remove them from the buffer and return them as an Array of items.
-
#size ⇒ Object
Returns the current size of the buffer.
Constructor Details
#initialize(options = {}) ⇒ Buffer
Returns a new instance of Buffer.
9 10 11 12 13 14 15 |
# File 'lib/gnip_api/power_track/buffer.rb', line 9 def initialize ={} @terminator = .delete(:terminator) || "\r\n" @data = "" @logger = GnipApi.logger @check_span = 30 @last_check = Time.now end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/gnip_api/power_track/buffer.rb', line 7 def data @data end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/gnip_api/power_track/buffer.rb', line 7 def logger @logger end |
#terminator ⇒ Object (readonly)
Returns the value of attribute terminator.
7 8 9 |
# File 'lib/gnip_api/power_track/buffer.rb', line 7 def terminator @terminator end |
Instance Method Details
#consume!(char_count) ⇒ Object
Reads char_count characters from data removing them, and returns the extracted characters.
72 73 74 |
# File 'lib/gnip_api/power_track/buffer.rb', line 72 def consume! char_count @data.slice!(0, char_count) end |
#insert!(chunk) ⇒ Object
Inserts a chunk into the buffer.
23 24 25 26 |
# File 'lib/gnip_api/power_track/buffer.rb', line 23 def insert! chunk check @data << chunk end |
#read! ⇒ Object
Splits the data and gets completed items from the buffer to remove them from the buffer and return them as an Array of items.
data is a simple String object that gets stuff added and using #read! gets stuff removed. At any moment data can contain:
-
a fragment of an item
-
a complete item and a fragment of another
-
one or more complete items
-
a complete item followed by various terminator chars
In all cases #read! will return complete items only, removing these from data as it reads it. The way it does this is by finding the last terminator from right to left. String#rindex method will return the index (starting at 0) of the first char of the terminator. To properly read the data, a 1 is added to this index. Then another 1 is added to make it a count for #consume! to read. Note that #consume! eats up an amount of characters from left to right. The cut piece from the buffer is then splitted by the terminator to return the completed items. In the cut piece a terminator at the end will always be present, but the Array#split method already ignores that final one.
If there’s a single incomplete fragment in data, the index lookup will return nil and an empty list will be returned, signaling nothing to read in the buffer and doing nothing to it until more chunks are inserted.
In the case many terminator chars end up in the buffer, after split these will render as empty strings and removed from the list of items to return.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gnip_api/power_track/buffer.rb', line 59 def read! objects = [] last_terminator_index = @data.rindex(terminator) if last_terminator_index last_terminator_index += 1 # include the following \n objects = consume!(last_terminator_index + 1) # extract upto las terminator positin, +1 because it's an index objects = objects.split(terminator).delete_if{|item| item.nil? || item.size == 0} end return objects end |
#size ⇒ Object
Returns the current size of the buffer.
18 19 20 |
# File 'lib/gnip_api/power_track/buffer.rb', line 18 def size @data.size end |