Class: MessageQueue::Adapters::Bunny::Connection::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/message_queue/adapters/bunny/publisher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options = {}) ⇒ Publisher

Public: Initialize a new Bunny publisher.

connection - The Bunny Connection. options - The Hash options used to initialize the exchange

of a publisher:
:exchange -
   :name    - The String exchange name.
   :type    - The Symbol exchange type.
   :durable - The Boolean exchange durability.
:message -
   :routing_key - The String message routing key.
   :persistent  - The Boolean indicate if the
   message persisted to disk .
Detailed options see
https://github.com/ruby-amqp/bunny/blob/master/lib/bunny/exchange.rb.

Returns a Publisher.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 23

def initialize(connection, options = {})
  @connection = connection

  options = options.dup

  @exchange_options = options.fetch(:exchange)
  @exchange_name = exchange_options.delete(:name) || (raise "Missing exchange name")
  @exchange_type = exchange_options.delete(:type) || (raise "Missing exchange type")

  @message_options = options.fetch(:message)

  @exchange = connection.connection.default_channel.send(exchange_type, exchange_name, exchange_options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



2
3
4
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 2

def connection
  @connection
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



2
3
4
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 2

def exchange
  @exchange
end

#exchange_nameObject (readonly)

Returns the value of attribute exchange_name.



3
4
5
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 3

def exchange_name
  @exchange_name
end

#exchange_optionsObject (readonly)

Returns the value of attribute exchange_options.



3
4
5
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 3

def exchange_options
  @exchange_options
end

#exchange_typeObject (readonly)

Returns the value of attribute exchange_type.



3
4
5
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 3

def exchange_type
  @exchange_type
end

#message_optionsObject (readonly)

Returns the value of attribute message_options.



4
5
6
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 4

def message_options
  @message_options
end

Instance Method Details

#publish(payload, options = {}) ⇒ Object



37
38
39
# File 'lib/message_queue/adapters/bunny/publisher.rb', line 37

def publish(payload, options = {})
  exchange.publish(connection.serializer.dump(payload), message_options.merge(options))
end