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) ⇒ EventParser

Constructs an instance of EventParser.

Parameters:

  • lines (Enumerator)

    an enumerator that will yield one line of text at a time



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

def initialize(lines)
  @lines = lines
  reset_buffers
end

Instance Method Details

#itemsObject

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



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

def items
  Enumerator.new do |gen|
    @lines.each do |line|
      line.chomp!
      if line.empty?
        event = maybe_create_event
        reset_buffers
        gen.yield event if !event.nil?
      else
        case line
          when /^(\w+): ?(.*)$/
            item = process_field($1, $2)
            gen.yield item if !item.nil?
        end
      end
    end
  end
end