Class: SSE::Impl::EventParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ld-eventsource/impl/event_parser.rb

Overview

Accepts lines of text via an enumerator, and parses them into SSE messages. You will not need to use this directly if you are using Client, but it may be useful for testing.

Instance Method Summary collapse

Constructor Details

#initialize(lines, last_event_id = nil) ⇒ EventParser

Constructs an instance of EventParser.

Parameters:

  • lines (Enumerator)

    an enumerator that will yield one line of text at a time; the lines should not include line terminators



26
27
28
29
30
# File 'lib/ld-eventsource/impl/event_parser.rb', line 26

def initialize(lines, last_event_id = nil)
  @lines = lines
  @last_event_id = last_event_id
  reset_buffers
end

Instance Method Details

#itemsObject

Generator that parses the input iterator and returns instances of StreamEvent or SetRetryInterval.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ld-eventsource/impl/event_parser.rb', line 33

def items
  Enumerator.new do |gen|
    @lines.each do |line|
      if line.empty?
        event = maybe_create_event
        reset_buffers
        gen.yield event if !event.nil?
      elsif (pos = line.index(':'))
        name = line.slice(0...pos)

        pos += 1  # skip colon
        pos += 1 if pos < line.length && line[pos] == ' '  # skip optional single space, per SSE spec
        line = line.slice(pos..-1)

        item = process_field(name, line)
        gen.yield item if !item.nil?
      end
    end
  end
end