Class: Grape::Middleware::Base

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

Direct Known Subclasses

Auth::Basic, Auth::OAuth2, Error, Formatter, Prefixer, Versioner

Instance Attribute Summary collapse

Instance Method Summary collapse

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) (defaults to: {})

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



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

def initialize(app, options = {})
  @app = app
  @options = default_options.merge(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

def options
  @options
end

Instance Method Details

#afterObject

This method is abstract.

Called after the application is called in the middleware lifecycle.



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

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before; end

#call(env) ⇒ Object



15
16
17
# File 'lib/grape/middleware/base.rb', line 15

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

#call!(env) ⇒ Object



19
20
21
22
23
24
# File 'lib/grape/middleware/base.rb', line 19

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

#default_optionsObject



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

def default_options; {} end

#requestObject



34
35
36
# File 'lib/grape/middleware/base.rb', line 34

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

#responseObject



38
39
40
# File 'lib/grape/middleware/base.rb', line 38

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