module Netfira::WebConnect::AMQP

Public Class Methods

connect!() click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 7
def connect!
  disconnect!
  @connection = Bunny.new(Netfira::WebConnect.amqp_uri)
  @connection.start
  @channel = @connection.create_channel
  @exchanges = {}
end
connected?() click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 15
def connected?
  !!@connection
end
disconnect!() click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 19
def disconnect!
  return unless connected?
  @connection.stop
  @connection = nil
  @channel = nil
  @exchanges = nil
end
exchange_for_shop(shop) click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 27
def exchange_for_shop(shop)
  @exchanges && @exchanges[shop.id || 0] ||= make_exchange_for_shop(shop)
end
queue_name_for_shop(shop) click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 31
def queue_name_for_shop(shop)
  "shops.#{shop.id || 0}"
end

Private Class Methods

make_exchange_for_shop(shop) click to toggle source
# File lib/netfira/web_connect/amqp.rb, line 37
def make_exchange_for_shop(shop)

  # Each shop will have its own exchange, and a queue bound to that exchange.
  # Their names will be the same.
  name = queue_name_for_shop(shop)

  # Make an exchange
  @connection.direct(name).tap do |exchange|

    # Make a queue and bind it to the created exchange
    @channel.queue(name, auto_delete: true).bind exchange, routing_key: name
  end
end