Skip to content

Instantly share code, notes, and snippets.

@milmih
Last active August 29, 2015 14:04
Show Gist options
  • Save milmih/20f38f16c1d86a422d69 to your computer and use it in GitHub Desktop.
Save milmih/20f38f16c1d86a422d69 to your computer and use it in GitHub Desktop.
Simple ApplicationController stub for RESTful API and usage example
class ApplicationController < ActionController::Base
include ApplicationHelper
before_action :current_user_session, :current_user
rescue_from 'StandardError', 'Exception', 'ActiveRecord::RecordInvalid',
'ActionController::RoutingError', with: :exception_handler
private
def exception_handler(exception)
case exception
when ActiveRecord::RecordInvalid
status = :bad_request
when ActionController::RoutingError
status = :bad_request
else
status = :internal_server_error
end
render_response(nil, meta_message(exception.message), status)
end
def require_user
unless current_user
render_unauthorized_response(nil, meta_message('You have to be logged in to preform this action'))
return false
end
check_permissions
end
def require_no_user
if current_user
render_unauthorized_response(nil, meta_message('You have to be logged out to preform this action'))
end
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
// ...
end
module ApplicationHelper
def render_response(object, meta, code)
response_data = {data: object, meta: meta}
respond_to do |format|
format.json { render :json => response_data, status: code }
format.all { render :text => 'Only JSON format is supported at the moment.' }
end
end
def render_ok_response(object, meta = {})
render_response(object, meta, :ok)
end
def render_created_response(object, meta = {})
render_response(object, meta, :created)
end
def render_bad_request_response(object, meta = {})
render_response(object, meta, :bad_request)
end
def render_unauthorized_response(object, meta = {})
render_response(object, meta, :unauthorized)
end
def render_server_error_response(object, meta = {})
render_response(object, meta, :internal_server_error)
end
def meta_message(message)
{message: message}
end
def meta_created_message(clazz)
meta_message(clazz.name + ' successfully created')
end
def meta_updated_message(clazz)
meta_message(clazz.name + ' successfully updated')
end
def meta_destroyed_message(clazz)
meta_message(clazz.name + ' successfully destroyed')
end
def meta_no_entity_message(clazz, object_id, parent = nil)
message = 'No ' + clazz.name + ' with id ' + object_id
message += ' for ' + parent.class.name + ' with id ' + parent.id unless parent.nil?
meta_message(message)
end
end
class EntityController < ApplicationController
before_action :require_user
before_action :get_entity, :only => [:show]
def index
render_ok_response(Entity.all)
end
def show
render_ok_response(@entity)
end
// ...
private
def get_entity
@entity = Entity.find(params[:id])
if @entity.nil?
render_bad_request_response(@entity, meta_no_entity_message(Entity, params[:id]))
end
end
// ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment