Class: Sneakers::Publisher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Publisher

Returns a new instance of Publisher.



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

def initialize(opts = {})
  @mutex = Mutex.new
  @opts = Sneakers::CONFIG.merge(opts)
  # If we've already got a bunny object, use it.  This allows people to
  # specify all kinds of options we don't need to know about (e.g. for ssl).
  @bunny = @opts[:connection]
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



4
5
6
# File 'lib/sneakers/publisher.rb', line 4

def channel
  @channel
end

#exchangeObject (readonly)

Returns the value of attribute exchange.



4
5
6
# File 'lib/sneakers/publisher.rb', line 4

def exchange
  @exchange
end

Instance Method Details

#ensure_connection!Object



24
25
26
27
28
# File 'lib/sneakers/publisher.rb', line 24

def ensure_connection!
  @mutex.synchronize do
    connect! unless connected?
  end
end

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



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

def publish(msg, options = {})
  ensure_connection!
  to_queue = options.delete(:to_queue)
  options[:routing_key] ||= to_queue
  Sneakers.logger.info {"publishing <#{msg}> to [#{options[:routing_key]}]"}
  serialized_msg = Sneakers::ContentType.serialize(msg, options[:content_type])
  encoded_msg = Sneakers::ContentEncoding.encode(serialized_msg, options[:content_encoding])
  @exchange.publish(encoded_msg, options)
end