@@ -46,10 +46,7 @@ def run_before_each(endpoint)
4646 # @note This happens at the time of API definition, so in this context the
4747 # endpoint does not know if it will be mounted under a different endpoint.
4848 # @yield a block defining what your API should do when this endpoint is hit
49- def initialize ( new_settings , options = { } , &block )
50- require_option ( options , :path )
51- require_option ( options , :method )
52-
49+ def initialize ( new_settings , **options , &block )
5350 self . inheritable_setting = new_settings . point_in_time_copy
5451
5552 # now +namespace_stackable(:declared_params)+ contains all params defined for
@@ -67,7 +64,6 @@ def initialize(new_settings, options = {}, &block)
6764 @options [ :path ] << '/' if options [ :path ] . empty?
6865
6966 @options [ :method ] = Array ( options [ :method ] )
70- @options [ :route_options ] ||= { }
7167
7268 @lazy_initialize_lock = Mutex . new
7369 @lazy_initialized = nil
@@ -88,10 +84,6 @@ def inherit_settings(namespace_stackable)
8884 endpoints &.each { |e | e . inherit_settings ( namespace_stackable ) }
8985 end
9086
91- def require_option ( options , key )
92- raise Grape ::Exceptions ::MissingOption . new ( key ) unless options . key? ( key )
93- end
94-
9587 def routes
9688 @routes ||= endpoints &.collect ( &:routes ) &.flatten || to_routes
9789 end
@@ -117,53 +109,6 @@ def mount_in(router)
117109 end
118110 end
119111
120- def to_routes
121- default_route_options = prepare_default_route_attributes
122-
123- map_routes do |method , raw_path |
124- prepared_path = Path . new ( raw_path , namespace , prepare_default_path_settings )
125- params = options [ :route_options ] . present? ? options [ :route_options ] . merge ( default_route_options ) : default_route_options
126- route = Grape ::Router ::Route . new ( method , prepared_path . origin , prepared_path . suffix , params )
127- route . apply ( self )
128- end . flatten
129- end
130-
131- def prepare_routes_requirements
132- { } . merge! ( *inheritable_setting . namespace_stackable [ :namespace ] . map ( &:requirements ) ) . tap do |requirements |
133- endpoint_requirements = options . dig ( :route_options , :requirements )
134- requirements . merge! ( endpoint_requirements ) if endpoint_requirements
135- end
136- end
137-
138- def prepare_default_route_attributes
139- {
140- namespace : namespace ,
141- version : prepare_version ,
142- requirements : prepare_routes_requirements ,
143- prefix : inheritable_setting . namespace_inheritable [ :root_prefix ] ,
144- anchor : options [ :route_options ] . fetch ( :anchor , true ) ,
145- settings : inheritable_setting . route . except ( :declared_params , :saved_validations ) ,
146- forward_match : options [ :forward_match ]
147- }
148- end
149-
150- def prepare_version
151- version = inheritable_setting . namespace_inheritable [ :version ]
152- return if version . blank?
153-
154- version . length == 1 ? version . first : version
155- end
156-
157- def map_routes
158- options [ :method ] . map { |method | options [ :path ] . map { |path | yield method , path } }
159- end
160-
161- def prepare_default_path_settings
162- namespace_stackable_hash = inheritable_setting . namespace_stackable . to_hash
163- namespace_inheritable_hash = inheritable_setting . namespace_inheritable . to_hash
164- namespace_stackable_hash . merge! ( namespace_inheritable_hash )
165- end
166-
167112 def namespace
168113 @namespace ||= Namespace . joined_space_path ( inheritable_setting . namespace_stackable [ :namespace ] )
169114 end
@@ -311,6 +256,59 @@ def options?
311256
312257 private
313258
259+ def to_routes
260+ route_options = options [ :route_options ]
261+ default_route_options = prepare_default_route_attributes ( route_options )
262+ complete_route_options = route_options . merge ( default_route_options )
263+ path_settings = prepare_default_path_settings
264+
265+ options [ :method ] . flat_map do |method |
266+ options [ :path ] . map do |path |
267+ prepared_path = Path . new ( path , default_route_options [ :namespace ] , path_settings )
268+ pattern = Grape ::Router ::Pattern . new (
269+ origin : prepared_path . origin ,
270+ suffix : prepared_path . suffix ,
271+ anchor : default_route_options [ :anchor ] ,
272+ params : route_options [ :params ] ,
273+ format : options [ :format ] ,
274+ version : default_route_options [ :version ] ,
275+ requirements : default_route_options [ :requirements ]
276+ )
277+ Grape ::Router ::Route . new ( self , method , pattern , complete_route_options )
278+ end
279+ end
280+ end
281+
282+ def prepare_default_route_attributes ( route_options )
283+ {
284+ namespace : namespace ,
285+ version : prepare_version ( inheritable_setting . namespace_inheritable [ :version ] ) ,
286+ requirements : prepare_routes_requirements ( route_options [ :requirements ] ) ,
287+ prefix : inheritable_setting . namespace_inheritable [ :root_prefix ] ,
288+ anchor : route_options . fetch ( :anchor , true ) ,
289+ settings : inheritable_setting . route . except ( :declared_params , :saved_validations ) ,
290+ forward_match : options [ :forward_match ]
291+ }
292+ end
293+
294+ def prepare_default_path_settings
295+ namespace_stackable_hash = inheritable_setting . namespace_stackable . to_hash
296+ namespace_inheritable_hash = inheritable_setting . namespace_inheritable . to_hash
297+ namespace_stackable_hash . merge! ( namespace_inheritable_hash )
298+ end
299+
300+ def prepare_routes_requirements ( route_options_requirements )
301+ namespace_requirements = inheritable_setting . namespace_stackable [ :namespace ] . filter_map ( &:requirements )
302+ namespace_requirements << route_options_requirements if route_options_requirements . present?
303+ namespace_requirements . reduce ( { } , :merge )
304+ end
305+
306+ def prepare_version ( namespace_inheritable_version )
307+ return if namespace_inheritable_version . blank?
308+
309+ namespace_inheritable_version . length == 1 ? namespace_inheritable_version . first : namespace_inheritable_version
310+ end
311+
314312 def build_stack
315313 stack = Grape ::Middleware ::Stack . new
316314
0 commit comments