2012-10-26 10:58:02 +00:00
|
|
|
class ApiController < ApplicationController
|
|
|
|
|
|
2015-01-01 05:17:47 +00:00
|
|
|
def log
|
|
|
|
|
@log || Logging.logger[ApiController]
|
|
|
|
|
end
|
|
|
|
|
|
2014-08-07 04:50:10 +00:00
|
|
|
@@html_encoder = HTMLEntities.new
|
2012-10-27 22:26:45 +00:00
|
|
|
|
2012-10-26 10:58:02 +00:00
|
|
|
# define common error handlers
|
|
|
|
|
rescue_from 'JamRuby::StateError' do |exception|
|
|
|
|
|
@exception = exception
|
2012-11-25 19:38:24 +00:00
|
|
|
render "errors/state_error", :status => 400
|
2012-10-26 10:58:02 +00:00
|
|
|
end
|
2012-11-22 08:27:00 +00:00
|
|
|
rescue_from 'JamRuby::JamArgumentError' do |exception|
|
2012-10-26 10:58:02 +00:00
|
|
|
@exception = exception
|
2014-03-20 11:53:26 +00:00
|
|
|
render "errors/jam_argument_error", :status => 422
|
2012-10-26 10:58:02 +00:00
|
|
|
end
|
2015-04-20 14:50:33 +00:00
|
|
|
rescue_from 'JamRuby::JamPermissionError' do |exception|
|
2012-10-26 10:58:02 +00:00
|
|
|
@exception = exception
|
2012-11-22 08:27:00 +00:00
|
|
|
render "errors/permission_error", :status => 403
|
2012-10-26 10:58:02 +00:00
|
|
|
end
|
2016-03-29 10:50:14 +00:00
|
|
|
rescue_from 'JamRuby::JamRecordNotFound' do |exception|
|
|
|
|
|
@exception = exception
|
|
|
|
|
render "errors/record_not_found", :status => 404
|
|
|
|
|
end
|
2014-10-26 02:23:52 +00:00
|
|
|
rescue_from 'JamRuby::ConflictError' do |exception|
|
|
|
|
|
@exception = exception
|
|
|
|
|
render "errors/conflict_error", :status => 409
|
|
|
|
|
end
|
2012-10-26 10:58:02 +00:00
|
|
|
rescue_from 'ActiveRecord::RecordNotFound' do |exception|
|
2015-01-01 05:17:47 +00:00
|
|
|
log.debug(exception)
|
2012-10-27 22:26:45 +00:00
|
|
|
render :json => { :errors => { :resource => ["record not found"] } }, :status => 404
|
2012-10-26 10:58:02 +00:00
|
|
|
end
|
2012-10-27 22:26:45 +00:00
|
|
|
rescue_from 'PG::Error' do |exception|
|
2015-01-01 05:17:47 +00:00
|
|
|
log.debug(exception)
|
2012-10-27 22:26:45 +00:00
|
|
|
if exception.to_s.include? "duplicate key value violates unique constraint"
|
|
|
|
|
render :json => { :errors => { :resource => ["resource already exists"] } }, :status => 409 # 409 = conflict
|
|
|
|
|
else
|
|
|
|
|
raise exception
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-11-24 18:23:13 +00:00
|
|
|
|
2014-02-19 22:56:13 +00:00
|
|
|
|
2012-11-24 18:23:13 +00:00
|
|
|
protected
|
2014-02-19 22:56:13 +00:00
|
|
|
|
|
|
|
|
def respond_with_model(model, options = {})
|
|
|
|
|
if model.errors.any?
|
2014-02-24 16:55:56 +00:00
|
|
|
respond_with model, status: :unprocessable_entity, layout: nil
|
2014-02-19 22:56:13 +00:00
|
|
|
else
|
|
|
|
|
status = options[:new] && options[:new] == true ? 201 : 200
|
|
|
|
|
redirect_on_success = options[:location]
|
|
|
|
|
if redirect_on_success
|
|
|
|
|
location = redirect_on_success.call
|
|
|
|
|
raise "location must return something" unless location # development time error
|
2014-02-20 22:23:44 +00:00
|
|
|
respond_with model, responder: ApiResponder, status: status, location: location, layout: nil
|
2014-02-19 22:56:13 +00:00
|
|
|
else
|
2014-02-20 22:56:00 +00:00
|
|
|
respond_with model, responder: ApiResponder, status: status, location:nil
|
2014-02-19 22:56:13 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2012-12-14 03:32:51 +00:00
|
|
|
def auth_user
|
|
|
|
|
unless current_user.id == params[:id]
|
2015-04-20 14:50:33 +00:00
|
|
|
raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
2012-11-24 18:23:13 +00:00
|
|
|
end
|
2012-12-14 03:32:51 +00:00
|
|
|
|
|
|
|
|
@user = User.find(params[:id])
|
2012-11-24 18:23:13 +00:00
|
|
|
end
|
2013-09-30 02:37:22 +00:00
|
|
|
|
|
|
|
|
def optional_auth_user
|
|
|
|
|
if current_user.nil?
|
|
|
|
|
@user = nil
|
|
|
|
|
else
|
|
|
|
|
auth_user
|
|
|
|
|
end
|
2015-05-28 13:20:14 +00:00
|
|
|
end
|
2013-09-30 02:37:22 +00:00
|
|
|
|
2015-05-28 13:20:14 +00:00
|
|
|
def affiliate_partner
|
|
|
|
|
if params[:affiliate_id]
|
|
|
|
|
@partner = AffiliatePartner.find(params[:affiliate_id])
|
|
|
|
|
if @partner.partner_user.nil?
|
|
|
|
|
raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
|
|
|
|
end
|
|
|
|
|
elsif current_user
|
|
|
|
|
@partner = current_user.affiliate_partner
|
|
|
|
|
else
|
|
|
|
|
raise JamPermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
|
|
|
|
end
|
2013-09-30 02:37:22 +00:00
|
|
|
end
|
2015-05-28 13:20:14 +00:00
|
|
|
end
|