Class: GnipApi::PowerTrack::Buffer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 options={}
  @terminator = options.delete(:terminator) || "\r\n"
  @data = ""
  @logger = GnipApi.logger
  @check_span = 30
  @last_check = Time.now
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/gnip_api/power_track/buffer.rb', line 7

def data
  @data
end

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/gnip_api/power_track/buffer.rb', line 7

def logger
  @logger
end

#terminatorObject (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

#sizeObject

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