Sha256: 8a83fbb26418e1794ea434bff5afabe06102369b44b25b39deca1b2643bf98b4

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

module Pubsubstub
  class Event
    attr_reader :id, :name, :data, :retry_after

    def initialize(data, options = {})
      @id = options[:id] || time_now
      @name = options[:name]
      @retry_after = options[:retry_after]
      @data = data
    end

    def to_json
      {id: @id, name: @name, data: @data, retry_after: @retry_after}.to_json
    end

    def to_message
      @message ||= build_message
    end

    def self.from_json(json)
      hash = JSON.load(json)
      new(hash['data'], name: hash['name'], id: hash['id'], retry_after: hash['retry_after'])
    end

    def ==(other)
      id == other.id && name == other.name && data == other.data && retry_after == other.retry_after
    end

    private

    def build_message
      data = @data.lines.map{ |segment| "data: #{segment}" }.join
      message = "id: #{id}\n"
      message << "event: #{name}\n" if name
      message << "retry: #{retry_after}\n" if retry_after
      message << data << "\n\n"
      message
    end

    def time_now
      (Time.now.to_f * 1000).to_i
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pubsubstub-0.3.0 lib/pubsubstub/event.rb
pubsubstub-0.2.2 lib/pubsubstub/event.rb
pubsubstub-0.2.1 lib/pubsubstub/event.rb
pubsubstub-0.2.0 lib/pubsubstub/event.rb
pubsubstub-0.1.3 lib/pubsubstub/event.rb
pubsubstub-0.1.2 lib/pubsubstub/event.rb
pubsubstub-0.1.1 lib/pubsubstub/event.rb
pubsubstub-0.1.0 lib/pubsubstub/event.rb
pubsubstub-0.1.0.beta1 lib/pubsubstub/event.rb