Sha256: 3bc0865b0208290bb9286b8aa6e18c4975f414f776c0684006d34f4ee57c8544

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 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

    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,
      }

      [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

    private

    def headers
      super || {}
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rabbit_messaging-0.7.1 lib/rabbit/publishing/message.rb
rabbit_messaging-0.7.0 lib/rabbit/publishing/message.rb
rabbit_messaging-0.6.0 lib/rabbit/publishing/message.rb