Sha256: e51ec52bdc0f85643121db0e0c8268001b253701e4a0d13646b795151bb2996b

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'hashie'
require 'bunny'

require 'hat/configurable'
module Hat

  module Publisher
    include Configurable

    has_configuration :publisher,
                      {
                        middlewares: [],
                        options:                 {
                          persistent: false,
                          mandatory:  false
                        },
                        default_direct_exchange: 'hat.direct',
                        default_topic_exchange:  'hat.topic'
                      }

    def self.channel
      unless @connection
        @connection = Bunny.new
        @connection.start
      end
      @channel ||= @connection.create_channel
    end

    def self.publish(type, exchange, message, queue=nil, options={}, exchange_options={})
      options.merge(routing_key: queue) if queue
      config.publisher.middlewares.each { |m| message = m.call(message) }
      channel.send(type, exchange, exchange_options.merge(config.exchange)).
        publish(message, options.merge(config.publisher.options))
      true
    end

    def self.direct(queue, message, options={})
      publish(:direct, (options.delete(:exchange) || config.publisher.default_direct_exchange), message, queue, options)
    end

    def self.topic(queue, message, options={})
      publish(:topic, (options.delete(:exchange) || config.publisher.default_topic_exchange), message, queue, options)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rabbit-hat-0.0.1 lib/hat/publisher.rb