class MqPublisher::Publisher

Attributes

channel[R]
connection[R]
exchange[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/mq_publisher/publisher.rb, line 5
def initialize(options={})
  setup_connection(options.slice(:host, :port, :username, :password, :heartbeat, :vhost))
  setup_channel
  setup_exchange(options.slice(:exchange_name, :exchange_type, :exchange_auto_delete, :exchange_durable))
end

Public Instance Methods

destroy() click to toggle source
# File lib/mq_publisher/publisher.rb, line 32
def destroy
  connection.close
end
publish(message="{}", options={}) click to toggle source
# File lib/mq_publisher/publisher.rb, line 11
def publish(message="{}", options={})
  exchange.publish message, options
end
setup_channel() click to toggle source
# File lib/mq_publisher/publisher.rb, line 19
def setup_channel
  @channel ||= connection.create_channel
end
setup_connection(options={}) click to toggle source
# File lib/mq_publisher/publisher.rb, line 15
def setup_connection(options={})
  @connection ||= Bunny.new(options).tap{ |c| c.start }
end
setup_exchange(options={}) click to toggle source
# File lib/mq_publisher/publisher.rb, line 23
def setup_exchange(options={})
  @exchange ||= Bunny::Exchange.new \
    channel,
    options[:exchange_type] || :fanout,
    options[:exchange_name],
    auto_delete: (options[:exchange_auto_delete].nil? ? false : options[:exchange_auto_delete]),
    durable: (options[:exchange_durable].nil? ? false : options[:exchange_durable])
end