Project

grapeful

0.0
No commit activity in last 3 years
No release in over 3 years
Custom Response builder for Grape
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

~> 0.6.0
 Project Readme

Grapeful

Custom Response builder for Grape.

Installation

Add this line to your application's Gemfile:

gem 'grapeful'

And then execute:

$ bundle

Or install it yourself as:

$ gem install grapeful

Usage

Hello world example

  class API < Grape::API
    get :hello do
      ful :hello, 'world'
    end
  end

The response will be

  {
    "hello": "world"
  }

Using with ActiveRecord

  class API < Grape::API
    resources :users do
      get '/' do
        users = User.recent
        ful :users, users
        ful :total, users.length if current_user.admin?
      end
    end
  end

The response will be

  {
    "users": [
        {
            "id": 21,
            "name": "John Smith",
            "uic": "john",
            "role": "admin",
            "created_at": "2013-08-14T10:12:30.933Z"
        }
    ],
    "total": 1
  }

Using with active_model_serializers

  class API < Grape::API
    resources :users do

      params { requires :user, type: Hash, desc: 'User data' }
      post '/' do
        user = User.new(params[:user])
        user.save!
        ful :user, UserSerializer.new(user), message: 'The user was successfuly created.'
      end
    end
  end

The response will be

  {
    "user": {
            "id": 25,
            "name": "Don Dan",
            "uic": "don",
            "role": "admin",
            "created_at": "2013-08-14T10:12:30.933Z"
    },
    "message": "The user was successfully created."
  }

Contributing

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