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