Sha256: f0e92d4e2cdf6b758eee7c120cb120ad659ab05bcf09f02da5bd9269ad004b70

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require 'securerandom'

module Vx
  module Lib
    module Consumer
      module Publish

        def publish(payload, options = {})
          session.open

          options ||= {}
          options[:routing_key]  = params.routing_key if params.routing_key && !options.key?(:routing_key)
          options[:headers]      = params.headers     if params.headers && !options.key?(:headers)

          options[:content_type] ||= params.content_type || configuration.content_type
          options[:message_id]   ||= SecureRandom.uuid

          name = params.exchange_name

          env = {
            payload:     payload,
            exchange:    name,
            consumer:    params.consumer_name,
            properties:  options,
          }

          with_middlewares :pub, env do
            session.with_pub_channel do |ch|
              encoded = encode_payload(payload, options[:content_type])
              x = session.declare_exchange ch, name, params.exchange_options
              x.publish encoded, options
            end
          end
        end

        private

          def encode_payload(payload, content_type)
            Serializer.pack(content_type, payload)
          end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vx-lib-consumer-0.3.4 lib/vx/lib/consumer/publish.rb
vx-lib-consumer-0.3.3 lib/vx/lib/consumer/publish.rb
vx-lib-consumer-0.3.2 lib/vx/lib/consumer/publish.rb
vx-lib-consumer-0.3.1 lib/vx/lib/consumer/publish.rb
vx-lib-consumer-0.3.0 lib/vx/lib/consumer/publish.rb