Class: LittleGhost::Providers::SSEParser

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initializeSSEParser

Returns a new instance of SSEParser.



6
7
8
# File 'lib/little_ghost/providers/sse_parser.rb', line 6

def initialize
  @buffer = +""
end

Instance Method Details

#<<(chunk) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/little_ghost/providers/sse_parser.rb', line 10

def <<(chunk)
  @buffer << chunk.to_s
  @buffer.gsub!("\r\n", "\n")
  events = []

  while (boundary = @buffer.index("\n\n"))
    frame = @buffer.slice!(0, boundary + 2)
    data = frame.lines.filter_map do |line|
      next unless line.start_with?("data:")

      line.delete_prefix("data:").sub(/\A /, "").chomp
    end
    events << data.join("\n") unless data.empty?
  end

  events
end

#finishObject



28
29
30
31
32
# File 'lib/little_ghost/providers/sse_parser.rb', line 28

def finish
  return [] if @buffer.empty?

  self << "\n\n"
end