Refactor Router Architecture: Improve Encapsulation and API Design #2629
+149
−161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor Router Architecture: Improve Encapsulation and API Design
Overview
This PR refactors the router internals to improve code quality, maintainability, and API design. The changes focus on better encapsulation, explicit interfaces, and cleaner delegation patterns throughout the routing system.
Motivation
The previous implementation had several design issues:
route.options[:endpoint].call(env))delegate_missing_tovs explicit delegation)These issues made the code harder to understand, test, and modify.
Key Changes
1. Router Classes Refactoring
BaseRoutepatternas first-class parameter instead of nested in optionsForwardabledelegation for common methods (path,origin,description, etc.)Before:
After:
GreedyRouteendpoint:,allow_header:)callmethodparamsmethod to returnnilinstead of accessing nested optionsBefore:
After:
Routeendpointandpatternas explicit parametersBefore:
After:
Patternextract_capturemethod to use direct hash constructionBefore:
After:
2. Endpoint Refactoring
**options)require_optionmethod (now handled by Ruby's keyword argument validation)prepare_version,prepare_routes_requirements) to accept explicit parameters3. Router Improvements
associate_routesto accept aGreedyRouteinstance directlyroute.options[:endpoint].call(env)toroute.call(env)route.options[:allow_header]toroute.allow_headermake_routing_argsto avoid merging whenroute_paramsis nil4. Code Quality Improvements
map(&:routes).flattenwithflat_map(&:routes)for better performance*extract_input_and_method(env)→extract_input_and_method(env))Grape::Util::ApiDescription::DSL_METHODS)5. API Instance Updates
collect_route_config_per_patternto createGreedyRouteinstances explicitlyBreaking Changes
None. These are internal refactorings that don't change the public API.
Testing
GreedyRouteto reflect new constructor signatureEndpointto use keyword argumentsroute_paramDSL methodBenefits
flat_mapand removing unnecessary hash lookupsFiles Changed
lib/grape/api/instance.rblib/grape/dsl/routing.rblib/grape/endpoint.rblib/grape/router.rblib/grape/router/base_route.rblib/grape/router/greedy_route.rblib/grape/router/pattern.rblib/grape/router/route.rblib/grape/util/api_description.rb