How i organize my grape files in rails app

原创文章,转载请注明来源并保留原文链接

My file structure looks like this:

app/
  - api
    |- application_api.rb
    |- entities.rb
    |- helpers.rb
    |- root_api.rb
    |- users_api.rb
    |- pages_api.rb
    |- comments_api.rb
  - assets
  - controllers
...

Each api is inherited from application_api, just like application_controller.
eg:

class UsersAPI < ApplicationAPI
 
  get '/users' do
    ......
  end

end

Let’s take a look at application_api

require 'entities'
require 'helpers'

class ApplicationAPI < Grape::API
 
  def self.inherited(child)
    super
    child.format :json
    child.prefix "api"
    child.version 'v1', :using => :path
  end

end

If u wanna only one mount into your routes.rb, i mount all i need in root_api

class RootAPI < Grape::API
  mount PagesAPI
  mount UsersAPI
  mount CommentsAPI
end

routes.rb

mount RootAPI => '/'

Done!

Leave a Reply

Your email address will not be published. Required fields are marked *