1
我试图创建一个使用Rails 4.0.4和0.7.0葡萄一个REST API的骨架,以这样的表现:Rails的葡萄API版本
调用与特定版本的API:
$ 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”
与调用默认的API:
$ curl http://localhost:3000/api/a
“a-v2”
$ curl http://localhost:3000/api/b
“b-v1”
$ curl http://localhost:3000/api/c
“c-v2”
我一直在努力,但我无法得到所需的行为。我结束了我的Rails应用程序中的以下文件:
应用程序/ 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
应用程序/ 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
应用程序/ 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
应用/配置/ routes.rb中
...
mount API::Base => '/'
...
有了上面的文件我只是得到的默认行为,不管我在卷曲指定版本command.-
它的工作原理,我改变了我的卷曲调用,我删除了你说的版本数组。尽管我看不到'cascade:true'语句有什么影响,但是我删除了它,并且它也工作正常。它的重要性在于我们在app/api/api.rb中安装API版本的顺序。 – hector