Class: Istox::Publisher

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

Overview

Publisher is relying on BunnyBoot to publish message, please make sure BunnyBoot is initalised properly first during runtime.

Class Method Summary collapse

Class Method Details

.manual_publish(exchange:, routing_key: nil, message:, exchange_type: 'direct', exchange_durable: true, options: {}) ⇒ Object

publish without defining exchange and queue in amqp.yml



36
37
38
39
40
41
42
# File 'lib/istox/helpers/publisher.rb', line 36

def manual_publish(exchange:, routing_key: nil, message:, exchange_type: 'direct', exchange_durable: true, options: {})
  ex = exchange(exchange, type: exchange_type, durable: exchange_durable, options: options)
  # TODO: Implement mandatory & persistent flag for manual publish
  options[:routing_key] = routing_key unless routing_key.nil?
  options[:type] = 'manual'
  do_publish ex, options, message
end

.publish(exchange: nil, routing_key: nil, binding: nil, binding_suffix: nil, message:, options: {}) ⇒ Object

routing_key can be nil if publishing to fanout exchange exchange can be nil if routing_key is unique in “publish” section, this function will be able to identify which exchange it is using else both routing_key and exchange should be provided check reference.rubybunny.info/Bunny/Exchange.html#publish-instance_method for available publish options extra publish options:

publish_confirm: true/false



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/istox/helpers/publisher.rb', line 18

def publish(exchange: nil, routing_key: nil, binding:nil, binding_suffix:nil, message:, options: {})

  if exchange.nil? && routing_key.nil?
    raise 'Exchange and routing key cannot be nil at the same time.' if binding.nil?
    exchange = ::Istox::BunnyBoot.binding_exchange_id(binding)
    raise "Can not find exchange for binding it #{binding}" if exchange.nil?
    routing_key = ::Istox::BunnyBoot.binding_routing_key(binding)
    routing_key = "#{routing_key}.#{binding_suffix}" unless binding_suffix.nil?
  end

  exchange = ::Istox::BunnyBoot.search_exchange_of_routing_key!(routing_key) if routing_key.present? && exchange.nil?
  ex = exchange(exchange)
  options[:routing_key] = routing_key unless routing_key.nil? || options[:routing_key]

  do_publish ex, options, message
end