class Servent::Stream

Attributes

last_event_id[R]
reconnection_time[R]

Public Class Methods

new(stream) click to toggle source
# File lib/servent/stream.rb, line 5
def initialize(stream)
  @stream = StringIO.new stream
  @buffer = []
  @events = []
end

Public Instance Methods

parse() click to toggle source
# File lib/servent/stream.rb, line 11
def parse
  @stream.each_line { |line|
    next if line.start_with?(Servent::COLON)
    handle_line line
  }
  complete_event
  @events
end

Private Instance Methods

buffer(line) click to toggle source
# File lib/servent/stream.rb, line 51
def buffer(line)
  # TODO: if this line defines a new type, than also #complete_event
  @buffer << line
end
complete_event(line = nil) click to toggle source
# File lib/servent/stream.rb, line 33
def complete_event(line = nil)
  return if @buffer.empty?
  buffer line unless line.nil?
  @events << create_event
  @buffer = []
end
create_event() click to toggle source
# File lib/servent/stream.rb, line 40
def create_event
  begin
    event = Event.new(@buffer.join("\n"))
  rescue Servent::Event::InvalidError
    return
  end
  @last_event_id = event.id
  @reconnection_time = event.retry
  event
end
handle_line(line) click to toggle source
# File lib/servent/stream.rb, line 22
def handle_line(line)
  # Line is empty:
  #  - can be the end of a stream or
  #  - can be a stream with multiple events
  if line.strip.chomp.empty?
    complete_event line
  else
    buffer line
  end
end