Skip to content

Commit f1908f4

Browse files
myxohdblock
authored andcommitted
Exposes the name of the API via to_s (Addresses #1825) (#1826)
1 parent de1f3e9 commit f1908f4

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#1825](https://github.com/ruby-grape/grape/pull/1825): `to_s` on a mounted class now responses with the API name - [@myxoh](https://github.com/myxoh).
1011

11-
### 1.2.0 (29/11/2018)
12+
### 1.2.0 (11/29/2018)
1213

1314
#### Features
1415

UPGRADING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
Upgrading Grape
22
===============
33

4+
### Upgrading to >= 1.2.1
5+
6+
#### Obtaining the name of a mounted class
7+
8+
In order to make obtaining the name of a mounted class simpler, we've delegated `.to_s` to `base.name`
9+
10+
**Deprecated in 1.2.0**
11+
```ruby
12+
payload[:endpoint].options[:for].name
13+
```
14+
**New**
15+
```ruby
16+
payload[:endpoint].options[:for].to_s
17+
```
18+
419
### Upgrading to >= 1.2.0
520

621
#### Changes in the Grape::API class
722

23+
##### Patching the class
24+
825
In an effort to make APIs re-mountable, The class `Grape::API` no longer refers to an API instance,
926
rather, what used to be `Grape::API` is now `Grape::API::Instance` and `Grape::API` was replaced
1027
with a class that can contain several instances of `Grape::API`.
@@ -29,6 +46,23 @@ class Grape::API::Instance
2946
end
3047
```
3148

49+
##### `name` (and other caveats) of the mounted API
50+
51+
After the patch, the mounted API is no longer a Named class inheriting from `Grape::API`, it is an anonymous class
52+
which inherit from `Grape::API::Instance`.
53+
What this means in practice, is:
54+
- Generally: you can access the named class from the instance calling the getter `base`.
55+
- In particular: If you need the `name`, you can use `base`.`name`
56+
57+
**Deprecated**
58+
```ruby
59+
payload[:endpoint].options[:for].name
60+
```
61+
**New**
62+
```ruby
63+
payload[:endpoint].options[:for].base.name
64+
```
65+
3266
#### Changes in rescue_from returned object
3367

3468
Grape will now check the object returned from `rescue_from` and ensure that it is a `Rack::Response`. That makes sure response is valid and avoids exposing service information. Change any code that invoked `Rack::Response.new(...).finish` in a custom `rescue_from` block to `Rack::Response.new(...)` to comply with the validation.

lib/grape/api/instance.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def base=(grape_api)
1717
grape_api.instances << self
1818
end
1919

20+
def to_s
21+
(base && base.to_s) || super
22+
end
23+
2024
# A class-level lock to ensure the API is not compiled by multiple
2125
# threads simultaneously within the same process.
2226
LOCK = Mutex.new

spec/grape/named_api_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe 'A named API' do
4+
subject(:api_name) { NamedAPI.endpoints.last.options[:for].to_s }
5+
6+
let(:api) do
7+
Class.new(Grape::API) do
8+
get 'test' do
9+
'response'
10+
end
11+
end
12+
end
13+
14+
before { stub_const('NamedAPI', api) }
15+
16+
it 'can access the name of the API' do
17+
expect(api_name).to eq 'NamedAPI'
18+
end
19+
end

0 commit comments

Comments
 (0)