Sha256: bcce32c74b0913724772fe15078398db296b407808636d5b34ef50b78fa7d9d3

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

module Grape
  module Middleware
    class Base
      attr_reader :app, :env, :options

      # @param [Rack Application] app The standard argument for a Rack middleware.
      # @param [Hash] options A hash of options, simply stored for use by subclasses.
      def initialize(app, options = {})
        @app = app
        @options = default_options.merge(options)
      end

      def default_options
        {}
      end

      def call(env)
        dup.call!(env)
      end

      def call!(env)
        @env = env
        before
        @app_response = @app.call(@env)
        after || @app_response
      end

      # @abstract
      # Called before the application is called in the middleware lifecycle.
      def before; end
      # @abstract
      # Called after the application is called in the middleware lifecycle.
      # @return [Response, nil] a Rack SPEC response or nil to call the application afterwards.
      def after; end

      def request
        Rack::Request.new(self.env)
      end

      def response
        Rack::Response.new(@app_response)
      end

      def content_type_for(format)
        HashWithIndifferentAccess.new(content_types)[format]
      end

      def content_types
        ContentTypes.content_types_for(options[:content_types])
      end

      def content_type
        content_type_for(env['api.format'] || options[:format]) || 'text/html'
      end

      def mime_types
        content_types.invert
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
grape-0.4.1 lib/grape/middleware/base.rb
grape-0.4.0 lib/grape/middleware/base.rb
grape-0.3.2 lib/grape/middleware/base.rb
grape-0.3.1 lib/grape/middleware/base.rb
grape-0.3.0 lib/grape/middleware/base.rb