Skip to content

Commit ced6190

Browse files
ericproulxclaude
andcommitted
Move HEAD route creation into Grape::Router::Route#to_head
`Endpoint#mount_in` built the HEAD variant of a GET route inline: dup the route, flip its request method, then apply and append it. Duplicating a route and turning it into a HEAD request is the route's own concern, not the endpoint's. Add `Route#to_head`, which returns a fresh HEAD copy of the route, and demote `convert_to_head_request!` to `protected` now that `to_head` is its only caller. `mount_in` keeps the decision of whether to add a HEAD route (the `do_not_route_head` setting and the GET check) and just calls `route.to_head.apply(self)`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 99f42ae commit ced6190

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

lib/grape/endpoint.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ def mount_in(router)
120120
router.append(route.apply(self))
121121
next if inheritable_setting.namespace_inheritable[:do_not_route_head] || route.request_method != Rack::GET
122122

123-
route.dup.then do |head_route|
124-
head_route.convert_to_head_request!
125-
router.append(head_route.apply(self))
126-
end
123+
router.append(route.to_head.apply(self))
127124
end
128125
end
129126

lib/grape/router/route.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ def initialize(endpoint, method, pattern, options, forward_match:, **route_attri
1919
@match_function = forward_match ? FORWARD_MATCH_METHOD : NON_FORWARD_MATCH_METHOD
2020
end
2121

22-
def convert_to_head_request!
23-
@request_method = Rack::HEAD
22+
def to_head
23+
head = dup
24+
head.convert_to_head_request!
25+
head
2426
end
2527

2628
def apply(app)
@@ -43,6 +45,12 @@ def params(input = nil)
4345
parsed.compact.symbolize_keys
4446
end
4547

48+
protected
49+
50+
def convert_to_head_request!
51+
@request_method = Rack::HEAD
52+
end
53+
4654
private
4755

4856
def params_without_input

0 commit comments

Comments
 (0)