Skip to content

Commit 091a331

Browse files
committed
Adds Readme.md to remountable APIs
1 parent ae879cb commit 091a331

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,59 @@ class Twitter::API < Grape::API
366366
end
367367
```
368368

369+
## Remounting
370+
371+
You can mount the same endpoints in two different locations using `RemountableAPI`
372+
373+
```ruby
374+
class Voting::API < Grape::RemountableAPI
375+
namespace 'votes' do
376+
get do
377+
# Your logic
378+
end
379+
380+
post do
381+
# Your logic
382+
end
383+
end
384+
end
385+
386+
class Post::API < Grape::API
387+
mount Voting::API
388+
end
389+
390+
class Comment::API < Grape::API
391+
mount Voting::API
392+
end
393+
```
394+
395+
Assuming that the post and comment endpoints are mounted in `/posts` and `/comments`,
396+
you should now be able to do `get /posts/votes`, `post /posts/votes`, `get /comments/votes`
397+
398+
### Configuration
399+
400+
You can configure remountable endpoints for small details changing according to where
401+
they are mounted
402+
403+
```ruby
404+
class Voting::API < Grape::RemountableAPI
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`,

0 commit comments

Comments
 (0)