cdunn / grape-jsonapi-resources

Serialize objects via JSONAPI::Resources for Grape API
MIT License
20 stars 8 forks source link

Grape::JSONAPIResources

Use jsonapi-resources with Grape!

Installation

Add the grape and grape-jsonapi-resources gems to Gemfile.

gem 'grape'
gem 'grape-jsonapi-resources'

Usage

Require grape-jsonapi-resources

Tell your API to use Grape::Formatter::JSONAPIResources

class API < Grape::API
  format :json
  formatter :json, Grape::Formatter::JSONAPIResources
end

Use render to specify JSONAPI options

get "/" do
  user = User.find("123")
  render user, include: ["account"], context: { something: :important }
end

Credit

Code adapted from grape-active_model_serializers