class Grape::API::Instance

def collect_route_config_per_pattern(all_routes)

def collect_route_config_per_pattern(all_routes)
  routes_by_regexp = all_routes.group_by(&:pattern_regexp)
  # Build the configuration based on the first endpoint and the collection of methods supported.
  routes_by_regexp.each_value do |routes|
    last_route = routes.last # Most of the configuration is taken from the last endpoint
    next if routes.any? { |route| route.request_method == '*' }
    allowed_methods = routes.map(&:request_method)
    allowed_methods |= [Rack::HEAD] if !self.class.namespace_inheritable(:do_not_route_head) && allowed_methods.include?(Rack::GET)
    allow_header = self.class.namespace_inheritable(:do_not_route_options) ? allowed_methods : [Rack::OPTIONS] | allowed_methods
    last_route.app.options[:options_route_enabled] = true unless self.class.namespace_inheritable(:do_not_route_options) || allowed_methods.include?(Rack::OPTIONS)
    @router.associate_routes(last_route.pattern, {
                               endpoint: last_route.app,
                               allow_header: allow_header
                             })
  end
end