Class: Gnip::GnipStream::JsonDataBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/gnip/gnip-stream/json_data_bufffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(split_pattern, check_pattern) ⇒ JsonDataBuffer

Returns a new instance of JsonDataBuffer.



6
7
8
9
10
# File 'lib/gnip/gnip-stream/json_data_bufffer.rb', line 6

def initialize(split_pattern, check_pattern)
  @split_pattern = split_pattern
  @check_pattern = check_pattern
  @buffer = ''
end

Instance Attribute Details

#check_patternObject

Returns the value of attribute check_pattern.



4
5
6
# File 'lib/gnip/gnip-stream/json_data_bufffer.rb', line 4

def check_pattern
  @check_pattern
end

#split_patternObject

Returns the value of attribute split_pattern.



4
5
6
# File 'lib/gnip/gnip-stream/json_data_bufffer.rb', line 4

def split_pattern
  @split_pattern
end

Instance Method Details

#complete_entriesObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gnip/gnip-stream/json_data_bufffer.rb', line 16

def complete_entries
  entries = []
  while @buffer =~ check_pattern
    new_line = @buffer[@buffer.size - 2..@buffer.size - 1] == "\r\n"
    activities = @buffer.split(split_pattern)
    entries << activities.shift
    @buffer = activities.join(split_pattern)
    @buffer += "\r\n" if !@buffer.empty? && new_line
  end
  entries.reject(&:empty?)
end

#process(chunk) ⇒ Object



12
13
14
# File 'lib/gnip/gnip-stream/json_data_bufffer.rb', line 12

def process(chunk)
  @buffer.concat(chunk)
end