Sha256: 4d072694264bee243ae4997b8f62e593f92b7ba5581a74839b717656f142eddd

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Redstream
  # The Redstream::Message class wraps a raw redis stream message to allow hash
  # and id/offset access as well as convenient parsing of the json payload.

  class Message
    # @api private
    #
    # Initializes the message.
    #
    # @param raw_message [Array] The raw message as returned by redis

    def initialize(raw_message)
      @message_id = raw_message[0]
      @raw_message = raw_message
    end

    # Returns the message id, i.e. the redis message id consisting of a
    # timestamp plus sequence number.
    #
    # @returns [String] The message id

    def message_id
      @message_id
    end

    # Returns the parsed message payload as provided by the model's
    # #redstream_payload method. Check out Redstream::Model for more details.
    #
    # @return [Hash] The parsed payload

    def payload
      @payload ||= JSON.parse(fields["payload"])
    end

    # As a redis stream message allows to specify fields,
    # this allows to retrieve the fields as a hash.
    #
    # @returns The fields hash

    def fields
      @fields ||= @raw_message[1]
    end

    # Returns the raw message content as returned by redis.

    def raw_message
      @raw_message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redstream-0.0.1 lib/redstream/message.rb