Giter VIP home page Giter VIP logo

grape-active_model_serializers's Introduction

Grape::ActiveModelSerializers

Use active_model_serializers with Grape!

Build Status Dependency Status

Installation

Add the grape and grape-active_model_serializers gems to Gemfile.

gem 'grape'
gem 'grape-active_model_serializers'

And then execute:

bundle

Usage

Require grape-active_model_serializers

# config.ru
require 'grape/active_model_serializers'

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

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

Writing serializers

See active_model_serializers

Serializers are inferred by active_record model names

grape-active_model_serializers will search for serializers for the objects returned by your grape API.

namespace :users do
  get ":id" do
    @user = User.find(params[:id])
  end
end

In this case, as User objects are being returned, grape-active_model_serializers will look for a serializer named UserSerializer.

Disabling serializer inferrence

You can turn off serializer inferrence.

Grape::Formatter::ActiveModelSerializers.infer_serializers = false

Manually specifying serializer options

Serializers can be specified at a route level by with the serializer option. A serializer can be specified by passing the the serializer class or the serializer name. The following are equivalent:

get "/home", :serializer => HomeSerializer
...
get "/home", :serializer => "home"
...
get "/home", :serializer => :home
...

You can also set a serializer at the namespace level. This serializer can/will be overriden if a serilizer is also specified on the route.

namespace 'foo', :serializer => :bar do
  get "/" do
    # will use "bar" serializer
  end

  get "/home", :serializer => :home do
    # will use "home" serializer
  end
end

Other standard options for ActiveModel::Serializers can be provided at either the namespace or route level with the same overriding behavior.

get "/home", :root => 'world', :each_serializer => :fancy_home
...

Example

class User < ActiveRecord::Base
  attr_accessor :first_name, :last_name, :password, :email
end

class UserSerializer < ActiveModel::Serializer
  attributes :first_name, :last_name
end

class API < Grape::API
  get("/home") do
    User.new({first_name: 'JR', last_name: 'HE', email: '[email protected]'})
  end
end

API.new.get "/home" # => '{:user=>{:first_name=>"JR", :last_name=>"HE"}}'

Rspec

See "Writing Tests" in https://github.com/intridea/grape.

Enjoy :)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Thanks to

The developers and maintainers of: active_model_serializers Grape!

Structured and based upon Grape.

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.