@@ -145,17 +145,16 @@ def reset_routes!
145145 end
146146
147147 def mount_in ( router )
148- if endpoints
149- endpoints . each { |e | e . mount_in ( router ) }
150- else
151- reset_routes!
152- routes . each do |route |
153- methods = [ route . request_method ]
154- methods << Rack ::HEAD if !namespace_inheritable ( :do_not_route_head ) && route . request_method == Rack ::GET
155- methods . each do |method |
156- route = Grape ::Router ::Route . new ( method , route . origin , **route . attributes . to_h ) unless route . request_method == method
157- router . append ( route . apply ( self ) )
158- end
148+ return endpoints . each { |e | e . mount_in ( router ) } if endpoints
149+
150+ reset_routes!
151+ routes . each do |route |
152+ router . append ( route . apply ( self ) )
153+ next unless !namespace_inheritable ( :do_not_route_head ) && route . request_method == Rack ::GET
154+
155+ route . dup . then do |head_route |
156+ head_route . convert_to_head_request!
157+ router . append ( head_route . apply ( self ) )
159158 end
160159 end
161160 end
@@ -164,8 +163,9 @@ def to_routes
164163 route_options = prepare_default_route_attributes
165164 map_routes do |method , path |
166165 path = prepare_path ( path )
167- params = merge_route_options ( **route_options . merge ( suffix : path . suffix ) )
168- route = Router ::Route . new ( method , path . path , **params )
166+ route_options [ :suffix ] = path . suffix
167+ params = options [ :route_options ] . merge ( route_options )
168+ route = Grape ::Router ::Route . new ( method , path . path , params )
169169 route . apply ( self )
170170 end . flatten
171171 end
@@ -196,10 +196,6 @@ def prepare_version
196196 version . length == 1 ? version . first : version
197197 end
198198
199- def merge_route_options ( **default )
200- options [ :route_options ] . clone . merge! ( **default )
201- end
202-
203199 def map_routes
204200 options [ :method ] . map { |method | options [ :path ] . map { |path | yield method , path } }
205201 end
@@ -259,9 +255,10 @@ def run
259255 run_filters befores , :before
260256
261257 if ( allowed_methods = env [ Grape ::Env ::GRAPE_ALLOWED_METHODS ] )
262- raise Grape ::Exceptions ::MethodNotAllowed . new ( header . merge ( 'Allow' => allowed_methods ) ) unless options?
258+ allow_header_value = allowed_methods . join ( ', ' )
259+ raise Grape ::Exceptions ::MethodNotAllowed . new ( header . merge ( 'Allow' => allow_header_value ) ) unless options?
263260
264- header Grape ::Http ::Headers ::ALLOW , allowed_methods
261+ header Grape ::Http ::Headers ::ALLOW , allow_header_value
265262 response_object = ''
266263 status 204
267264 else
@@ -287,59 +284,6 @@ def run
287284 end
288285 end
289286
290- def build_stack ( helpers )
291- stack = Grape ::Middleware ::Stack . new
292-
293- content_types = namespace_stackable_with_hash ( :content_types )
294- format = namespace_inheritable ( :format )
295-
296- stack . use Rack ::Head
297- stack . use Class . new ( Grape ::Middleware ::Error ) ,
298- helpers : helpers ,
299- format : format ,
300- content_types : content_types ,
301- default_status : namespace_inheritable ( :default_error_status ) ,
302- rescue_all : namespace_inheritable ( :rescue_all ) ,
303- rescue_grape_exceptions : namespace_inheritable ( :rescue_grape_exceptions ) ,
304- default_error_formatter : namespace_inheritable ( :default_error_formatter ) ,
305- error_formatters : namespace_stackable_with_hash ( :error_formatters ) ,
306- rescue_options : namespace_stackable_with_hash ( :rescue_options ) ,
307- rescue_handlers : namespace_reverse_stackable_with_hash ( :rescue_handlers ) ,
308- base_only_rescue_handlers : namespace_stackable_with_hash ( :base_only_rescue_handlers ) ,
309- all_rescue_handler : namespace_inheritable ( :all_rescue_handler ) ,
310- grape_exceptions_rescue_handler : namespace_inheritable ( :grape_exceptions_rescue_handler )
311-
312- stack . concat namespace_stackable ( :middleware )
313-
314- if namespace_inheritable ( :version ) . present?
315- stack . use Grape ::Middleware ::Versioner . using ( namespace_inheritable ( :version_options ) [ :using ] ) ,
316- versions : namespace_inheritable ( :version ) . flatten ,
317- version_options : namespace_inheritable ( :version_options ) ,
318- prefix : namespace_inheritable ( :root_prefix ) ,
319- mount_path : namespace_stackable ( :mount_path ) . first
320- end
321-
322- stack . use Grape ::Middleware ::Formatter ,
323- format : format ,
324- default_format : namespace_inheritable ( :default_format ) || :txt ,
325- content_types : content_types ,
326- formatters : namespace_stackable_with_hash ( :formatters ) ,
327- parsers : namespace_stackable_with_hash ( :parsers )
328-
329- builder = stack . build
330- builder . run -> ( env ) { env [ Grape ::Env ::API_ENDPOINT ] . run }
331- builder . to_app
332- end
333-
334- def build_helpers
335- helpers = namespace_stackable ( :helpers )
336- return if helpers . empty?
337-
338- Module . new { helpers . each { |mod_to_include | include mod_to_include } }
339- end
340-
341- private :build_stack , :build_helpers
342-
343287 def execute
344288 @block &.call ( self )
345289 end
@@ -411,13 +355,66 @@ def validations
411355 return enum_for ( :validations ) unless block_given?
412356
413357 route_setting ( :saved_validations ) &.each do |saved_validation |
414- yield Grape ::Validations ::ValidatorFactory . create_validator ( ** saved_validation )
358+ yield Grape ::Validations ::ValidatorFactory . create_validator ( saved_validation )
415359 end
416360 end
417361
418362 def options?
419363 options [ :options_route_enabled ] &&
420364 env [ Rack ::REQUEST_METHOD ] == Rack ::OPTIONS
421365 end
366+
367+ private
368+
369+ def build_stack ( helpers )
370+ stack = Grape ::Middleware ::Stack . new
371+
372+ content_types = namespace_stackable_with_hash ( :content_types )
373+ format = namespace_inheritable ( :format )
374+
375+ stack . use Rack ::Head
376+ stack . use Class . new ( Grape ::Middleware ::Error ) ,
377+ helpers : helpers ,
378+ format : format ,
379+ content_types : content_types ,
380+ default_status : namespace_inheritable ( :default_error_status ) ,
381+ rescue_all : namespace_inheritable ( :rescue_all ) ,
382+ rescue_grape_exceptions : namespace_inheritable ( :rescue_grape_exceptions ) ,
383+ default_error_formatter : namespace_inheritable ( :default_error_formatter ) ,
384+ error_formatters : namespace_stackable_with_hash ( :error_formatters ) ,
385+ rescue_options : namespace_stackable_with_hash ( :rescue_options ) ,
386+ rescue_handlers : namespace_reverse_stackable_with_hash ( :rescue_handlers ) ,
387+ base_only_rescue_handlers : namespace_stackable_with_hash ( :base_only_rescue_handlers ) ,
388+ all_rescue_handler : namespace_inheritable ( :all_rescue_handler ) ,
389+ grape_exceptions_rescue_handler : namespace_inheritable ( :grape_exceptions_rescue_handler )
390+
391+ stack . concat namespace_stackable ( :middleware )
392+
393+ if namespace_inheritable ( :version ) . present?
394+ stack . use Grape ::Middleware ::Versioner . using ( namespace_inheritable ( :version_options ) [ :using ] ) ,
395+ versions : namespace_inheritable ( :version ) . flatten ,
396+ version_options : namespace_inheritable ( :version_options ) ,
397+ prefix : namespace_inheritable ( :root_prefix ) ,
398+ mount_path : namespace_stackable ( :mount_path ) . first
399+ end
400+
401+ stack . use Grape ::Middleware ::Formatter ,
402+ format : format ,
403+ default_format : namespace_inheritable ( :default_format ) || :txt ,
404+ content_types : content_types ,
405+ formatters : namespace_stackable_with_hash ( :formatters ) ,
406+ parsers : namespace_stackable_with_hash ( :parsers )
407+
408+ builder = stack . build
409+ builder . run -> ( env ) { env [ Grape ::Env ::API_ENDPOINT ] . run }
410+ builder . to_app
411+ end
412+
413+ def build_helpers
414+ helpers = namespace_stackable ( :helpers )
415+ return if helpers . empty?
416+
417+ Module . new { helpers . each { |mod_to_include | include mod_to_include } }
418+ end
422419 end
423420end
0 commit comments