No description
Find a file
Jonathan Richard Henry Evans 25e19804c3 Removes rspec version specifier
2013-09-09 11:15:44 +01:00
lib Refactors Formatter 2013-08-29 16:41:03 +01:00
spec Added guard-rspec 2013-08-30 09:56:25 +01:00
.DS_Store Added guard-rspec 2013-08-30 09:56:25 +01:00
.gitignore Adds DS_Store to gitignore 2013-08-30 09:58:32 +01:00
Gemfile Added jazz_hands for debugging in development 2013-08-29 15:22:29 +01:00
grape-active_model_serializers.gemspec Removes rspec version specifier 2013-09-09 11:15:44 +01:00
Guardfile Added guard-rspec 2013-08-30 09:56:25 +01:00
LICENSE.txt Added MIT License 2013-08-11 00:01:26 +01:00
Rakefile Initial commit 2013-04-04 20:10:51 +01:00
README.md Refactors Formatter 2013-08-29 16:41:03 +01:00

Grape::ActiveModelSerializers

Use active_model_serializers with Grape!

Build Status Dependency Status Code Climate

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.

Manually specifying serializer options

# Serializer options can be specified on routes or namespaces.
namespace 'foo', :serializer => :bar do
  get "/" do
    # will use "bar" serializer
  end

  # Options specified on a route or namespace override those of the containing namespace.
  get "/home", :serializer => :home do
    # will use "home" serializer
  end

  # All standard options for `ActiveModel::Serializers` are supported.
  get "/fancy_homes", :root => 'world', :each_serializer => :fancy_homes
  ...
  end
end

Full 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: 'contact@jrhe.co.uk'})
  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-rabl.