Class: Grape::Middleware::Base
- Inherits:
-
Object
- Object
- Grape::Middleware::Base
- Includes:
- DSL::Headers
- Defined in:
- lib/grape/middleware/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#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
- #context ⇒ Object
-
#initialize(app, **options) ⇒ Base
constructor
A new instance of Base.
- #query_params ⇒ Object
- #rack_request ⇒ Object
- #response ⇒ Object
Methods included from DSL::Headers
Constructor Details
#initialize(app, **options) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/grape/middleware/base.rb', line 17 def initialize(app, **) @app = app if self.class.const_defined?(:Options) # Search ancestors so subclasses (e.g. Versioner::Path → Versioner::Base) # inherit their parent's Options Data class without redeclaring it. @config = self.class::Options.new(**) @options = @config.to_h.freeze else @options = ().freeze end @app_response = nil end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/grape/middleware/base.rb', line 8 def app @app end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/grape/middleware/base.rb', line 8 def config @config end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
8 9 10 |
# File 'lib/grape/middleware/base.rb', line 8 def env @env end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/grape/middleware/base.rb', line 8 def @options end |
Instance Method Details
#after ⇒ Response?
This method is abstract.
Called after the application is called in the middleware lifecycle.
60 |
# File 'lib/grape/middleware/base.rb', line 60 def after; end |
#before ⇒ Object
This method is abstract.
Called before the application is called in the middleware lifecycle.
55 |
# File 'lib/grape/middleware/base.rb', line 55 def before; end |
#call(env) ⇒ Object
30 31 32 |
# File 'lib/grape/middleware/base.rb', line 30 def call(env) dup.call!(env).to_a end |
#call!(env) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/grape/middleware/base.rb', line 34 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 |
#context ⇒ Object
66 67 68 |
# File 'lib/grape/middleware/base.rb', line 66 def context env[Grape::Env::API_ENDPOINT] end |
#query_params ⇒ Object
76 77 78 79 80 |
# File 'lib/grape/middleware/base.rb', line 76 def query_params rack_request.GET rescue *Grape::RACK_ERRORS raise Grape::Exceptions::RequestError end |
#rack_request ⇒ Object
62 63 64 |
# File 'lib/grape/middleware/base.rb', line 62 def rack_request @rack_request ||= Rack::Request.new(env) end |
#response ⇒ Object
70 71 72 73 74 |
# File 'lib/grape/middleware/base.rb', line 70 def response return @app_response if @app_response.is_a?(Rack::Response) @app_response = Rack::Response[*@app_response] end |