class Grape::Middleware::Base
Constants
- TEXT_HTML
Attributes
app[R]
env[R]
options[R]
Public Class Methods
new(app, **options)
click to toggle source
@param [Rack Application] app The standard argument for a Rack middleware. @param [Hash] options A hash of options, simply stored for use by subclasses.
# File lib/grape/middleware/base.rb, line 13 def initialize(app, **options) @app = app @options = default_options.merge(**options) @app_response = nil end
Public Instance Methods
after()
click to toggle source
@abstract Called after the application is called in the middleware lifecycle. @return [Response, nil] a Rack SPEC response or nil to call the application afterwards.
# File lib/grape/middleware/base.rb, line 53 def after; end
before()
click to toggle source
@abstract Called before the application is called in the middleware lifecycle.
# File lib/grape/middleware/base.rb, line 48 def before; end
call(env)
click to toggle source
# File lib/grape/middleware/base.rb, line 23 def call(env) dup.call!(env) end
call!(env)
click to toggle source
# File lib/grape/middleware/base.rb, line 27 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 merge_headers response response end
content_type()
click to toggle source
# File lib/grape/middleware/base.rb, line 68 def content_type content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || TEXT_HTML end
content_type_for(format)
click to toggle source
# File lib/grape/middleware/base.rb, line 60 def content_type_for(format) HashWithIndifferentAccess.new(content_types)[format] end
content_types()
click to toggle source
# File lib/grape/middleware/base.rb, line 64 def content_types ContentTypes.content_types_for(options[:content_types]) end
default_options()
click to toggle source
# File lib/grape/middleware/base.rb, line 19 def default_options {} end
mime_types()
click to toggle source
# File lib/grape/middleware/base.rb, line 72 def mime_types types_without_params = {} content_types.each_pair do |k, v| types_without_params[v.split(';').first] = k end types_without_params end
response()
click to toggle source
# File lib/grape/middleware/base.rb, line 55 def response return @app_response if @app_response.is_a?(Rack::Response) Rack::Response.new(@app_response[2], @app_response[0], @app_response[1]) end
Private Instance Methods
merge_headers(response)
click to toggle source
# File lib/grape/middleware/base.rb, line 82 def merge_headers(response) return unless headers.is_a?(Hash) case response when Rack::Response then response.headers.merge!(headers) when Array then response[1].merge!(headers) end end