Sha256: fe56505201ed8c0ffcca9d0aa7d672d2a8e69b10a79f2623deeaee2dce0315ce

Contents?: true

Size: 969 Bytes

Versions: 6

Compression:

Stored size: 969 Bytes

Contents

module MessageDriver
  module Middleware
    class BlockMiddleware < Base
      attr_reader :on_publish_block, :on_consume_block

      def initialize(destination, opts)
        super(destination)
        fail ArgumentError, 'you must provide at least one of :on_publish and :on_consume' \
          unless opts.keys.any? { |k| [:on_publish, :on_consume].include? k }
        @on_publish_block = opts[:on_publish]
        @on_consume_block = opts[:on_consume]
      end

      def on_publish(body, headers, properties)
        delegate_to_block(on_publish_block, body, headers, properties)
      end

      def on_consume(body, headers, properties)
        delegate_to_block(on_consume_block, body, headers, properties)
      end

      private

      def delegate_to_block(block, body, headers, properties)
        if block.nil?
          [body, headers, properties]
        else
          block.call(body, headers, properties)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
message-driver-0.6.1 lib/message_driver/middleware/block_middleware.rb
message-driver-0.6.0 lib/message_driver/middleware/block_middleware.rb
message-driver-0.5.3 lib/message_driver/middleware/block_middleware.rb
message-driver-0.5.2 lib/message_driver/middleware/block_middleware.rb
message-driver-0.5.1 lib/message_driver/middleware/block_middleware.rb
message-driver-0.5.0 lib/message_driver/middleware/block_middleware.rb