-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbase.rb
105 lines (84 loc) · 2.82 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true
module Grape
module Middleware
class Base
include Helpers
include Grape::DSL::Headers
attr_reader :app, :env, :options
# @param [Rack Application] app The standard argument for a Rack middleware.
# @param [Hash] options A hash of options, simply stored for use by subclasses.
def initialize(app, *options)
@app = app
@options = options.any? ? default_options.deep_merge(options.shift) : default_options
@app_response = nil
end
def default_options
{}
end
def call(env)
dup.call!(env).to_a
end
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
# @abstract
# Called before the application is called in the middleware lifecycle.
def before; end
# @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.
def after; end
def rack_request
@rack_request ||= Rack::Request.new(env)
end
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
def content_types
@content_types ||= Grape::ContentTypes.content_types_for(options[:content_types])
end
def mime_types
@mime_types ||= Grape::ContentTypes.mime_types_for(content_types)
end
def content_type_for(format)
content_types_indifferent_access[format]
end
def content_type
content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || 'text/html'
end
def query_params
rack_request.GET
rescue Rack::QueryParser::ParamsTooDeepError
raise Grape::Exceptions::TooDeepParameters.new(Rack::Utils.param_depth_limit)
rescue Rack::Utils::ParameterTypeError
raise Grape::Exceptions::ConflictingTypes
end
private
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
def content_types_indifferent_access
@content_types_indifferent_access ||= content_types.with_indifferent_access
end
end
end
end