Skip to content

Commit 58a9835

Browse files
authored
Merge pull request #2778 from ruby-grape/endpoint_api_kwarg
Rename Grape::Endpoint's for: keyword to api: and expose #api
2 parents ab97320 + e26108b commit 58a9835

9 files changed

Lines changed: 31 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#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).
88
* [#2776](https://github.com/ruby-grape/grape/pull/2776): Pass `path` to `Grape::Endpoint` as a keyword instead of via options - [@ericproulx](https://github.com/ericproulx).
99
* [#2777](https://github.com/ruby-grape/grape/pull/2777): Add `Grape::Endpoint#mounted_app` reader and `app:` keyword - [@ericproulx](https://github.com/ericproulx).
10+
* [#2778](https://github.com/ruby-grape/grape/pull/2778): Rename `Grape::Endpoint`'s `for:` keyword to `api:` and expose `#api` - [@ericproulx](https://github.com/ericproulx).
1011
* Your contribution here.
1112

1213
#### Fixes

UPGRADING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ Grape::Endpoint.new(settings, http_methods: :get, path: '/foo', for: self)
2323

2424
Relatedly, an endpoint's public `options` Hash no longer carries `:method` or `:path`. Nothing in Grape read `:method`, and the only reader of `:path` was an internal test; both values are available from the route instead — `route.request_method` for the verb and `route.path` for the (compiled) path, which is what grape-swagger and other introspection already use. The raw definition-time path array is no longer exposed on the endpoint.
2525

26+
#### `Grape::Endpoint.new` takes `api:` instead of `for:`
27+
28+
The keyword identifying the API an endpoint belongs to has been renamed from `for:` — a reserved word whose value can't be referenced as a local — to `api:`:
29+
30+
```ruby
31+
# before
32+
Grape::Endpoint.new(settings, http_methods: :get, path: '/foo', for: my_api)
33+
34+
# after
35+
Grape::Endpoint.new(settings, http_methods: :get, path: '/foo', api: my_api)
36+
```
37+
38+
The owning API is no longer carried on the endpoint's public `options` Hash — `endpoint.options[:for]` is gone. Use the new `endpoint.api` reader instead.
39+
2640
### Upgrading to >= 3.3
2741

2842
#### Minimum required Ruby is now 3.3

lib/grape/dsl/inside_route.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def version
1515
end
1616

1717
def configuration
18-
config.for.configuration.evaluate
18+
config.api.configuration.evaluate
1919
end
2020

2121
# End the request and display an error to the

lib/grape/dsl/routing.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def mount(mounts, *opts)
160160
path:,
161161
app:,
162162
route_options: { anchor: false },
163-
for: self
163+
api: self
164164
)
165165
end
166166
end
@@ -189,7 +189,7 @@ def route(methods, paths = ['/'], route_options = {}, &)
189189
inheritable_setting,
190190
http_methods:,
191191
path: paths,
192-
for: self,
192+
api: self,
193193
route_options: all_route_options,
194194
&
195195
)

lib/grape/endpoint.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ class Endpoint
1717
def_delegators :request, :params, :headers, :cookies
1818
def_delegator :cookies, :response_cookies
1919

20+
# The API (a +Grape::API+ instance) this endpoint belongs to.
21+
def_delegator :@config, :api
22+
2023
# The logger configured on the API this endpoint belongs to. Available
2124
# inside route handlers, +before+/+after+/+after_validation+/+finally+
2225
# filters, and +rescue_from+ blocks.
23-
def logger
24-
config.for.logger
25-
end
26+
def_delegator :api, :logger
2627

2728
# The Rack app or Grape API mounted at this endpoint, or +nil+ for a plain
2829
# block endpoint. Prefer this over +options[:app]+, which is retained only
@@ -49,6 +50,8 @@ def block_to_unbound_method(block)
4950
# reach this endpoint.
5051
# @param path [String or Array] the path to this endpoint, within the
5152
# current scope.
53+
# @param api [Grape::API] the API this endpoint belongs to. Exposed as
54+
# {#api}.
5255
# @param app [#call, nil] the Rack app or Grape API mounted at this
5356
# endpoint; +nil+ for a plain block endpoint. Exposed as {#mounted_app}.
5457
# @param options [Hash] attributes of this endpoint, normalized into a
@@ -57,7 +60,7 @@ def block_to_unbound_method(block)
5760
# @note This happens at the time of API definition, so in this context the
5861
# endpoint does not know if it will be mounted under a different endpoint.
5962
# @yield a block defining what your API should do when this endpoint is hit
60-
def initialize(new_settings, http_methods:, path:, app: nil, **options, &block)
63+
def initialize(new_settings, http_methods:, path:, api:, app: nil, **options, &block)
6164
self.inheritable_setting = new_settings.point_in_time_copy
6265

6366
# now +namespace_stackable(:declared_params)+ contains all params defined for
@@ -70,7 +73,7 @@ def initialize(new_settings, http_methods:, path:, app: nil, **options, &block)
7073
inheritable_setting.namespace_inheritable[:default_error_status] ||= 500
7174

7275
@options = options
73-
@config = Options.new(http_methods:, path:, app:, **options)
76+
@config = Options.new(http_methods:, path:, api:, app:, **options)
7477
# +:app+ is still surfaced on the public options Hash for backwards
7578
# compatibility (e.g. grape-swagger); prefer the +mounted_app+ reader.
7679
@options[:app] = app if app

lib/grape/endpoint/options.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ 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-
Options = Data.define(:path, :http_methods, :for, :route_options, :app, :format) do
10-
def initialize(path:, http_methods:, route_options: {}, app: nil, format: nil, **rest)
9+
Options = Data.define(:path, :http_methods, :api, :route_options, :app, :format) do
10+
def initialize(path:, http_methods:, api:, route_options: {}, app: nil, format: nil)
1111
path = Array(path)
1212
path << '/' if path.empty?
1313
http_methods = Array(http_methods)

spec/grape/dsl/routing_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class << self
157157
expect(Grape::Endpoint).to receive(:new) do |_inheritable_setting, endpoint_options|
158158
expect(endpoint_options[:http_methods]).to eq :get
159159
expect(endpoint_options[:path]).to eq '/foo'
160-
expect(endpoint_options[:for]).to eq subject
160+
expect(endpoint_options[:api]).to eq subject
161161
expect(endpoint_options[:route_options]).to eq(foo: 'bar', fiz: 'baz', params: { nuz: 'naz' })
162162
end.and_yield
163163

spec/grape/endpoint_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ def memoized
10341034
path: '/path',
10351035
app: {},
10361036
route_options: { anchor: false },
1037-
for: Class.new
1037+
api: Class.new
10381038
}
10391039
end
10401040
let(:settings) { Grape::Util::InheritableSetting.new }

spec/grape/named_api_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
describe Grape::API do
4-
subject(:api_name) { NamedAPI.endpoints.last.options[:for].to_s }
4+
subject(:api_name) { NamedAPI.endpoints.last.api.to_s }
55

66
let(:api) do
77
Class.new(Grape::API) do

0 commit comments

Comments
 (0)