Skip to content

Commit 6458f1b

Browse files
authored
Merge pull request #2775 from ruby-grape/endpoint_http_methods_kwarg
Pass http_methods to Grape::Endpoint as a keyword instead of via options
2 parents 4b08638 + 2e7890c commit 6458f1b

7 files changed

Lines changed: 27 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#2768](https://github.com/ruby-grape/grape/pull/2768): Remove Guard - [@ericproulx](https://github.com/ericproulx).
66
* [#2774](https://github.com/ruby-grape/grape/pull/2774): Make `forward_match` an internal kwarg instead of a route option - [@ericproulx](https://github.com/ericproulx).
7+
* [#2775](https://github.com/ruby-grape/grape/pull/2775): Pass `http_methods` to `Grape::Endpoint` as a keyword instead of via options - [@ericproulx](https://github.com/ericproulx).
78
* Your contribution here.
89

910
#### Fixes

UPGRADING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ Upgrading Grape
99

1010
As a result it is no longer readable through `route.options[:forward_match]` or the `route.forward_match` reader — both previously returned the flag. Nothing in Grape consumed either, so this only affects code that introspected routes directly; there is no replacement, as the value is now purely internal.
1111

12+
#### `Grape::Endpoint.new` takes `http_methods:` instead of `method:`
13+
14+
`Grape::Endpoint.new` now receives the HTTP verb(s) under the `http_methods:` keyword instead of `method:`, matching the name used everywhere else. If you build endpoints directly (uncommon — this is an internal API normally driven by the routing DSL), rename the keyword:
15+
16+
```ruby
17+
# before
18+
Grape::Endpoint.new(settings, method: :get, path: '/foo', for: self)
19+
20+
# after
21+
Grape::Endpoint.new(settings, http_methods: :get, path: '/foo', for: self)
22+
```
23+
24+
Relatedly, an endpoint's public `options` Hash no longer carries `:method` (`endpoint.options[:method]`). Nothing in Grape read it, and downstream gems such as grape-swagger already read the verb from `route.request_method`, which is unchanged.
25+
1226
### Upgrading to >= 3.3
1327

1428
#### Minimum required Ruby is now 3.3

lib/grape/dsl/routing.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def mount(mounts, *opts)
156156

157157
endpoints << Grape::Endpoint.new(
158158
in_setting,
159-
method: :any,
159+
http_methods: :any,
160160
path:,
161161
app:,
162162
route_options: { anchor: false },
@@ -178,7 +178,7 @@ def mount(mounts, *opts)
178178
# end
179179
# end
180180
def route(methods, paths = ['/'], route_options = {}, &)
181-
method = methods == :any ? '*' : methods
181+
http_methods = methods == :any ? '*' : methods
182182
endpoint_params = inheritable_setting.namespace_stackable_with_hash(:params) || {}
183183
endpoint_description = inheritable_setting.route[:description]
184184
all_route_options = { params: endpoint_params }
@@ -187,7 +187,7 @@ def route(methods, paths = ['/'], route_options = {}, &)
187187

188188
new_endpoint = Grape::Endpoint.new(
189189
inheritable_setting,
190-
method:,
190+
http_methods:,
191191
path: paths,
192192
for: self,
193193
route_options: all_route_options,

lib/grape/endpoint.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ def block_to_unbound_method(block)
3838
# Create a new endpoint.
3939
# @param new_settings [InheritableSetting] settings to determine the params,
4040
# validations, and other properties from.
41+
# @param http_methods [String or Array] which HTTP method(s) can be used to
42+
# reach this endpoint.
4143
# @param options [Hash] attributes of this endpoint, normalized into a
4244
# +Grape::Endpoint::Options+ value object.
4345
# @option options path [String or Array] the path to this endpoint, within
4446
# the current scope.
45-
# @option options method [String or Array] which HTTP method(s) can be used
46-
# to reach this endpoint.
4747
# @option options route_options [Hash]
4848
# @note This happens at the time of API definition, so in this context the
4949
# endpoint does not know if it will be mounted under a different endpoint.
5050
# @yield a block defining what your API should do when this endpoint is hit
51-
def initialize(new_settings, **options, &block)
51+
def initialize(new_settings, http_methods:, **options, &block)
5252
self.inheritable_setting = new_settings.point_in_time_copy
5353

5454
# now +namespace_stackable(:declared_params)+ contains all params defined for
@@ -63,8 +63,7 @@ def initialize(new_settings, **options, &block)
6363
@options = options
6464
@options[:path] = Array(@options[:path])
6565
@options[:path] << '/' if @options[:path].empty?
66-
@options[:method] = Array(@options[:method])
67-
@config = Options.new(**options)
66+
@config = Options.new(http_methods:, **options)
6867

6968
@status = nil
7069
@stream = nil

lib/grape/endpoint/options.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ class Endpoint
66
# +Grape::Endpoint.new+. Internal to {Grape::Endpoint}, which builds it
77
# from the +**options+ Hash in #initialize so the public +options+ reader
88
# stays a plain Hash for downstream gems (e.g. grape-swagger).
9-
# +:method+ is renamed to +:http_methods+ on the value object to avoid
10-
# shadowing +Object#method+ via the generated Data accessor.
119
Options = Data.define(:path, :http_methods, :for, :route_options, :app, :format) do
12-
def initialize(path:, method:, route_options: {}, app: nil, format: nil, **rest)
10+
def initialize(path:, http_methods:, route_options: {}, app: nil, format: nil, **rest)
1311
path = Array(path)
1412
path << '/' if path.empty?
15-
super(path:, http_methods: Array(method), route_options:, app:, format:, **rest)
13+
http_methods = Array(http_methods)
14+
super
1615
end
1716
end
1817
end

spec/grape/dsl/routing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class << self
155155
subject.inheritable_setting.namespace_stackable[:params] = { nuz: 'naz' }
156156

157157
expect(Grape::Endpoint).to receive(:new) do |_inheritable_setting, endpoint_options|
158-
expect(endpoint_options[:method]).to eq :get
158+
expect(endpoint_options[:http_methods]).to eq :get
159159
expect(endpoint_options[:path]).to eq '/foo'
160160
expect(endpoint_options[:for]).to eq subject
161161
expect(endpoint_options[:route_options]).to eq(foo: 'bar', fiz: 'baz', params: { nuz: 'naz' })

spec/grape/endpoint_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ def memoized
10301030

10311031
let(:options) do
10321032
{
1033-
method: :path,
1033+
http_methods: :path,
10341034
path: '/path',
10351035
app: {},
10361036
route_options: { anchor: false },

0 commit comments

Comments
 (0)