Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split things up in a grape api app?

In every examples I see, people only implement one giant api.rb file. Ex:

  • intridea/grape
  • bloudraak/grape-sample-blog-api
  • djones/grape-goliath-example

While this approach works fine as is, it can quickly become crowded and difficult to maintain so I would like to split things up on my app.

For instance, I would like to split my entities from my resources, and then split up my resources between different files. For examples:

app
 - api
   api.rb
   - entities
     - weblog.rb
     - post.rb
     - comment.rb
   - resources
     - weblog.rb
     - post.rb
     - comment.rb

Now, api.rb would be something like:

require 'grape'
module Blog
  class API < Grape::API
    prefix "api"
  end
end

app/api/entities/post.rb would be something like:

module Blog
  module Entities
    class Post < Grape::Entity
      root 'posts', 'posts'
      expose :id
      expose :content
    end
  end
end

app/api/resources/post.rb would be something like:

module Blog
  class API < Grape::API
    resource :posts do
      get do
        present Post.all, with: Blog::Entities::Post
      end

      desc "returns the payment method corresponding to a certain id"
      params do
        requires :id, :type => Integer, :desc => "Post id."
      end
      get ':id' do
        present Post.find(params[:id]), with: Blog::Entities::Post
      end
    end
  end
end

When we do this, we encounter the following message:

Expected /blog-app/api/resources/post.rb to define Post


SOLUTION (thanks to dB. and my co-workers)

You have to change the structure to something like:

app
 - api
   api.rb
   - resources
     - post_api.rb

Then, in the post_api.rb

module Blog
  class Resources::PostAPI < Grape::API
    resource :posts do
      get do
        present Post.all
      end
    end
  end
end

Finally, the api.rb becomes:

require 'grape'
module Blog
  class API < Grape::API
    prefix 'api'
    version 'v1', :using => :path
    format :json

    mount Blog::Resources::PostAPI => '/'
  end
end

Now /api/v1/posts should work :)

like image 815
X2theZ Avatar asked Feb 13 '13 16:02

X2theZ


People also ask

What is grape API?

What is Grape? Grape is a REST-like API framework for Ruby. It's designed to run on Rack or complement existing web application frameworks such as Rails and Sinatra by providing a simple DSL to easily develop RESTful APIs.

What is grape version?

In viticulture, veraison (French: véraison, IPA: [veʁɛzɔ̃]) is the onset of the ripening of the grapes.


2 Answers

The class in post.rb should be Post, not API. Then you can mount the Post API inside class API.

class API < Grape::API
  mount Blog::Post => '/'
end

To avoid confusion I would put Post in a Resources namespace, too or rename it to PostAPI.

like image 95
dB. Avatar answered Oct 10 '22 03:10

dB.


I found it not working for path prefix:

mount Blog::Post => '/blog'

doesn't work if you want have prefix the path.

use

namespace :blog do
   mount Blog::Post
end

Hope it helps!

like image 35
psenthu Avatar answered Oct 10 '22 02:10

psenthu