Class: Grape::Middleware::Base
- Inherits:
-
Object
- Object
- Grape::Middleware::Base
show all
- Includes:
- DSL::Headers
- Defined in:
- lib/grape/middleware/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#header
Constructor Details
#initialize(app, **options) ⇒ Base
Returns a new instance of Base.
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
#app ⇒ Object
Returns the value of attribute app.
8
9
10
|
# File 'lib/grape/middleware/base.rb', line 8
def app
@app
end
|
#env ⇒ Object
Returns the value of attribute env.
8
9
10
|
# File 'lib/grape/middleware/base.rb', line 8
def env
@env
end
|
#options ⇒ Object
Returns the value of attribute options.
8
9
10
|
# File 'lib/grape/middleware/base.rb', line 8
def options
@options
end
|
Instance Method Details
#after ⇒ Response?
Called after the application is called in the middleware lifecycle.
48
|
# File 'lib/grape/middleware/base.rb', line 48
def after; end
|
#before ⇒ Object
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
response
response
end
|
#content_type ⇒ Object
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
|
#context ⇒ Object
54
55
56
|
# File 'lib/grape/middleware/base.rb', line 54
def context
env[Grape::Env::API_ENDPOINT]
end
|
#rack_request ⇒ Object
50
51
52
|
# File 'lib/grape/middleware/base.rb', line 50
def rack_request
@rack_request ||= Rack::Request.new(env)
end
|
#response ⇒ Object
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
|