class AMQPHelpers::Publisher

Public Class Methods

new(config) click to toggle source
# File lib/amqp_helpers/publisher.rb, line 6
def initialize(config)
  @config = config
end

Public Instance Methods

publish(message, exchange_name, routing_key) click to toggle source
# File lib/amqp_helpers/publisher.rb, line 10
def publish(message, exchange_name, routing_key)
  Bunny.run(@config) do |connection|
    exchange_config = @config[:exchanges][exchange_name]
    return false unless exchange_config

    exchange = connection.exchange(exchange_name, exchange_config[:params])
    exchange.publish(message,
                     key: routing_key,
                     persistent: exchange_config[:params][:durable],
                     content_type: 'application/json')
  end
  true
rescue => error
  Airbrake.notify_or_ignore(error) if defined?(Airbrake)
  Rails.logger.error("#{error.class}: #{error}") if defined?(Rails)
  raise error
end