From cb9b0151bee80b2dd3377e7d39223bf26851a884 Mon Sep 17 00:00:00 2001 From: Pushpa Raj Badu Date: Tue, 8 Mar 2016 18:56:26 +0545 Subject: [PATCH] update readme file with example usage for mounting multiple version in grape api related with: https://github.com/ruby-grape/grape/issues/1305\#issuecomment-193223103 --- README.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/README.md b/README.md index f13fca8829..3f8199ffea 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [Header](#header) - [Accept-Version Header](#accept-version-header) - [Param](#param) + - [Mounting multiple versions](#mounting-multiple-versions) - [Describing Methods](#describing-methods) - [Parameters](#parameters) - [Declared](#declared) @@ -390,6 +391,69 @@ 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'] + 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 even version ['v2', 'v1'] included in api.rb + # If you miss version block here, then + # /api/v1/test/first and + # /api/v2/test/first + # both will return 'v2 API' response, regardless of what version you supplied in the url. + + resource :test do + get do + 'v1 API' + end + end +end +``` + +`api/v2/api` + +```ruby +class V2::API < Grape::API + version 'v2' # required block even version ['v2', 'v1'] included in api.rb + + resource :test do + get do + 'v2 API' + end + end +end +``` + + curl http://localhost:3000/api/v2/test + # => "v1 API" + curl http://localhost:3000/api/v1/test + # => "v2 API" ## Describing Methods