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, 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)

    Options forwarded to the subclass. When the subclass declares an ‘Options` Data class, the kwargs are routed through it and exposed via #config; #options keeps returning a frozen Hash representation for back-compat with subclasses that read `options`. Otherwise the kwargs are deep-merged with the subclass’s ‘DEFAULT_OPTIONS` Hash (legacy path) and frozen.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grape/middleware/base.rb', line 17

def initialize(app, **options)
  @app = app
  if self.class.const_defined?(:Options)
    # Search ancestors so subclasses (e.g. Versioner::Path → Versioner::Base)
    # inherit their parent's Options Data class without redeclaring it.
    @config = self.class::Options.new(**options)
    @options = @config.to_h.freeze
  else
    @options = merge_default_options(options).freeze
  end
  @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

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
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.



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

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before; end

#call(env) ⇒ Object



30
31
32
# File 'lib/grape/middleware/base.rb', line 30

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

#call!(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/grape/middleware/base.rb', line 34

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

#contextObject



66
67
68
# File 'lib/grape/middleware/base.rb', line 66

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

#query_paramsObject



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

def query_params
  rack_request.GET
rescue *Grape::RACK_ERRORS
  raise Grape::Exceptions::RequestError
end

#rack_requestObject



62
63
64
# File 'lib/grape/middleware/base.rb', line 62

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

#responseObject



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

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

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