Project

rest_area

0.0
No commit activity in last 3 years
No release in over 3 years
RestArea adds a restfull controller and api to any Rails Application, simply add the gem and whitelist of available models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.5.1
~> 0.7
~> 1.0
~> 0.9
~> 1.3
~> 0.7.1

Runtime

~> 4.2
 Project Readme

Code Climate Build Status Coverage Status Gem Version Dependency Status

RestArea

This gem adds a bare bones rest api to a rails application. Simply create the initilizer file with the classes that you want to be in the rest api and you will get the get basic rest routes for that class This project rocks and uses MIT-LICENSE.

Versions

This library supports both Rails 3.2.x and Rails 4.1.x.

Rails 3.2.x

If you are using Rails 3.2.x you should use the rails-3_2 branch and/or version/tags 1.x.x, ie

gem 'rest_area', '~>1.0'

Rails 4.1.x

If you are using Rails 4.1.x you should use the master branch and/or versions/tags 2.x.x, ie

gem 'rest_area', '~>2.0'

Initialization / Setup

  1. Create a file called rest_area.rb in your initializers file

  2. Add configuration like the following in that file.

     RestArea.configure do
       resources :thing_one, :thing_two     # Defaults to all actions
    
       resources :cereal, :thing do
         action :index, :show, :create, :update, :delete
         key :name
         headers({
           'Cache-Control' => 'public, max-age=86400'
           'Expires' => ->{Date.today + 1}
         })
       end
    
       resource :supermarket do
         read_only!
         messages :say_hello, :ring_it_up
       end
     end
    

Alternatively you can still just pass in a class whitelist, but this is being deprecated.

    RestArea.class_whitelist = [YourModel, ThatYouWant, ToBeInYour, RestApi]
  1. Add something like the following to your config/routes.rb file.

     mount RestArea::Engine => "/your_base_route"
    

Config Settings

Within the RestArea.config block, use resource to specify a single resource and resources to specify mulitiple resources

With in the resource / resources block,

  • Use action to specify what actions that resource will respond to, the available actions are :index, :show, :create, :create, :update, and :delete, the default is to use all actions

  • Use read_only! to specify that the resource will only respond to the index and show actions

  • Use key to specify what column rest_area will use to look up a resource. This is the column name that is used for the :key in /:resource/:key part of the restful path. This defaults to :id

  • Use message or messages to whitelist methods for a class. Methods much be defined at launch. Methods must respond with an object that responds to .to_json

  • Use headers to specify the response headers hash, this will be merged with existing headers

Serializers

But what if I want to customize the JSON that comes back? Simple this gem supports active_model_serializer. Go there, ignore the stuff about controllers.

NOTE You must use AMS 0.8.x, things are not working for AMS 0.9.x+