Class: GnipApi::Apis::PowerTrack::Buffer
- Inherits:
-
Object
- Object
- GnipApi::Apis::PowerTrack::Buffer
- Defined in:
- lib/gnip_api/apis/power_track/buffer.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#terminator ⇒ Object
readonly
Returns the value of attribute terminator.
Instance Method Summary collapse
- #consume!(chars) ⇒ Object
-
#initialize(options = {}) ⇒ Buffer
constructor
A new instance of Buffer.
- #insert!(chunk) ⇒ Object
- #read! ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Buffer
Returns a new instance of Buffer.
7 8 9 10 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 7 def initialize ={} @terminator = .delete(:terminator) || "\r\n" @data = "" end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 5 def data @data end |
#terminator ⇒ Object (readonly)
Returns the value of attribute terminator.
5 6 7 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 5 def terminator @terminator end |
Instance Method Details
#consume!(chars) ⇒ Object
32 33 34 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 32 def consume! chars @data[0..chars-1] = '' end |
#insert!(chunk) ⇒ Object
16 17 18 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 16 def insert! chunk @data << chunk end |
#read! ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 20 def read! objects = @data.split(terminator)[0..-2] unless objects.empty? # Get the number of chars to read from buffer, counting # the size of each splited chunk plus the number of chunks by # terminator size, since it's still present in the buffer size = objects.map(&:size).reduce(:+) + objects.size * terminator.size consume!(size) end return objects end |
#size ⇒ Object
12 13 14 |
# File 'lib/gnip_api/apis/power_track/buffer.rb', line 12 def size @data.size end |