Class: PikaQue::Publisher

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Publisher

Returns a new instance of Publisher.



6
7
8
9
10
11
12
# File 'lib/pika_que/publisher.rb', line 6

def initialize(opts = {})
  @opts = PikaQue.config.merge(opts) 
  @codec = PikaQue::Util.constantize(@opts[:codec])
  @connection = @opts[:connection] || PikaQue.connection
  @channel = @connection.create_channel
  @exchange = @channel.exchange(@opts[:exchange], @opts[:exchange_options])
end

Instance Method Details

#exchange_nameObject



24
25
26
# File 'lib/pika_que/publisher.rb', line 24

def exchange_name
  @opts[:exchange]
end

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



14
15
16
17
18
19
20
21
22
# File 'lib/pika_que/publisher.rb', line 14

def publish(msg, options = {})
  to_queue = options.delete(:to_queue)
  options[:routing_key] ||= to_queue
  options[:content_type] ||= @codec.content_type
  msg = @codec.encode(msg)
  
  PikaQue.logger.info {"publishing <#{msg}> to [#{options[:routing_key]}]"}
  @exchange.publish(msg, options)
end