Class: Huck::Senders::RabbitMQSender

Inherits:
Huck::Sender show all
Defined in:
lib/huck/senders/rabbitmq.rb

Overview

A sender that talks to RabbitMQ

Instance Attribute Summary

Attributes inherited from Huck::Sender

#config

Instance Method Summary collapse

Methods inherited from Huck::Sender

factory

Constructor Details

#initializeRabbitMQSender

Includes all required modules for the RabbitMQ sender



9
10
11
# File 'lib/huck/senders/rabbitmq.rb', line 9

def initialize
  Huck::must_load 'bunny'
end

Instance Method Details

#send(msg) ⇒ Object

Send an arbitrary text message to the queue for processing

Parameters:

msg

The message to process



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/huck/senders/rabbitmq.rb', line 30

def send msg
  verify_config

  options = Hash.new
  [:host, :port, :user, :pass, :vhost].each do |arg|
    if @config['rabbitmq'].has_key? arg.to_s
      options[arg] = @config['rabbitmq'][arg.to_s]
    end
  end

  conn = Bunny.new options
  conn.start

  ch = conn.create_channel
  queue = ch.queue(@config['rabbitmq']['queue_name'])
  xfer = ch.default_exchange

  xfer.publish msg, :routing_key => @config['rabbitmq']['queue_name']
end

#verify_configObject

Ensures that configuration is set properly before trying to use the connection data to talk to rabbitmq



15
16
17
18
19
20
21
22
# File 'lib/huck/senders/rabbitmq.rb', line 15

def verify_config
  if !@config.has_key? 'rabbitmq'
    raise Huck::Error, 'missing rabbitmq sender config'
  end
  if !@config['rabbitmq'].has_key? 'queue_name'
    raise Huck::Error, 'missing rabbitmq sender config: queue_name'
  end
end