Class: NchanTools::Subscriber::EventSourceClient::EventSourceParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nchan_tools/pubsub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventSourceParser

Returns a new instance of EventSourceParser.



1170
1171
1172
1173
# File 'lib/nchan_tools/pubsub.rb', line 1170

def initialize
  @buf={data: "", id: "", comments: ""}
  buf_reset
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



1169
1170
1171
# File 'lib/nchan_tools/pubsub.rb', line 1169

def buf
  @buf
end

#connectedObject

Returns the value of attribute connected.



1169
1170
1171
# File 'lib/nchan_tools/pubsub.rb', line 1169

def connected
  @connected
end

#on_headersObject

Returns the value of attribute on_headers.



1169
1170
1171
# File 'lib/nchan_tools/pubsub.rb', line 1169

def on_headers
  @on_headers
end

Instance Method Details

#buf_empty?Boolean

Returns:

  • (Boolean)


1183
1184
1185
# File 'lib/nchan_tools/pubsub.rb', line 1183

def buf_empty?
  @buf[:comments].length == 0 && @buf[:data].length == 0
end

#buf_resetObject



1175
1176
1177
1178
1179
1180
1181
# 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) ⇒ Object



1218
1219
1220
# File 'lib/nchan_tools/pubsub.rb', line 1218

def on_event(&block)
  @on_event=block
end

#parse_eventObject



1208
1209
1210
1211
1212
1213
1214
1215
1216
# 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) ⇒ Object



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# 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