Class: Pinky::EnergizerBunny::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/pinky/energizer_bunny/connection.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/pinky/energizer_bunny/connection.rb', line 28

def connected?
  @connection && @connection.open?
end

#connection_urlObject



36
37
38
# File 'lib/pinky/energizer_bunny/connection.rb', line 36

def connection_url
  @config[:broker][:url]
end

#enabled?Boolean

Returns:

  • (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, message, opts = {}
  add_message_id_to_header! opts
  @exchanges[topic_key].publish message, 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