Sha256: 65fe39e8c9f2cbe1f49036bea46dde442c3a5c7939b2d4c7a9bb25ab10551429

Contents?: true

Size: 1.04 KB

Versions: 19

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

class Freddy
  module MessageHandlerAdapters
    class Factory
      def initialize(producer)
        @no_op_handler = NoOpHandler.new
        @request_handler = RequestHandler.new(producer)
      end

      def for(delivery)
        if delivery.type == 'request'
          @request_handler
        else
          @no_op_handler
        end
      end
    end

    class NoOpHandler
      def success(*)
        # NOP
      end

      def error(*)
        # NOP
      end
    end

    class RequestHandler
      def initialize(producer)
        @producer = producer
      end

      def success(delivery, response)
        send_response(delivery, response, type: 'success')
      end

      def error(delivery, response)
        send_response(delivery, response, type: 'error')
      end

      private

      def send_response(delivery, response, opts = {})
        @producer.produce delivery.reply_to.force_encoding('utf-8'), response, {
          correlation_id: delivery.correlation_id
        }.merge(opts)
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
freddy-2.8.0 lib/freddy/message_handler_adapaters.rb
freddy-2.7.0 lib/freddy/message_handler_adapaters.rb
freddy-2.6.0 lib/freddy/message_handler_adapaters.rb
freddy-2.5.1 lib/freddy/message_handler_adapaters.rb
freddy-2.5.0 lib/freddy/message_handler_adapaters.rb
freddy-2.5.0.pre.rc.1 lib/freddy/message_handler_adapaters.rb
freddy-2.4.0 lib/freddy/message_handler_adapaters.rb
freddy-2.3.0 lib/freddy/message_handler_adapaters.rb
freddy-2.2.4 lib/freddy/message_handler_adapaters.rb
freddy-2.2.3 lib/freddy/message_handler_adapaters.rb
freddy-2.2.2 lib/freddy/message_handler_adapaters.rb
freddy-2.2.1 lib/freddy/message_handler_adapaters.rb
freddy-2.2.0 lib/freddy/message_handler_adapaters.rb
freddy-2.1.0 lib/freddy/message_handler_adapaters.rb
freddy-2.0.0 lib/freddy/message_handler_adapaters.rb
freddy-1.7.0 lib/freddy/message_handler_adapaters.rb
freddy-1.6.0 lib/freddy/message_handler_adapaters.rb
freddy-1.5.0 lib/freddy/message_handler_adapaters.rb
freddy-1.4.2 lib/freddy/message_handler_adapaters.rb