Rest Responses plugin

Plugin details

RestResponses is a couple of useful little methods to clear up your restfull controllers

Websitehttp://code.google.com/p/rest-responses/ Repositoryhttp://rest-responses.googlecode.com/svn/trunk/rest_responses Author Alexander MacCaw Tags Rest, controller LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://rest-responses.googlecode.com/svn/trunk/rest_responses

Usage
=====

This plugin is basically a bunch of useful methods that return valid REST responses, while staying DRY.

The main method is 'responds' which returns xml/json/yaml to clients, depending on what they requested. If you needs to do anything at all fancy, like RJS etc, you'll probably need to code it out in full (with respond_to), otherwise, this'll tidy things up.

Another one that may need explaining is 'needs'. This basically ensures you have the correct parameters to requests, stopping you having to do extra error checking.

The usage for 'needs' and 'responds' is gone into with a bit more detail in rest_responses.rb

ActiveRecord::RecordNotFound and ActiveRecord::RecordInvalid exceptions are automatically caught (so you don't have to catch them for every method). Just remember to configure 'check_instance_variables'. See rest_responses.rb


Example of your average REST controller (don't forget the routes)
=================================================================

class UsersController < ApplicationController
    before_filter :find_user, :except => [:index, :new, :create]

	# GET /users
	def index
	  @users =  User.find(:all)
	  responds @users
	end

	# GET /users/1
	def show
	  responds @user
	end

	# GET /users/new
	def new
	  @user = User.new
	  responds @user
	end

	# GET /users/1/edit
	# def edit
	#   @user = User.find(params[:id])
	# end

	# POST /users
	def create
	  @user = User.new(params[:user])
	  @user.save!
	  responds_creation @user
	end

	# PUT /users/1
	def update
	  @user.update_attributes!(params[:user])
	  responds_updation @user
	end

	# DELETE /users/1
	def destroy
	  @user.destroy
	  responds_deletion
	end
	
	# POST /users/1/email
	# def email
	#   needs :from, :msg
	#   @user.email(params[:from], params[:to])
	#   responds_nothing
	# end

	private

	def	find_user
	 @user = User.find(params[:id])
	end
end

class ApplicationController < ActionController::Base
	def check_instance_variables
   		[:user]
 	end
end

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 9 months ago