I am trying to create a skeleton for a REST API using Rails 4.0.4 and Grape 0.7.0 to behave like this:
Calling the API with a specific version:
$ curl -H Accept=application/vnd.acme-v1+json http://localhost:3000/api/a
“a-v1”
$ curl -H Accept=application/vnd.acme-v1+json http://localhost:3000/api/b
“b-v1”
Calling the API with as default:
$ curl http://localhost:3000/api/a
“a-v2”
$ curl http://localhost:3000/api/b
“b-v1”
$ curl http://localhost:3000/api/c
“c-v2”
I have been trying but I couldn't get the desired behavior. I ended up with the following files in my rails app:
app/api/api.rb
require 'grape'
require 'api_v1.rb'
require 'api_v2.rb'
module API
class Base < Grape::API
mount API::V2
mount API::V1
end
end
app/api/api_v1.rb
require 'grape'
module API
class V1 < Grape::API
version 'v1', using: :header, vendor: 'acme', format: :json
prefix 'api'
format :json
get :a do
"a-v1"
end
get :b do
"b-v1"
end
end
end
app/api/api_v2.rb
require 'grape'
module API
class V2 < Grape::API
version ['v2', 'v1'], using: :header, vendor: 'acme', cascade: true
prefix 'api'
format :json
get :a do
"a-v2"
end
get :c do
"c-v2"
end
end
end
app/config/routes.rb
...
mount API::Base => '/'
...
With above files I just get the default behavior, no matter which version I specify in the curl command.-
Grape does not (to my knowledge) allow an API class to specify an array of version strings, but I don't think you need to do that here anyway. Also, your curl syntax is incorrect.
Your code works for me once I change the version
line in app/api/api_v2.rb
to
version 'v2', using: :header, vendor: 'acme', cascade: true
and invoke curl using the correct syntax for the -H
parameter (note the colon in place of the equals sign):
$ curl -H Accept:application/vnd.acme-v1+json http://localhost:3000/api/a
"a-v1"
$ curl -H Accept:application/vnd.acme-v1+json http://localhost:3000/api/b
"b-v1"
$ curl http://localhost:3000/api/a
"a-v2"
$ curl http://localhost:3000/api/b
"b-v1"
$ curl http://localhost:3000/api/c
"c-v2"