Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update readme file with example usage for mounting multiple versions #1311

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [Header](#header)
- [Accept-Version Header](#accept-version-header)
- [Param](#param)
- [Mounting multiple versions](#mounting-multiple-versions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalize Multiple Versions

- [Describing Methods](#describing-methods)
- [Parameters](#parameters)
- [Declared](#declared)
Expand Down Expand Up @@ -390,6 +391,68 @@ version 'v1', using: :param, parameter: 'v'

curl http://localhost:9292/statuses/public_timeline?v=v1

## Mounting multiple versions

Example directory structure.


|- api/
|-- api.rb
|-- v1/
|--- api.rb
|-- v2/
|--- api.rb


`api/api.rb`

```ruby
class API < Grape::API
prefix 'api'
version ['v2', 'v1']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect though, no? It doesn't do anything as far as I know. You don't need format either here, as those don't get inherited.

format :json
default_format :json

mount V2::API
mount V1::API
end
```

`api/v1/api.rb`

```ruby
class V1::API < Grape::API
version 'v1' # required block although `version ['v2', 'v1']` included in api.rb

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove one of the empty lines.


resource :test do
get do
'v1 API'
end
end
end
```

`api/v2/api`

```ruby
class V2::API < Grape::API
version 'v2' # required block although `version ['v2', 'v1']` included in api.rb

resource :test do
get do
'v2 API'
end
end
end
```

curl http://localhost:3000/api/v1/test
# => "v1 API"
curl http://localhost:3000/api/v2/test
# => "v2 API"

> NOTE: In above example, if you do not include `version 'v1'` in `api/v1/api.rb` and `version 'v2'` in `api/v2/api.rb`, then `curl http://localhost:3000/api/v1/test` and `curl http://localhost:3000/api/v2/test` both will return same response from `v2` api, regardless of whatever version you supply in API endpoint.

## Describing Methods

Expand Down