Sha256: d9aabaf572573de9b5bc4b1fea298973f90de3a4b0df17e285443b077154ad68

Contents?: true

Size: 632 Bytes

Versions: 10

Compression:

Stored size: 632 Bytes

Contents

module Rasti
  module Web
    class ServerSentEvent < String

      attr_reader :data, :id, :event

      private

      def initialize(data, options={})
        @data  = data
        @id    = options[:id]
        @event = options[:event]

        super serialize
      end

      def serialize
        serialized_data = data.respond_to?(:to_json) ? data.to_json : data.to_s

        message = ''
        message << "id: #{id}\n" if id
        message << "event: #{event}\n" if event
        serialized_data.split("\n").each do |d|
          message << "data: #{d}\n"
        end
        message << "\n"
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rasti-web-0.2.3 lib/rasti/web/server_sent_event.rb
rasti-web-0.2.2 lib/rasti/web/server_sent_event.rb
rasti-web-0.2.1 lib/rasti/web/server_sent_event.rb
rasti-web-0.2.0 lib/rasti/web/server_sent_event.rb
rasti-web-0.1.1 lib/rasti/web/server_sent_event.rb
rasti-web-0.1.0 lib/rasti/web/server_sent_event.rb
rasti-web-0.0.7 lib/rasti/web/server_sent_event.rb
rasti-web-0.0.6 lib/rasti/web/server_sent_event.rb
rasti-web-0.0.5 lib/rasti/web/server_sent_event.rb
rasti-web-0.0.4 lib/rasti/web/server_sent_event.rb