Class: Tama::SSEParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tama/sse_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(callback) ⇒ SSEParser

Returns a new instance of SSEParser.



7
8
9
10
11
# File 'lib/tama/sse_parser.rb', line 7

def initialize(callback)
  @callback = callback
  @buffer = +""
  @done = false
end

Instance Method Details

#feed(chunk) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
# File 'lib/tama/sse_parser.rb', line 13

def feed(chunk)
  return if @done
  raise Error::ParseError, "SSE chunk must be a string" unless chunk.is_a?(String)

  @buffer << chunk
  consume_complete_events
  self
end

#finishObject



22
23
24
25
26
# File 'lib/tama/sse_parser.rb', line 22

def finish
  process_event(@buffer) unless @done || @buffer.empty?
  @buffer.clear
  self
end