class ServerSentEvents::Event
Class that represents server event.
This class can be used to construct new events on the server and then serialize them for the transfer or is returned as a result of {Parser} action on the client.
Attributes
data[R]
event[R]
id[R]
Public Class Methods
new()
click to toggle source
# File lib/server_sent_events/event.rb, line 11 def initialize @data = "" end
Public Instance Methods
==(other)
click to toggle source
# File lib/server_sent_events/event.rb, line 47 def ==(other) other.id == id && other.event == event && other.data == data end
set(key, value)
click to toggle source
Set event key-value pair.
Note that the only valid keys are `id`, `event` and `data`, last one being a bit special. Calling `set(“data”, “some data”)` will *apend* this the string `some data` to existing data using newline as a separator.
@param key [String] key to use @param value [String] data to set/append
# File lib/server_sent_events/event.rb, line 24 def set(key, value) case key when "id" then @id = value when "event" then @event = value when "data" then append_data(value) end end
to_s()
click to toggle source
Serialize event into form for transmission.
Output of this method call can be written directly into the socket.
# File lib/server_sent_events/event.rb, line 35 def to_s repr = "" repr += "id: #{id}\n" if id repr += "event: #{event}\n" if event if data.empty? repr += "data: \n" else data.split("\n").each { |l| repr += "data: #{l}\n" } end repr += "\n" end
Private Instance Methods
append_data(chunk)
click to toggle source
# File lib/server_sent_events/event.rb, line 53 def append_data(chunk) @data = @data.empty? ? chunk : "#{@data}\n#{chunk}" end