Class: ServerSentEvents::Event
- Inherits:
-
Object
- Object
- ServerSentEvents::Event
- Defined in:
- lib/server_sent_events/event.rb
Overview
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.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize ⇒ Event
constructor
A new instance of Event.
-
#set(key, value) ⇒ Object
Set event key-value pair.
-
#to_s ⇒ Object
Serialize event into form for transmission.
Constructor Details
#initialize ⇒ Event
Returns a new instance of Event.
12 13 14 |
# File 'lib/server_sent_events/event.rb', line 12 def initialize @data = "" end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
10 11 12 |
# File 'lib/server_sent_events/event.rb', line 10 def data @data end |
#event ⇒ Object (readonly)
Returns the value of attribute event.
10 11 12 |
# File 'lib/server_sent_events/event.rb', line 10 def event @event end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/server_sent_events/event.rb', line 10 def id @id end |
Instance Method Details
#==(other) ⇒ Object
48 49 50 |
# File 'lib/server_sent_events/event.rb', line 48 def ==(other) other.id == id && other.event == event && other.data == data end |
#set(key, value) ⇒ Object
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.
25 26 27 28 29 30 31 |
# File 'lib/server_sent_events/event.rb', line 25 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 ⇒ Object
Serialize event into form for transmission.
Output of this method call can be written directly into the socket.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/server_sent_events/event.rb', line 36 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 |