class NchanTools::Subscriber::EventSourceClient::EventSourceParser

Attributes

buf[RW]
connected[RW]
on_headers[RW]

Public Class Methods

new() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1170
def initialize
  @buf={data: "", id: "", comments: ""}
  buf_reset
end

Public Instance Methods

buf_empty?() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1183
def buf_empty?
  @buf[:comments].length == 0 && @buf[:data].length == 0
end
buf_reset() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1175
def buf_reset
  @buf[:data].clear
  @buf[:id].clear
  @buf[:comments].clear
  @buf[:retry_timeout] = nil
  @buf[:event] = nil
end
on_event(&block) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1218
def on_event(&block)
  @on_event=block
end
parse_event() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1208
def parse_event
  
  if @buf[:comments].length > 0
    @on_event.call :comment, @buf[:comments].chomp!
  elsif @buf[:data].length > 0 || @buf[:id].length > 0 || !@buf[:event].nil?
    @on_event.call @buf[:event], @buf[:data].chomp!, @buf[:id]
  end
  buf_reset
end
parse_line(line) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 1187
def parse_line(line)
  ret = nil
  case line
  when /^: ?(.*)/
    @buf[:comments] << "#{$1}\n"
  when /^data(: (.*))?/
    @buf[:data] << "#{$2}\n" or "\n"
  when /^id(: (.*))?/
    @buf[:id] = $2 or ""
  when /^event(: (.*))?/
    @buf[:event] = $2 or ""
  when /^retry: (.*)/
    @buf[:retry_timeout] = $1
  when /^$/
    ret = parse_event
  else
    raise SubscriberError, "Invalid eventsource data: #{line}"
  end
  ret
end