Coder Social home page Coder Social logo

emcousin / grape-jsonapi Goto Github PK

View Code? Open in Web Editor NEW
37.0 2.0 17.0 130 KB

Use jsonapi-serializer with Grape

License: MIT License

Ruby 100.00%
grape grape-api jsonapi netflix rails fast-jsonapi ruby ruby-on-rails gem fastjsonapi model-parser swagger

grape-jsonapi's Introduction

CircleCI

Grape::Jsonapi

Use jsonapi-serializer with Grape.

Installation

Add grape-jsonapi to your Gemfile.

gem 'grape-jsonapi', require: "grape_jsonapi"

Usage

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

class API < Grape::API
  content_type :jsonapi, "application/vnd.api+json"
  formatter :json, Grape::Formatter::Jsonapi
  formatter :jsonapi, Grape::Formatter::Jsonapi
end

Use render to specify JSONAPI options

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

Use a custom serializer

get "/" do
  user = User.find("123")
  render user, serializer: 'CustomUserSerializer'
end

Or

get "/" do
  user = User.find("123")
  render CustomUserSerializer.new(user).serialized_json
end

Override metaand links properties

meta and links properties are usually defined per resource within your serializer (here and here)

However, if you need to override those properties, you can pass them as options when rendering your response:

user = User.find("123")
render user, meta: { pagination: { page: 1, total: 42 } }, links: { self: 'https://my-awesome.app.com/users/1' }

Model parser for response documentation

When using Grape with Swagger via grape-swagger, you can generate response documentation automatically via the provided following model parser:

# FastJsonapi serializer example

# app/serializers/base_serializer.rb
class BaseSerializer; end
# app/serializers/user_serializer.rb
class UserSerializer < BaseSerializer
  include JSONAPI::Serializer

  set_type :user
  has_many :orders

  attributes :name, :email
end

# config/initializers/grape_swagger.rb
GrapeSwagger.model_parsers.register(GrapeSwagger::Jsonapi::Parser, BaseSerializer)

# Your grape API endpoint
desc 'Get current user' do
  success code: 200, model: UserSerializer, message: 'The current user'
# [...]
end

Note that you need the grape-swagger gem for this to work, otherwise it will throw an error.

Credit

Code adapted from grape-jsonapi-resources

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.