Class: AMQPHelpers::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/amqp_helpers/publisher.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Publisher

Returns a new instance of Publisher.



6
7
8
# File 'lib/amqp_helpers/publisher.rb', line 6

def initialize(config)
  @config = config
end

Instance Method Details

#publish(message, exchange_name, routing_key) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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