Class: Arkaan::Utils::ControllerWithoutFilter
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Arkaan::Utils::ControllerWithoutFilter
- Defined in:
- lib/arkaan/utils/controller_without_filter.rb
Overview
Base controller to handle the standard error when accessing the API.
Direct Known Subclasses
Class Method Summary collapse
-
.declare_premium_route(verb, path, &block) ⇒ Object
Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
-
.declare_route(verb, path, &block) ⇒ Object
Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
-
.declare_route_with(verb, path, premium, &block) ⇒ Object
Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
-
.load_errors_from(file) ⇒ Object
Loads the errors configuration file from the config folder.
Instance Method Summary collapse
-
#add_body_to_params ⇒ Object
Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values of the SON body if they have similar keys.
- #check_application(action) ⇒ Object
-
#check_presence(*fields, route:) ⇒ Object
Checks the presence of several fields given as parameters and halts the execution if it’s not present.
-
#check_session(action) ⇒ Arkaan::Authentication::Session
Checks if the session ID is given in the parameters and if the session exists.
-
#custom_error(status, path) ⇒ Object
Halts the application and creates the returned body from the parameters and the errors config file.
-
#handle_arkaan_exception(exception) ⇒ Object
Creates a custom error from an existing Arkaan exception class.
-
#model_error(instance, route) ⇒ Object
Halts the application with a Bad Request error affecting a field of a model.
-
#parse_current_route ⇒ Arkaan::Monitoring::Route
Gets the current route in the database from the sinatra route.
-
#select_params(*fields) ⇒ Hash
Select parameters in the params hash, by its keys.
Class Method Details
.declare_premium_route(verb, path, &block) ⇒ Object
Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
18 19 20 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 18 def self.declare_premium_route(verb, path, &block) self.declare_route_with(verb, path, true, &block) end |
.declare_route(verb, path, &block) ⇒ Object
Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
11 12 13 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 11 def self.declare_route(verb, path, &block) self.declare_route_with(verb, path, false, &block) end |
.declare_route_with(verb, path, premium, &block) ⇒ Object
Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 26 def self.declare_route_with(verb, path, premium, &block) service = Arkaan::Utils::MicroService.instance.service unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil? Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service) end if premium self.public_send(verb, path) do @sinatra_route = parse_current_route if !@application.premium? custom_error(403, 'common.app_key.forbidden') end instance_eval(&block) end else self.public_send(verb, path, &block) end end |
.load_errors_from(file) ⇒ Object
Loads the errors configuration file from the config folder.
46 47 48 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 46 def self.load_errors_from(file) config_file File.join(File.dirname(file), '..', 'config', 'errors.yml') end |
Instance Method Details
#add_body_to_params ⇒ Object
Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values of the SON body if they have similar keys.
79 80 81 82 83 84 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 79 def add_body_to_params parsed_body = JSON.parse(request.body.read.to_s) rescue {} parsed_body.keys.each do |key| params[key] = parsed_body[key] end end |
#check_application(action) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 70 def check_application(action) check_presence('app_key', route: action) application = Arkaan::OAuth::Application.where(key: params['app_key']).first custom_error(404, "#{action}.app_key.unknown") if application.nil? return application end |
#check_presence(*fields, route:) ⇒ Object
Checks the presence of several fields given as parameters and halts the execution if it’s not present.
52 53 54 55 56 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 52 def check_presence(*fields, route:) fields.each do |field| custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == '' end end |
#check_session(action) ⇒ Arkaan::Authentication::Session
Checks if the session ID is given in the parameters and if the session exists.
61 62 63 64 65 66 67 68 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 61 def check_session(action) check_presence('session_id', route: action) session = Arkaan::Authentication::Session.where(token: params['session_id']).first if session.nil? custom_error(404, "#{action}.session_id.unknown") end return session end |
#custom_error(status, path) ⇒ Object
Halts the application and creates the returned body from the parameters and the errors config file.
96 97 98 99 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 96 def custom_error(status, path) route, field, error = path.split('.') halt status, {status: status, field: field, error: error, docs: settings.errors[route][field][error]}.to_json end |
#handle_arkaan_exception(exception) ⇒ Object
Creates a custom error from an existing Arkaan exception class.
119 120 121 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 119 def handle_arkaan_exception(exception) custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}") end |
#model_error(instance, route) ⇒ Object
Halts the application with a Bad Request error affecting a field of a model.
104 105 106 107 108 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 104 def model_error(instance, route) = instance.errors. field = .keys.first custom_error(400, "#{route}.#{field}.#{[field].first}") end |
#parse_current_route ⇒ Arkaan::Monitoring::Route
Gets the current route in the database from the sinatra route.
88 89 90 91 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 88 def parse_current_route splitted = request.env['sinatra.route'].split(' ') return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first end |
#select_params(*fields) ⇒ Hash
Select parameters in the params hash, by its keys.
113 114 115 |
# File 'lib/arkaan/utils/controller_without_filter.rb', line 113 def select_params(*fields) return params.select { |key, value| fields.include?(key) } end |