title: How to split things up in a grape api app?
body: <p>In every examples I see, people only implement one giant api.rb file. Ex:</p>
  
  <ul>
  <li><a href="https://github.com/intridea/grape" rel="noreferrer">intridea/grape</a></li>
  <li><a href="https://github.com/bloudraak/grape-sample-blog-api/blob/master/app/api/blog/api.rb" rel="noreferrer">bloudraak/grape-sample-blog-api</a></li>
  <li><a href="https://github.com/djones/grape-goliath-example/blob/master/app/apis/posts.rb" rel="noreferrer">djones/grape-goliath-example</a></li>
  </ul>
  
  <p>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.</p>
  
  <p>For instance, I would like to split my entities from my resources, and then split up my resources between different files. For examples:</p>
  
  <pre><code>app
   - api
     api.rb
     - entities
       - weblog.rb
       - post.rb
       - comment.rb
     - resources
       - weblog.rb
       - post.rb
       - comment.rb
  </code></pre>
  
  <p>Now, <strong>api.rb</strong> would be something like:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>require 'grape'
  module Blog
    class API &lt; Grape::API
      prefix "api"
    end
  end
  </code></pre>
  
  <p><strong>app/api/entities/post.rb</strong> would be something like:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>module Blog
    module Entities
      class Post &lt; Grape::Entity
        root 'posts', 'posts'
        expose :id
        expose :content
      end
    end
  end
  </code></pre>
  
  <p><strong>app/api/resources/post.rb</strong> would be something like:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>module Blog
    class API &lt; 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 =&gt; Integer, :desc =&gt; "Post id."
        end
        get ':id' do
          present Post.find(params[:id]), with: Blog::Entities::Post
        end
      end
    end
  end
  </code></pre>
  
  <p>When we do this, we encounter the following message:</p>
  
  <blockquote>
    <p>Expected /blog-app/api/resources/post.rb to define Post</p>
  </blockquote>
  
  <hr>
  
  <p><strong>SOLUTION (thanks to dB. and my co-workers)</strong></p>
  
  <p>You have to change the structure to something like:</p>
  
  <pre><code>app
   - api
     api.rb
     - resources
       - post_api.rb
  </code></pre>
  
  <p>Then, in the <strong>post_api.rb</strong>
  </p>
  
  <pre><code>module Blog
    class Resources::PostAPI &lt; Grape::API
      resource :posts do
        get do
          present Post.all
        end
      end
    end
  end
  </code></pre>
  
  <p>Finally, the <strong>api.rb</strong> becomes:</p>
  
  <pre class="lang-ruby prettyprint-override"><code>require 'grape'
  module Blog
    class API &lt; Grape::API
      prefix 'api'
      version 'v1', :using =&gt; :path
      format :json
  
      mount Blog::Resources::PostAPI =&gt; '/'
    end
  end
  </code></pre>
  
  <p>Now <code>/api/v1/posts</code> should work :)</p>