Rails Grape API versioning -


i trying create skeleton rest api using rails 4.0.4 , grape 0.7.0 behave this:

calling api 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 api 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 couldn't desired behavior. ended following files in 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     :a       "a-v1"     end     :b       "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     :a       "a-v2"     end     :c       "c-v2"     end   end end 

app/config/routes.rb

... mount api::base => '/' ... 

with above files default behavior, no matter version specify in curl command.-

grape not (to knowledge) allow api class specify array of version strings, don't think need here anyway. also, curl syntax incorrect.

your code works me once change version line in app/api/api_v2.rb to

version 'v2', using: :header, vendor: 'acme', cascade: true 

and invoke curl using correct syntax -h parameter (note colon in place of 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" 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -