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.
15 16 17 18 19 |
# File 'lib/grape/middleware/base.rb', line 15 def initialize(app, *) @app = app @options = .any? ? .deep_merge(.shift) : @app_response = nil end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/grape/middleware/base.rb', line 9 def app @app end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
9 10 11 |
# File 'lib/grape/middleware/base.rb', line 9 def env @env end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/grape/middleware/base.rb', line 9 def @options end |
Instance Method Details
#after ⇒ Response?
This method is abstract.
Called after the application is called in the middleware lifecycle.
55 |
# File 'lib/grape/middleware/base.rb', line 55 def after; end |
#before ⇒ Object
This method is abstract.
Called before the application is called in the middleware lifecycle.
50 |
# File 'lib/grape/middleware/base.rb', line 50 def before; end |
#call(env) ⇒ Object
25 26 27 |
# File 'lib/grape/middleware/base.rb', line 25 def call(env) dup.call!(env).to_a end |
#call!(env) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/grape/middleware/base.rb', line 29 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
75 76 77 |
# File 'lib/grape/middleware/base.rb', line 75 def content_type content_type_for(env[Grape::Env::API_FORMAT] || [:format]) || TEXT_HTML end |
#content_type_for(format) ⇒ Object
71 72 73 |
# File 'lib/grape/middleware/base.rb', line 71 def content_type_for(format) content_types_indifferent_access[format] end |
#content_types ⇒ Object
63 64 65 |
# File 'lib/grape/middleware/base.rb', line 63 def content_types @content_types ||= Grape::ContentTypes.content_types_for([:content_types]) end |
#default_options ⇒ Object
21 22 23 |
# File 'lib/grape/middleware/base.rb', line 21 def {} end |
#mime_types ⇒ Object
67 68 69 |
# File 'lib/grape/middleware/base.rb', line 67 def mime_types @mime_types ||= Grape::ContentTypes.mime_types_for(content_types) end |
#response ⇒ Object
57 58 59 60 61 |
# File 'lib/grape/middleware/base.rb', line 57 def response return @app_response if @app_response.is_a?(Rack::Response) @app_response = Rack::Response.new(@app_response[2], @app_response[0], @app_response[1]) end |