Sha256: e92893b7242ec5de46e1f72b80f1de9171f448a3c2788edc3e59a3f6a51e4262

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 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

        def initialize(channel)
          @channel = channel
        end

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

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

        def on_return(&block)
          default_exchange.on_return do |return_info, properties, content|
            block.call(return_info[:reply_code], properties[:correlation_id])
          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

4 entries across 4 versions & 2 rubygems

Version Path
freddy-jruby-0.4.9 lib/freddy/adapters/bunny_adapter.rb
freddy-0.4.9 lib/freddy/adapters/bunny_adapter.rb
freddy-0.4.8 lib/freddy/adapters/bunny_adapter.rb
freddy-0.4.7 lib/freddy/adapters/bunny_adapter.rb