1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-05 16:24:21 +01:00
fab-manager/app/controllers/open_api/v1/base_controller.rb

61 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# module definition
module OpenAPI::V1; end
# Parameters for OpenAPI endpoints
2016-05-04 11:59:51 +02:00
class OpenAPI::V1::BaseController < ActionController::Base
protect_from_forgery with: :null_session
skip_before_action :verify_authenticity_token
2016-05-04 11:59:51 +02:00
before_action :authenticate
before_action :increment_calls_count
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from OpenAPI::ParameterError, with: :bad_request
rescue_from ActionController::ParameterMissing, with: :bad_request
rescue_from TypeError, with: :server_error
rescue_from NoMethodError, with: :server_error
rescue_from ArgumentError, with: :server_error
2016-05-04 11:59:51 +02:00
helper_method :current_api_client
protected
def not_found(exception)
render json: { errors: ['Not found', exception] }, status: :not_found
end
2016-05-04 11:59:51 +02:00
def bad_request(exception)
render json: { errors: ['Bad request', exception] }, status: :bad_request
end
2016-05-04 11:59:51 +02:00
def server_error(exception)
render json: { error: ['Server error', exception] }, status: :internal_server_error
end
2016-05-04 11:59:51 +02:00
def authenticate
authenticate_token || render_unauthorized
end
2016-05-04 11:59:51 +02:00
def authenticate_token
authenticate_with_http_token do |token, _options|
@open_api_client = OpenAPI::Client.find_by(token: token)
2016-05-04 11:59:51 +02:00
end
end
def current_api_client
@open_api_client
end
def render_unauthorized
render json: { errors: ['Bad credentials'] }, status: 401
end
2016-05-04 11:59:51 +02:00
private
def increment_calls_count
@open_api_client.increment_calls_count
end
2016-05-04 11:59:51 +02:00
end