Module: DramaQueen::Consumer

Defined in:
lib/drama_queen/consumer.rb

Overview

A consumer is simply an object that receives messages from a producer. In order to sign up to receive messages from a producer, the consumer subscribes to a topic that they’re interested in. When the producer publishes something on that topic, the consumer’s callback will get called.

Instance Method Summary collapse

Instance Method Details

#subscribe(routing_key, callback) ⇒ Object

Parameters:

  • routing_key (Object)

    The routing key that represents the Exchange to subscribe to.

  • callback (Symbol, Method, Proc)

    If given a Symbol, this will be converted to a Method that gets called on the includer of Consumer. If callback is not a Symbol, it simply must just respond to call.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/drama_queen/consumer.rb', line 19

def subscribe(routing_key, callback)
  callable_callback = callback.is_a?(Symbol) ? method(callback) : callback

  unless callable_callback.respond_to?(:call)
    raise "The given callback is not a Symbol, nor responds to #call: #{callback}"
  end

  unless DramaQueen.routes_to? routing_key
    DramaQueen.exchanges << Exchange.new(routing_key)
  end

  exchange = DramaQueen.exchange_for(routing_key)
  exchange.subscribers << callable_callback
end