Class: Fal::Stream::SSEDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/fal/stream.rb

Overview

Minimal SSE decoder for parsing standard server-sent event stream lines.

Instance Method Summary collapse

Constructor Details

#initializeSSEDecoder

Returns a new instance of SSEDecoder.



40
41
42
43
44
45
# File 'lib/fal/stream.rb', line 40

def initialize
  @event = ""
  @data = ""
  @id = nil
  @retry = nil
end

Instance Method Details

#decode(line) ⇒ Hash?

Parameters:

  • line (String)

Returns:

  • (Hash, nil)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fal/stream.rb', line 49

def decode(line)
  return flush_event if line.empty?
  return if line.start_with?(":")

  field, _, value = line.partition(":")
  value = value.lstrip

  case field
  when "event"
    @event = value
  when "data"
    @data += "#{value}\n"
  when "id"
    @id = value
  when "retry"
    @retry = value.to_i
  end

  nil
end