Class: Urabbit::Publisher

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

Overview

Usage: begin

pubisher = Publisher.new(
  exchange_name: "courier_tracker",
  routing_key: "in.courier_statuses.created"
)
publisher.publish(message)

rescue Publisher::Error => exception

puts exception.message
puts exception.cause

end

Message is usually a JSON. Exception can contain a cause raised from Bunny.

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Publisher

Returns a new instance of Publisher.



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

def initialize(opts)
  cloudamqp_url = opts[:cloudamqp_url] || ENV["CLOUDAMQP_URL"]
  exchange_type = opts[:exchange_type] || :topic
  exchange_name = opts[:exchange_name] ||
    raise(Error.new("Please provide an 'exchange_name'"))
  @routing_key = opts[:routing_key] ||
    raise(Error.new("Please provide a 'routing_key'"))

  @channel = Urabbit.create_channel
  @exchange = Bunny::Exchange.new(
    @channel,
    exchange_type,
    exchange_name,
    durable: true
  )
rescue Bunny::Exception
  raise Error.new("Error connecting to queue")
end

Instance Method Details

#publish(message) ⇒ Object



35
36
37
38
39
# File 'lib/urabbit/publisher.rb', line 35

def publish(message)
  @exchange.publish(message, routing_key: @routing_key)
rescue Bunny::Exception
  raise Error.new("Error communicating with queue")
end