Skip to content

Instantly share code, notes, and snippets.

@leandromarques
Created February 3, 2017 15:25
Show Gist options
  • Save leandromarques/95052679ed027636bfa8aba356c235ff to your computer and use it in GitHub Desktop.
Save leandromarques/95052679ed027636bfa8aba356c235ff to your computer and use it in GitHub Desktop.
class Api::V1::BaseController < ApplicationController
protect_from_forgery with: :null_session
serialization_scope :current_user
include ResourceLoader
include TokenAuthentication
after_action :build_response_headers
rescue_from ActiveRecord::RecordNotFound do |exception|
render json: { errors: ["Resource not found."] }, status: :not_found
end
rescue_from ActionController::ParameterMissing do |exception|
render json: { errors: ["Missing parameters: #{list_to_comma_string(exception.param)}"] }, status: :bad_request
end
rescue_from AuthenticationError::InvalidCredentials do |exception|
render json: { errors: ["Invalid credentials"] }, status: :unauthorized
end
rescue_from AuthenticationError::Unauthorized do |exception|
render json: { errors: ["Unauthorized"] }, status: :unauthorized
end
rescue_from ActiveRecord::RecordInvalid do |exception|
errors = exception.to_s.match(/\:(.*)/)
errors = errors[1][1..errors[1].length].split(', ')
render json: { errors: errors }, status: :unprocessable_entity
end
def require_parameters(required_params=[])
missing_params = Array.new
required_params.map { |p| missing_params << p unless params.has_key? p }
raise ActionController::ParameterMissing.new(missing_params) if missing_params.any?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment