Sha256: eed3d7acc523fb03ce8c31fe4eb1b7865ca6b7f0d92c93b79e5a956135708440

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

shared_context :async do
  class Callback
    attr_reader :response

    def call *args
      status, headers, body = *args.flatten.to_a
      @response = Rack::Response.new(body, status, headers)
      EventMachine.stop if EventMachine.reactor_running?
    end
  end

  def callback
    @callback ||= Callback.new
  end

  def async_response_handler
    @async_response_handler ||= begin
      instance_double(EM::DefaultDeferrable).tap do |handler|
        allow(handler).to receive(:succeed) do |arg|
          sitehub_response = arg[:downstream_response]
          handler.instance_variable_set(:@arg, arg)
          sitehub_response = sitehub_response.to_a
          handler.instance_variable_set(:@last_response, Rack::Response.new(sitehub_response[2], sitehub_response[0], sitehub_response[1]))
          EM.stop
        end

        def handler.last_response
          @last_response
        end

        def handler.callback_args
          @arg
        end
      end
    end
  end

  def last_response
    app.last_response
  end

  def async_middleware
    Class.new do
      def initialize app
        @app = app
      end

      def last_response
        callback.response
      end

      def callback
        @callback ||= Callback.new
      end


      def call env

        env['async.callback'] = callback

        catch(:async) do
          @app.call env
        end

        [200, {}, []]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sitehub-0.4.2 spec/support/shared_contexts/async_context.rb
sitehub-0.4.1 spec/support/shared_contexts/async_context.rb