Class: Pinky::EnergizerBunny::Connection
- Inherits:
-
Object
- Object
- Pinky::EnergizerBunny::Connection
- Defined in:
- lib/pinky/energizer_bunny/connection.rb
Instance Method Summary collapse
- #connected? ⇒ Boolean
- #connection_url ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(config, logger = Rails.logger) ⇒ Connection
constructor
A new instance of Connection.
- #publish(topic_key, message, opts = {}) ⇒ Object
- #subscribe(topic_key, subscription_opts = {}, &block) ⇒ Object
Constructor Details
#initialize(config, logger = Rails.logger) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 7 def initialize config, logger = Rails.logger @config = config @logger = logger return unless enabled? at_exit { close } @exchanges = Hash.new { |exchanges, topic_key| exchanges[topic_key] = create_exchange topic_key } @queues = Hash.new { |queues, topic_key| queues[topic_key] = create_queue topic_key } @subscriptions = [] create_connection end |
Instance Method Details
#connected? ⇒ Boolean
28 29 30 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 28 def connected? @connection && @connection.open? end |
#connection_url ⇒ Object
36 37 38 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 36 def connection_url @config[:broker][:url] end |
#enabled? ⇒ Boolean
32 33 34 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 32 def enabled? @config[:enabled] end |
#publish(topic_key, message, opts = {}) ⇒ Object
23 24 25 26 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 23 def publish topic_key, , opts = {} opts @exchanges[topic_key].publish , opts if enabled? end |
#subscribe(topic_key, subscription_opts = {}, &block) ⇒ Object
18 19 20 21 |
# File 'lib/pinky/energizer_bunny/connection.rb', line 18 def subscribe topic_key, subscription_opts = {}, &block return unless enabled? @subscriptions << Subscription.new(@queues[topic_key], subscription_opts, @logger, block) end |