Class: RubstApi::ServerSentEvent

Inherits:
Data
  • Object
show all
Defined in:
lib/rubst_api/sse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment

Returns:

  • (Object)

    the current value of comment



4
5
6
# File 'lib/rubst_api/sse.rb', line 4

def comment
  @comment
end

#dataObject (readonly)

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



4
5
6
# File 'lib/rubst_api/sse.rb', line 4

def data
  @data
end

#eventObject (readonly)

Returns the value of attribute event

Returns:

  • (Object)

    the current value of event



4
5
6
# File 'lib/rubst_api/sse.rb', line 4

def event
  @event
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



4
5
6
# File 'lib/rubst_api/sse.rb', line 4

def id
  @id
end

#retryObject (readonly)

Returns the value of attribute retry

Returns:

  • (Object)

    the current value of retry



4
5
6
# File 'lib/rubst_api/sse.rb', line 4

def retry
  @retry
end

Instance Method Details

#encodeObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/rubst_api/sse.rb', line 5

def encode
  lines = []
  lines << ": #{comment}" if comment
  lines << "id: #{id}" if id
  lines << "event: #{event}" if event
  lines << "retry: #{self.retry}" if self.retry
  payload = data.is_a?(String) ? data : JSON.generate(Serializer.dump(data))
  payload.each_line { |line| lines << "data: #{line.chomp}" }
  "#{lines.join("\n")}\n\n"
end