Sha256: d92787f88c0eed14047fd270e7378ebe73ab70d5b7fe57b63ac5d420e95858b0

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

require 'bunny'

class Freddy
  module Adapters
    class BunnyAdapter
      def self.connect(config)
        bunny = Bunny.new(config)
        bunny.start
        new(bunny)
      end

      def initialize(bunny)
        @bunny = bunny
      end

      def create_channel
        Channel.new(@bunny.create_channel)
      end

      def close
        @bunny.close
      end

      class Channel
        extend Forwardable

        NO_ROUTE = 312

        def initialize(channel)
          @channel = channel
        end

        def_delegators :@channel, :topic, :default_exchange, :consumers

        def queue(*args)
          Queue.new(@channel.queue(*args))
        end

        def on_no_route(&block)
          default_exchange.on_return do |return_info, properties, content|
            if return_info[:reply_code] == NO_ROUTE
              block.call(properties[:correlation_id])
            end
          end
        end
      end

      class Queue
        def initialize(queue)
          @queue = queue
        end

        def subscribe(&block)
          @queue.subscribe do |info, properties, payload|
            parsed_payload = Payload.parse(payload)
            block.call(Delivery.new(parsed_payload, properties, info.routing_key))
          end
        end

        def bind(*args)
          @queue.bind(*args)
          self
        end

        def name
          @queue.name
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
freddy-0.5.3 lib/freddy/adapters/bunny_adapter.rb
freddy-0.5.2 lib/freddy/adapters/bunny_adapter.rb
freddy-jruby-0.5.1 lib/freddy/adapters/bunny_adapter.rb
freddy-0.5.1 lib/freddy/adapters/bunny_adapter.rb
freddy-jruby-0.5.0 lib/freddy/adapters/bunny_adapter.rb
freddy-0.5.0 lib/freddy/adapters/bunny_adapter.rb