Class: Grape::Middleware::Base
- Inherits:
-
Object
- Object
- Grape::Middleware::Base
- Defined in:
- lib/grape/middleware/base.rb
Direct Known Subclasses
Auth::Basic, Auth::Digest, Auth::OAuth2, Error, Filter, Formatter, Versioner::Header, Versioner::Param, Versioner::Path
Constant Summary collapse
- CONTENT_TYPES =
Content types are listed in order of preference.
ActiveSupport::OrderedHash[ :xml, 'application/xml', :serializable_hash, 'application/json', :json, 'application/json', :atom, 'application/atom+xml', :rss, 'application/rss+xml', :txt, 'text/plain', ]
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
- #request ⇒ Object
- #response ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Base
Returns a new instance of Base.
23 24 25 26 |
# File 'lib/grape/middleware/base.rb', line 23 def initialize(app, = {}) @app = app @options = .merge() end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
19 20 21 |
# File 'lib/grape/middleware/base.rb', line 19 def app @app end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
19 20 21 |
# File 'lib/grape/middleware/base.rb', line 19 def env @env end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/grape/middleware/base.rb', line 19 def @options end |
Instance Method Details
#after ⇒ Response?
This method is abstract.
Called after the application is called in the middleware lifecycle.
49 |
# File 'lib/grape/middleware/base.rb', line 49 def after; end |
#before ⇒ Object
This method is abstract.
Called before the application is called in the middleware lifecycle.
45 |
# File 'lib/grape/middleware/base.rb', line 45 def before; end |
#call(env) ⇒ Object
32 33 34 |
# File 'lib/grape/middleware/base.rb', line 32 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/grape/middleware/base.rb', line 36 def call!(env) @env = env before @app_response = @app.call(@env) after || @app_response end |
#content_type ⇒ Object
67 68 69 |
# File 'lib/grape/middleware/base.rb', line 67 def content_type content_type_for(env['api.format'] || [:format]) || 'text/html' end |
#content_type_for(format) ⇒ Object
59 60 61 |
# File 'lib/grape/middleware/base.rb', line 59 def content_type_for(format) HashWithIndifferentAccess.new(content_types)[format] end |
#content_types ⇒ Object
63 64 65 |
# File 'lib/grape/middleware/base.rb', line 63 def content_types [:content_types] || CONTENT_TYPES end |
#default_options ⇒ Object
28 29 30 |
# File 'lib/grape/middleware/base.rb', line 28 def {} end |
#mime_types ⇒ Object
71 72 73 |
# File 'lib/grape/middleware/base.rb', line 71 def mime_types content_types.invert end |
#request ⇒ Object
51 52 53 |
# File 'lib/grape/middleware/base.rb', line 51 def request Rack::Request.new(self.env) end |
#response ⇒ Object
55 56 57 |
# File 'lib/grape/middleware/base.rb', line 55 def response Rack::Response.new(@app_response) end |