Sha256: bdb830042dcdab99b5995f68ce0dc91b90c380f5bc7ab06c0dbeb53c3e2f4097

Contents?: true

Size: 953 Bytes

Versions: 12

Compression:

Stored size: 953 Bytes

Contents

require 'json'
require 'deepstream/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, timeout: nil)
      if args.one?
        args = args.first.delete(MESSAGE_SEPARATOR).split(MESSAGE_PART_SEPARATOR)
      end
      @sending_deadline = Time.now + timeout if timeout
      @topic, @action = args.take(2).map(&:to_sym)
      @data = args.drop(2)
    end

    def to_s
      args = [@topic, @action]
      args << @data unless @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

12 entries across 12 versions & 1 rubygems

Version Path
deepstream-0.3.2 lib/deepstream/message.rb
deepstream-0.3.1 lib/deepstream/message.rb
deepstream-0.3.0 lib/deepstream/message.rb
deepstream-0.2.9 lib/deepstream/message.rb
deepstream-0.2.8 lib/deepstream/message.rb
deepstream-0.2.7 lib/deepstream/message.rb
deepstream-0.2.6 lib/deepstream/message.rb
deepstream-0.2.5 lib/deepstream/message.rb
deepstream-0.2.4 lib/deepstream/message.rb
deepstream-0.2.3 lib/deepstream/message.rb
deepstream-0.2.2 lib/deepstream/message.rb
deepstream-0.2.1 lib/deepstream/message.rb