|
3 | 3 | module Grape |
4 | 4 | module Middleware |
5 | 5 | class Base |
6 | | - include Helpers |
7 | 6 | include Grape::DSL::Headers |
8 | 7 |
|
9 | 8 | attr_reader :app, :env, :options |
10 | 9 |
|
11 | 10 | # @param [Rack Application] app The standard argument for a Rack middleware. |
12 | 11 | # @param [Hash] options A hash of options, simply stored for use by subclasses. |
13 | | - def initialize(app, *options) |
| 12 | + def initialize(app, **options) |
14 | 13 | @app = app |
15 | | - @options = options.any? ? default_options.deep_merge(options.shift) : default_options |
| 14 | + @options = merge_default_options(options) |
16 | 15 | @app_response = nil |
17 | 16 | end |
18 | 17 |
|
19 | | - def default_options |
20 | | - {} |
21 | | - end |
22 | | - |
23 | 18 | def call(env) |
24 | 19 | dup.call!(env).to_a |
25 | 20 | end |
@@ -56,10 +51,14 @@ def rack_request |
56 | 51 | @rack_request ||= Rack::Request.new(env) |
57 | 52 | end |
58 | 53 |
|
| 54 | + def context |
| 55 | + env[Grape::Env::API_ENDPOINT] |
| 56 | + end |
| 57 | + |
59 | 58 | def response |
60 | 59 | return @app_response if @app_response.is_a?(Rack::Response) |
61 | 60 |
|
62 | | - @app_response = Rack::Response.new(@app_response[2], @app_response[0], @app_response[1]) |
| 61 | + @app_response = Rack::Response[*@app_response] |
63 | 62 | end |
64 | 63 |
|
65 | 64 | def content_types |
@@ -100,6 +99,16 @@ def merge_headers(response) |
100 | 99 | def content_types_indifferent_access |
101 | 100 | @content_types_indifferent_access ||= content_types.with_indifferent_access |
102 | 101 | end |
| 102 | + |
| 103 | + def merge_default_options(options) |
| 104 | + if respond_to?(:default_options) |
| 105 | + default_options.deep_merge(options) |
| 106 | + elsif self.class.const_defined?(:DEFAULT_OPTIONS) |
| 107 | + self.class::DEFAULT_OPTIONS.deep_merge(options) |
| 108 | + else |
| 109 | + options |
| 110 | + end |
| 111 | + end |
103 | 112 | end |
104 | 113 | end |
105 | 114 | end |
0 commit comments