@@ -51,6 +51,11 @@ def settings_stack
51
51
def set ( key , value )
52
52
@settings . last [ key . to_sym ] = value
53
53
end
54
+
55
+ # Merge multiple setting in at once. Mostly for internal use.
56
+ def merge_settings ( settings )
57
+ settings_stack . last . merge! ( settings )
58
+ end
54
59
55
60
# Define a root URL prefix for your entire
56
61
# API.
@@ -180,6 +185,15 @@ def http_digest(options = {}, &block)
180
185
auth :http_digest , options , &block
181
186
end
182
187
188
+ def mount ( mounts )
189
+ mounts . each_pair do |app , path |
190
+ next unless app . respond_to? ( :call )
191
+ route_set . add_route ( app ,
192
+ path_info : compile_path ( path , false )
193
+ )
194
+ end
195
+ end
196
+
183
197
# Defines a route that will be recognized
184
198
# by the Grape API.
185
199
#
@@ -200,16 +214,12 @@ def route(methods, paths = ['/'], route_options = {}, &block)
200
214
201
215
endpoint = build_endpoint ( &block )
202
216
203
- endpoint_options = { }
204
- endpoint_options [ :version ] = /#{ version . join ( '|' ) } / if version
205
-
206
217
route_options ||= { }
207
218
208
219
methods . each do |method |
209
220
paths . each do |path |
210
-
211
- compiled_path = compile_path ( path )
212
- path = Rack ::Mount ::Strexp . compile ( compiled_path , endpoint_options , %w( / . ? ) , true )
221
+ prepared_path = prepare_path ( path )
222
+ path = compile_path ( path )
213
223
regex = Rack ::Mount ::RegexpWithNamedGroups . new ( path )
214
224
path_params = regex . named_captures . map { |nc | nc [ 0 ] } - [ 'version' , 'format' ]
215
225
path_params |= ( route_options [ :params ] || [ ] )
@@ -220,7 +230,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
220
230
:version => version ? version . join ( '|' ) : nil ,
221
231
:namespace => namespace ,
222
232
:method => request_method ,
223
- :path => compiled_path ,
233
+ :path => prepared_path ,
224
234
:params => path_params } ) )
225
235
226
236
route_set . add_route ( endpoint ,
@@ -352,7 +362,7 @@ def route_set
352
362
@route_set ||= Rack ::Mount ::RouteSet . new
353
363
end
354
364
355
- def compile_path ( path )
365
+ def prepare_path ( path )
356
366
parts = [ ]
357
367
parts << prefix if prefix
358
368
parts << ':version' if version
@@ -361,6 +371,13 @@ def compile_path(path)
361
371
parts . last << '(.:format)'
362
372
Rack ::Mount ::Utils . normalize_path ( parts . join ( '/' ) )
363
373
end
374
+
375
+ def compile_path ( path , anchor = true )
376
+ endpoint_options = { }
377
+ endpoint_options [ :version ] = /#{ version . join ( '|' ) } / if version
378
+
379
+ Rack ::Mount ::Strexp . compile ( prepare_path ( path ) , endpoint_options , %w( / . ? ) , anchor )
380
+ end
364
381
end
365
382
366
383
reset!
0 commit comments