Class: Grape::Middleware::Base

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

Defined Under Namespace

Modules: Formats

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.



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

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



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

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.



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

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before; end

#call(env) ⇒ Object



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

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

#call!(env) ⇒ Object



22
23
24
25
26
27
# File 'lib/grape/middleware/base.rb', line 22

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

#default_optionsObject



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

def default_options; {} end

#requestObject



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

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

#responseObject



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

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