Sha256: a24565cafed25e813a1a45e8b7a20c8c9263fc480ed93952b17e55bcb5fd7ccf

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

require "tainbox"

module Rabbit::Publishing
  class Message
    include Tainbox

    attribute :routing_key,    String
    attribute :event,          String
    attribute :data,           default: {}
    attribute :exchange_name,  default: []
    attribute :confirm_select, default: true
    attribute :realtime,       default: false
    attribute :headers
    attribute :message_id

    alias_method :confirm_select?, :confirm_select
    alias_method :realtime?, :realtime

    def to_hash
      {
        **attributes,
        data: JSON.parse(data.to_json),
      }
    end

    def to_s
      "#{real_exchange_name} -> #{routing_key} -> #{event}"
    end

    def basic_publish_args
      Rabbit.config.validate!

      raise "Routing key not specified" unless routing_key
      raise "Event name not specified" unless event

      options = {
        mandatory: confirm_select?,
        persistent: true,
        type: event,
        content_type: "application/json",
        app_id: Rabbit.config.app_name,
        headers: headers,
        message_id: message_id,
      }

      [JSON.dump(data), real_exchange_name, routing_key.to_s, options]
    end

    def exchange_name=(names)
      super(Array(names).map(&:to_s))
    end

    def real_exchange_name
      [Rabbit.config.group_id, Rabbit.config.project_id, *exchange_name].join(".")
    end

    def headers
      super || {}
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rabbit_messaging-1.1.0 lib/rabbit/publishing/message.rb
rabbit_messaging-1.0.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.15.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.13.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.12.1 lib/rabbit/publishing/message.rb
rabbit_messaging-0.12.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.11.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.10.0 lib/rabbit/publishing/message.rb