forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
188 lines (158 loc) · 5.66 KB
/
api.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
module Grape
# The API class is the primary entry point for
# creating Grape APIs.Users should subclass this
# class in order to build an API.
class API
include Grape::DSL::API
class << self
attr_reader :instance
LOCK = Mutex.new
def reset!
@route_set = Rack::Mount::RouteSet.new
@endpoints = []
@routes = nil
reset_validations!
end
def compile
@instance ||= new
end
def change!
@instance = nil
end
def call(env)
LOCK.synchronize { compile } unless instance
call!(env)
end
def call!(env)
instance.call(env)
end
# Create a scope without affecting the URL.
#
# @param name [Symbol] Purely placebo, just allows to name the scope to make the code more readable.
def scope(name = nil, &block)
within_namespace do
nest(block)
end
end
def cascade(value = nil)
if value.nil?
inheritable_setting.namespace_inheritable.keys.include?(:cascade) ? !!namespace_inheritable(:cascade) : true
else
namespace_inheritable(:cascade, value)
end
end
protected
def prepare_routes
routes = []
endpoints.each do |endpoint|
routes.concat(endpoint.routes)
end
routes
end
# Execute first the provided block, then each of the
# block passed in. Allows for simple 'before' setups
# of settings stack pushes.
def nest(*blocks, &block)
blocks.reject! { |b| b.nil? }
if blocks.any?
instance_eval(&block) if block_given?
blocks.each { |b| instance_eval(&b) }
reset_validations!
else
instance_eval(&block)
end
end
def inherited(subclass)
subclass.reset!
subclass.logger = logger.clone
end
def inherit_settings(other_settings)
top_level_setting.inherit_from other_settings.point_in_time_copy
endpoints.each do |e|
e.reset_routes!
end
@routes = nil
end
end
def initialize
@route_set = Rack::Mount::RouteSet.new
add_head_not_allowed_methods_and_options_methods
self.class.endpoints.each do |endpoint|
endpoint.mount_in(@route_set)
end
@route_set.freeze
end
def call(env)
status, headers, body = @route_set.call(env)
headers.delete('X-Cascade') unless cascade?
[status, headers, body]
end
# Some requests may return a HTTP 404 error if grape cannot find a matching
# route. In this case, Rack::Mount adds a X-Cascade header to the response
# and sets it to 'pass', indicating to grape's parents they should keep
# looking for a matching route on other resources.
#
# In some applications (e.g. mounting grape on rails), one might need to trap
# errors from reaching upstream. This is effectivelly done by unsetting
# X-Cascade. Default :cascade is true.
def cascade?
return !!self.class.namespace_inheritable(:cascade) if self.class.inheritable_setting.namespace_inheritable.keys.include?(:cascade)
return !!self.class.namespace_inheritable(:version_options)[:cascade] if self.class.namespace_inheritable(:version_options) && self.class.namespace_inheritable(:version_options).key?(:cascade)
true
end
reset!
private
# For every resource add a 'OPTIONS' route that returns an HTTP 204 response
# with a list of HTTP methods that can be called. Also add a route that
# will return an HTTP 405 response for any HTTP method that the resource
# cannot handle.
def add_head_not_allowed_methods_and_options_methods
methods_per_path = {}
self.class.endpoints.each do |endpoint|
routes = endpoint.routes
routes.each do |route|
methods_per_path[route.route_path] ||= []
methods_per_path[route.route_path] << route.route_method
end
end
# The paths we collected are prepared (cf. Path#prepare), so they
# contain already versioning information when using path versioning.
# Disable versioning so adding a route won't prepend versioning
# informations again.
without_versioning do
methods_per_path.each do |path, methods|
allowed_methods = methods.dup
unless self.class.namespace_inheritable(:do_not_route_head)
allowed_methods |= ['HEAD'] if allowed_methods.include?('GET')
end
allow_header = (['OPTIONS'] | allowed_methods).join(', ')
unless self.class.namespace_inheritable(:do_not_route_options)
unless allowed_methods.include?('OPTIONS')
self.class.options(path, {}) do
header 'Allow', allow_header
status 204
''
end
end
end
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - allowed_methods
not_allowed_methods << 'OPTIONS' if self.class.namespace_inheritable(:do_not_route_options)
self.class.route(not_allowed_methods, path) do
header 'Allow', allow_header
status 405
''
end
end
end
end
def without_versioning(&block)
old_version = self.class.namespace_inheritable(:version)
old_version_options = self.class.namespace_inheritable(:version_options)
self.class.namespace_inheritable_to_nil(:version)
self.class.namespace_inheritable_to_nil(:version_options)
yield
self.class.namespace_inheritable(:version, old_version)
self.class.namespace_inheritable(:version_options, old_version_options)
end
end
end