Module: APIResourceMultigettable

Extended by:
ActiveSupport::Concern
Included in:
API::V1
Defined in:
app/api/concerns/api_resource_multigettable.rb

Overview

Helper To Make Resource APIs Multigettable

A normal resource API can let clients retrieve one specified data at a time:

GET /posts/3

If it's declared to be multigettable, then clients can retrieve multiple specified data like this:

GET /posts/3,4,8,9

Usage

Include this Concern in your Grape API class:

class SampleAPI < Grape::API
  include APIResourceMultigettable
end

then use the multiget method like this:

resources :posts do
  # ...
  get :id do
    @post = multiget(Post, find_by: :id, max: 12)    # ...

  end
end

The multiget method returns a array of or a single model, based directly from the requested URL. Further usage of this method can be found in the docs of the HelperMethods class.

There is also another helper method to determine whether the request is multigeting or not:

multiget?(find_by: id) #=> true of false

It can be used to interact with other condition and functionalities, like this:

inclusion_for :post, root: true,
  default_includes: (multiget?(find_by: :id) ? [] : [:author])

Defined Under Namespace

Modules: HelperMethods