Class: Grape::Middleware::Base

Inherits:
Object
  • Object
show all
Includes:
DSL::Headers
Defined in:
lib/grape/middleware/base.rb

Direct Known Subclasses

Auth::Base, Error, Filter, Formatter, Globals, Versioner::Base

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Headers

#header

Constructor Details

#initialize(app, **options) ⇒ Base

Returns a new instance of Base.

Parameters:

  • app (Rack Application)

    The standard argument for a Rack middleware.

  • options (Hash)

    A hash of options, simply stored for use by subclasses.



12
13
14
15
16
# File 'lib/grape/middleware/base.rb', line 12

def initialize(app, **options)
  @app = app
  @options = merge_default_options(options)
  @app_response = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/grape/middleware/base.rb', line 8

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/grape/middleware/base.rb', line 8

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/grape/middleware/base.rb', line 8

def options
  @options
end

Instance Method Details

#afterResponse?

This method is abstract.

Called after the application is called in the middleware lifecycle.

Returns:

  • (Response, nil)

    a Rack SPEC response or nil to call the application afterwards.



48
# File 'lib/grape/middleware/base.rb', line 48

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



43
# File 'lib/grape/middleware/base.rb', line 43

def before; end

#call(env) ⇒ Object



18
19
20
# File 'lib/grape/middleware/base.rb', line 18

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

#call!(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grape/middleware/base.rb', line 22

def call!(env)
  @env = env
  before
  begin
    @app_response = @app.call(@env)
  ensure
    begin
      after_response = after
    rescue StandardError => e
      warn "caught error of type #{e.class} in after callback inside #{self.class.name} : #{e.message}"
      raise e
    end
  end

  response = after_response || @app_response
  merge_headers response
  response
end

#content_typeObject



76
77
78
# File 'lib/grape/middleware/base.rb', line 76

def content_type
  content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || 'text/html'
end

#content_type_for(format) ⇒ Object



72
73
74
# File 'lib/grape/middleware/base.rb', line 72

def content_type_for(format)
  content_types_indifferent_access[format]
end

#content_typesObject



64
65
66
# File 'lib/grape/middleware/base.rb', line 64

def content_types
  @content_types ||= Grape::ContentTypes.content_types_for(options[:content_types])
end

#contextObject



54
55
56
# File 'lib/grape/middleware/base.rb', line 54

def context
  env[Grape::Env::API_ENDPOINT]
end

#mime_typesObject



68
69
70
# File 'lib/grape/middleware/base.rb', line 68

def mime_types
  @mime_types ||= Grape::ContentTypes.mime_types_for(content_types)
end

#query_paramsObject



80
81
82
83
84
85
86
# File 'lib/grape/middleware/base.rb', line 80

def query_params
  rack_request.GET
rescue Rack::QueryParser::ParamsTooDeepError
  raise Grape::Exceptions::TooDeepParameters.new(Rack::Utils.param_depth_limit)
rescue Rack::Utils::ParameterTypeError
  raise Grape::Exceptions::ConflictingTypes
end

#rack_requestObject



50
51
52
# File 'lib/grape/middleware/base.rb', line 50

def rack_request
  @rack_request ||= Rack::Request.new(env)
end

#responseObject



58
59
60
61
62
# File 'lib/grape/middleware/base.rb', line 58

def response
  return @app_response if @app_response.is_a?(Rack::Response)

  @app_response = Rack::Response[*@app_response]
end