Skip to content

Commit ca80feb

Browse files
authored
Merge pull request ruby-grape#1803 from myxoh/replacing-api
Remountable APIs, allows to re-mounting all APIs
2 parents 1a9d713 + ce4966d commit ca80feb

File tree

12 files changed

+481
-211
lines changed

12 files changed

+481
-211
lines changed

.rubocop_todo.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ Metrics/CyclomaticComplexity:
5050
# URISchemes: http, https
5151
Metrics/LineLength:
5252
Max: 215
53-
5453
# Offense count: 57
5554
# Configuration parameters: CountComments.
5655
Metrics/MethodLength:
@@ -60,6 +59,8 @@ Metrics/MethodLength:
6059
# Configuration parameters: CountComments.
6160
Metrics/ModuleLength:
6261
Max: 212
62+
Exclude:
63+
- 'spec/grape/dsl/routing_spec.rb'
6364

6465
# Offense count: 21
6566
Metrics/PerceivedComplexity:
@@ -87,7 +88,7 @@ Naming/HeredocDelimiterNaming:
8788
# Configuration parameters: AutoCorrect.
8889
Performance/HashEachMethods:
8990
Exclude:
90-
- 'lib/grape/api.rb'
91+
- 'lib/grape/api/instance.rb'
9192
- 'lib/grape/middleware/versioner/header.rb'
9293

9394
# Offense count: 1

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
### 1.1.1 (Next)
1+
### 1.2.0 (Next)
22

33
#### Features
44

55
* Your contribution here.
6+
* [#1795](https://github.com/ruby-grape/grape/pull/1803): Adds the ability to re-mount all endpoints in any location - [@myxoh](https://github.com/bschmeck).
67
* [#1795](https://github.com/ruby-grape/grape/pull/1795): Fix vendor/subtype parsing of an invalid Accept header - [@bschmeck](https://github.com/bschmeck).
78
* [#1791](https://github.com/ruby-grape/grape/pull/1791): Support `summary`, `hidden`, `deprecated`, `is_array`, `nickname`, `produces`, `consumes`, `tags` options in `desc` block - [@darren987469](https://github.com/darren987469).
89

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
- [Alongside Sinatra (or other frameworks)](#alongside-sinatra-or-other-frameworks)
2323
- [Rails](#rails)
2424
- [Modules](#modules)
25+
- [Remounting](#remounting)
26+
- [Configuration](#configuration)
2527
- [Versioning](#versioning)
2628
- [Path](#path)
2729
- [Header](#header)
@@ -366,6 +368,57 @@ class Twitter::API < Grape::API
366368
end
367369
```
368370

371+
## Remounting
372+
373+
You can mount the same endpoints in two different locations.
374+
375+
```ruby
376+
class Voting::API < Grape::API
377+
namespace 'votes' do
378+
get do
379+
# Your logic
380+
end
381+
382+
post do
383+
# Your logic
384+
end
385+
end
386+
end
387+
388+
class Post::API < Grape::API
389+
mount Voting::API
390+
end
391+
392+
class Comment::API < Grape::API
393+
mount Voting::API
394+
end
395+
```
396+
397+
Assuming that the post and comment endpoints are mounted in `/posts` and `/comments`, you should now be able to do `get /posts/votes`, `post /posts/votes`, `get /comments/votes`.
398+
399+
### Configuration
400+
401+
You can configure remountable endpoints for small details changing according to where they are mounted.
402+
403+
```ruby
404+
class Voting::API < Grape::API
405+
namespace 'votes' do
406+
desc "Vote for your #{configuration[:votable]}"
407+
get do
408+
# Your logic
409+
end
410+
end
411+
end
412+
413+
class Post::API < Grape::API
414+
mount Voting::API, with: { votable: 'posts' }
415+
end
416+
417+
class Comment::API < Grape::API
418+
mount Voting::API, with: { votable: 'comments' }
419+
end
420+
```
421+
369422
## Versioning
370423

371424
There are four strategies in which clients can reach your API's endpoints: `:path`,

UPGRADING.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
Upgrading Grape
22
===============
33

4-
### Upgrading to >= 1.1.1
4+
### Upgrading to >= 1.2.0
5+
6+
#### Changes in the Grape::API class
7+
8+
In an effort to make APIs re-mountable, The class `Grape::API` no longer refers to an API instance,
9+
rather, what used to be `Grape::API` is now `Grape::API::Instance` and `Grape::API` was replaced
10+
with a class that can contain several instances of `Grape::API`.
11+
12+
This changes were done in such a way that no code-changes should be required.
13+
However, if experiencing problems, or relying on private methods and internal behaviour too deeply, it is possible to restore the prior behaviour by replacing the references from `Grape::API` to `Grape::API::Instance`.
514

615
#### Changes in rescue_from returned object
716

@@ -10,9 +19,9 @@ Grape will now check the object returned from `rescue_from` and ensure that it i
1019
```ruby
1120
class Twitter::API < Grape::API
1221
rescue_from :all do |e|
13-
# version prior to 1.1.1
22+
# version prior to 1.2.0
1423
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' }).finish
15-
# 1.1.1 version
24+
# 1.2.0 version
1625
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' })
1726
end
1827
end

0 commit comments

Comments
 (0)