Sha256: eca9cbbc1d78f17240930c6d5f9b6b3355855908ab9d041ff82f49ba6cf4a26b

Contents?: true

Size: 1.11 KB

Versions: 23

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Praxis
  class MiddlewareApp
    attr_reader :target

    # Initialize the application instance with the desired args, and return the wrapping class.
    def self.for(**args)
      Class.new(self) do
        @args = args
        @setup_done = false
        def self.name
          'MiddlewareApp'
        end

        class << self
          attr_reader :args
        end

        class << self
          attr_reader :setup_done
        end

        def self.setup
          @setup_done = true
          Praxis::Application.instance.setup(**@args)
        end
      end
    end

    def initialize(inner)
      @target = inner
      @setup_done = false
    end

    def call(env)
      self.class.setup unless self.class.setup_done

      result = Praxis::Application.instance.call(env)

      if [404, 405].include?(result[0].to_i) && result[1]['X-Cascade'] == 'pass'
        last_body = result[2]
        last_body.close if last_body.respond_to? :close
        target.call(env)
      else
        # Respect X-Cascade header if it doesn't specify 'pass'
        result
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
praxis-2.0.pre.21 lib/praxis/middleware_app.rb
praxis-2.0.pre.20 lib/praxis/middleware_app.rb
praxis-2.0.pre.19 lib/praxis/middleware_app.rb