Class: SSE::EventParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sse_client/sse_events.rb

Overview

Accepts lines of text via an iterator, and parses them into SSE messages.

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ EventParser



12
13
14
15
# File 'lib/sse_client/sse_events.rb', line 12

def initialize(lines)
  @lines = lines
  reset_buffers
end

Instance Method Details

#itemsObject

Generator that parses the input interator and returns instances of SSEEvent or SSERetryInterval.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sse_client/sse_events.rb', line 18

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