Sha256: 4ac08a0a7f18fd7c1140924b74da82437bf75a6413e995b874651ea6d7425bec

Contents?: true

Size: 1.01 KB

Versions: 9

Compression:

Stored size: 1.01 KB

Contents

require 'json'
require_relative './constants'

module Deepstream
  class Message
    attr_reader :action, :data, :topic, :sending_deadline

    def self.parse(*args)
      args.first.is_a?(self) ? args.first : new(*args)
    end

    def initialize(*args)
      if args.one?
        args = args.first.delete(MESSAGE_SEPARATOR).split(MESSAGE_PART_SEPARATOR)
      end
      @sending_deadline = nil
      @topic, @action = args.take(2).map(&:to_sym)
      @data = args.drop(2)
    rescue
      ''
    end

    def set_timeout(timeout)
      @sending_deadline = Time.now + timeout
    end

    def to_s
      args = [@topic, @action]
      args << @data unless (@data.nil? || @data.empty?)
      args.join(MESSAGE_PART_SEPARATOR).concat(MESSAGE_SEPARATOR)
    end

    def inspect
      "#{self.class.name}: #{@topic} #{@action} #{@data}"
    end

    def needs_authentication?
      ![TOPIC::CONNECTION, TOPIC::AUTH].include?(@topic)
    end

    def expired?
      @sending_deadline && @sending_deadline < Time.now
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
deepstream-1.0.10 lib/deepstream/message.rb
deepstream-1.0.9 lib/deepstream/message.rb
deepstream-1.0.8 lib/deepstream/message.rb
deepstream-1.0.7 lib/deepstream/message.rb
deepstream-1.0.6 lib/deepstream/message.rb
deepstream-1.0.5 lib/deepstream/message.rb
deepstream-1.0.4 lib/deepstream/message.rb
deepstream-1.0.3 lib/deepstream/message.rb
deepstream-1.0.2 lib/deepstream/message.rb