Class: Smplkit::EventStream::Parser
- Inherits:
-
Object
- Object
- Smplkit::EventStream::Parser
- Defined in:
- lib/smplkit/event_stream.rb
Overview
Incremental parser for a text/event-stream byte stream.
Feed it raw chunks as they arrive; it returns the events completed by
each chunk. Implements the SSE wire format: \n, \r\n, and \r
line terminators (including a CRLF split across chunks), a leading
UTF-8 BOM, comment lines (leading :), field values split across
chunk boundaries, multiple data: lines joined with \n, and the
numeric retry: field. Unknown fields are ignored.
Defined Under Namespace
Classes: Event
Constant Summary collapse
- UTF8_BOM =
String.new("\xEF\xBB\xBF", encoding: Encoding::BINARY).freeze
Instance Attribute Summary collapse
-
#retry_ms ⇒ Object
readonly
Milliseconds from the most recent valid
retry:field, ornil.
Instance Method Summary collapse
-
#feed(chunk) ⇒ Object
Consume one chunk of the stream.
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
85 86 87 88 89 90 91 |
# File 'lib/smplkit/event_stream.rb', line 85 def initialize @buffer = String.new(encoding: Encoding::BINARY) @bom_pending = true @event_type = "" @data_lines = [] @retry_ms = nil end |
Instance Attribute Details
#retry_ms ⇒ Object (readonly)
Milliseconds from the most recent valid retry: field, or nil.
83 84 85 |
# File 'lib/smplkit/event_stream.rb', line 83 def retry_ms @retry_ms end |
Instance Method Details
#feed(chunk) ⇒ Object
Consume one chunk of the stream. Returns the (possibly empty) array of +Event+s completed by this chunk.
95 96 97 98 99 100 101 102 103 |
# File 'lib/smplkit/event_stream.rb', line 95 def feed(chunk) @buffer << chunk.dup.force_encoding(Encoding::BINARY) strip_bom if @bom_pending events = [] while (line = next_line) handle_line(line, events) end events end |