Class: Grape::Middleware::Base
- Inherits:
-
Object
- Object
- Grape::Middleware::Base
- Includes:
- DSL::Headers, Helpers
- Defined in:
- lib/grape/middleware/base.rb
Direct Known Subclasses
Error, Filter, Formatter, Globals, Versioner::AcceptVersionHeader, Versioner::Header, Versioner::Param, Versioner::Path
Constant Summary collapse
- TEXT_HTML =
'text/html'
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#after ⇒ Response?
abstract
Called after the application is called in the middleware lifecycle.
-
#before ⇒ Object
abstract
Called before the application is called in the middleware lifecycle.
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
- #content_type ⇒ Object
- #content_type_for(format) ⇒ Object
- #content_types ⇒ Object
- #default_options ⇒ Object
-
#initialize(app, *options) ⇒ Base
constructor
A new instance of Base.
- #mime_types ⇒ Object
- #response ⇒ Object
Methods included from DSL::Headers
Methods included from Helpers
Constructor Details
#initialize(app, *options) ⇒ Base
Returns a new instance of Base.
18 19 20 21 22 |
# File 'lib/grape/middleware/base.rb', line 18 def initialize(app, *) @app = app @options = .any? ? .merge(.shift) : @app_response = nil end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
10 11 12 |
# File 'lib/grape/middleware/base.rb', line 10 def app @app end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
10 11 12 |
# File 'lib/grape/middleware/base.rb', line 10 def env @env end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/grape/middleware/base.rb', line 10 def @options end |
Instance Method Details
#after ⇒ Response?
This method is abstract.
Called after the application is called in the middleware lifecycle.
58 |
# File 'lib/grape/middleware/base.rb', line 58 def after; end |
#before ⇒ Object
This method is abstract.
Called before the application is called in the middleware lifecycle.
53 |
# File 'lib/grape/middleware/base.rb', line 53 def before; end |
#call(env) ⇒ Object
28 29 30 |
# File 'lib/grape/middleware/base.rb', line 28 def call(env) dup.call!(env).to_a end |
#call!(env) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/grape/middleware/base.rb', line 32 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.}" raise e end end response = after_response || @app_response merge_headers response response end |
#content_type ⇒ Object
74 75 76 |
# File 'lib/grape/middleware/base.rb', line 74 def content_type content_type_for(env[Grape::Env::API_FORMAT] || [:format]) || TEXT_HTML end |
#content_type_for(format) ⇒ Object
66 67 68 |
# File 'lib/grape/middleware/base.rb', line 66 def content_type_for(format) HashWithIndifferentAccess.new(content_types)[format] end |
#content_types ⇒ Object
70 71 72 |
# File 'lib/grape/middleware/base.rb', line 70 def content_types ContentTypes.content_types_for([:content_types]) end |
#default_options ⇒ Object
24 25 26 |
# File 'lib/grape/middleware/base.rb', line 24 def {} end |
#mime_types ⇒ Object
78 79 80 81 82 |
# File 'lib/grape/middleware/base.rb', line 78 def mime_types @mime_type ||= content_types.each_pair.with_object({}) do |(k, v), types_without_params| types_without_params[v.split(';').first] = k end end |