1

I am building web API with Grape gem on Ruby on Rails 4.1, with their 'version' function.

Sample code here.

# app/api/api.rb
class API < Grape::API
  prefix 'api'
  format :json
  formatter :json, Grape::Formatter::Rabl
  default_format :json

  mount V1::Root
end

# app/api/v1/root.rb
module V1
  class Root < Grape::API
    version 'v1'
    resource :users, rabl: "users" do
      get '/' do
        @users = User.all
      end
    end
  end
end

# config/routes.rb
mount API => "/"

with these code, app/views/api/users.rabl is used for view template on request to http://localhost:3000/api/v1/users.

I want to use template in app/views/api/v1 for v1 request. Is there any way to do that ?

current

  • /api/v1/users -> app/views/api/users.rabl
  • /api/v2/users -> app/views/api/users.rabl

want

  • /api/v1/users -> app/views/api/v1/users.rabl
  • /api/v2/users -> app/views/api/v2/users.rabl
xoyip
  • 82
  • 6
  • 1
    I looking for some great solutions, but it seems to rabl-grape gem native does not support grape versioning, maybe you can try: 'resource :users, rabl: "v1/users" do' ... Iam not sure if it will be work, cause Iam not able to test it right now.. – Jan Strnádek Oct 28 '14 at 08:50
  • Thanks Jan, your solution is one way to switch version of views. If there is no other solutions, I'll take this way. – xoyip Oct 28 '14 at 11:01
  • Did you end up getting a result? – jufemaiz Jul 02 '15 at 04:09
  • Yes, finally I took Jan's way. – xoyip Jul 03 '15 at 10:51

2 Answers2

0

I'm using Grape Entity:https://github.com/intridea/grape-entity

So I created a directory on v1 folder called entities

ex: api/v1/entities/token_response_entity.rb

module ExampleAPI::V1::Entities
  class TokenResponseEntity < Grape::Entity
    expose :token , documentation: { type: 'string', desc: 'Token String' }
  end
end

So when I need to present I just need to use:

present tokens, with: ExampleAPI::V1::Entities::TokenResponseEntity

Ivan Santos
  • 626
  • 2
  • 7
  • 19
0

I finally took Jan's way.

resource :users, rabl: "v1/users" do
xoyip
  • 82
  • 6