class Rails::SSE::Channel

Attributes

stream[R]

Public Class Methods

new(stream) click to toggle source
# File lib/rails/sse/channel.rb, line 6
def initialize(stream)
  @stream = stream
end

Public Instance Methods

ping!() click to toggle source
# File lib/rails/sse/channel.rb, line 21
def ping!
  post(:ping)
end
post(data, options = {}) click to toggle source
# File lib/rails/sse/channel.rb, line 10
def post(data, options = {})
  raise ArgumentError if empty_arg?(data) && empty_arg?(options)

  options.each do |key, value|
    @stream.write("#{key}: #{value}\n")
  end

  @stream.write("data: #{JSON.dump(data)}\n") unless empty_arg?(data)
  end_message
end

Private Instance Methods

empty_arg?(argument) click to toggle source
# File lib/rails/sse/channel.rb, line 30
def empty_arg?(argument)
  !argument || argument.empty?
end
end_message() click to toggle source
# File lib/rails/sse/channel.rb, line 26
def end_message
  @stream.write("\n")
end