Fixed codeclimate issues

This commit is contained in:
Sergei Tsoganov 2023-07-13 12:32:33 +03:00
parent 65ae5adbb8
commit cbe78e505b
3 changed files with 84 additions and 74 deletions

View file

@ -3,7 +3,8 @@ module Repp
class BaseController < ActionController::API # rubocop:disable Metrics/ClassLength
attr_reader :current_user
around_action :log_request
include ErrorAndLogHandler
before_action :authenticate_user
before_action :set_locale
before_action :validate_webclient_ca
@ -13,73 +14,6 @@ module Repp
private
# rubocop:disable Metrics/MethodLength
def log_request
yield
rescue ActiveRecord::RecordNotFound
handle_record_not_found
rescue ActionController::ParameterMissing, Apipie::ParamMissing => e
handle_parameter_missing(e)
rescue Apipie::ParamInvalid => e
handle_param_invalid(e)
rescue CanCan::AccessDenied => e
handle_access_denied(e)
rescue Shunter::ThrottleError => e
handle_throttle_error(e)
ensure
create_repp_log
end
# rubocop:enable Metrics/MethodLength
def handle_record_not_found
@response = { code: 2303, message: 'Object does not exist' }
render(json: @response, status: :not_found)
end
def handle_parameter_missing(e)
@response = { code: 2003, message: e.message.gsub(/\n/, '. ') }
render(json: @response, status: :bad_request)
end
def handle_param_invalid(e)
@response = { code: 2005, message: e.message.gsub(/\n/, '. ') }
render(json: @response, status: :bad_request)
end
def handle_access_denied(e)
@response = { code: 2201, message: 'Authorization error' }
logger.error e.to_s
render(json: @response, status: :unauthorized)
end
def handle_throttle_error(e)
@response = { code: 2502, message: Shunter.default_error_message }
logger.error e.to_s unless Rails.env.test?
render(json: @response, status: :bad_request)
end
def create_repp_log
log_attributes = build_log_attributes
ApiLog::ReppLog.create(log_attributes)
end
def build_log_attributes
{
request_path: request.path, ip: request.ip,
request_method: request.request_method,
request_params: build_request_params_json,
uuid: request.try(:uuid),
response: @response.to_json,
response_code: response.status,
api_user_name: current_user.try(:username),
api_user_registrar: current_user.try(:registrar).try(:to_s)
}
end
def build_request_params_json
request.params.except('route_info').to_json
end
def set_domain
registrar = current_user.registrar
@domain = Epp::Domain.find_by(registrar: registrar, name: params[:domain_id])
@ -234,10 +168,6 @@ module Repp
request.headers['Requester'] == 'tara'
end
def logger
Rails.logger
end
def auth_values_to_data(registrar:)
data = current_user.as_json(only: %i[id username roles])
data[:registrar_name] = registrar.name