arkaan 1.4.7 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/arkaan/utils.rb +4 -4
- data/lib/arkaan/utils/controllers.rb +8 -0
- data/lib/arkaan/utils/controllers/base.rb +193 -0
- data/lib/arkaan/utils/controllers/checked.rb +14 -0
- data/lib/arkaan/utils/loaders.rb +7 -0
- data/lib/arkaan/utils/loaders/heroku.rb +20 -0
- data/lib/arkaan/utils/micro_service.rb +7 -12
- data/lib/arkaan/version.rb +3 -0
- metadata +8 -4
- data/lib/arkaan/utils/controller.rb +0 -12
- data/lib/arkaan/utils/controller_without_filter.rb +0 -191
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c39aa671150c915b8a1b26d7bc4ec8101eaa3128
|
|
4
|
+
data.tar.gz: 47aecda9bdc9b5a2973687d8a732c4fe029522d5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a7585c99ec2d678ce02a022b494793b6d28a929ac903121c90fe91ea4459079140cce09991e546c7ab52a2c0ad8e6860ecf2a1c875a0e7327ae8c3bab924f284
|
|
7
|
+
data.tar.gz: 94defbac662fe38f06ed30a557f7ee4dd50f1a5021989ba52ebfd7338d7fbc01c634e7407473389188b85f97de4c2392bbe3bd376d677b0004cef65c22a72514
|
data/lib/arkaan/utils.rb
CHANGED
|
@@ -2,9 +2,9 @@ module Arkaan
|
|
|
2
2
|
# Utility classes for the different micro-services of the suite.
|
|
3
3
|
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
|
4
4
|
module Utils
|
|
5
|
-
autoload :
|
|
6
|
-
autoload :
|
|
7
|
-
autoload :
|
|
8
|
-
autoload :MicroService
|
|
5
|
+
autoload :Controllers , 'arkaan/utils/controllers'
|
|
6
|
+
autoload :Errors , 'arkaan/utils/errors'
|
|
7
|
+
autoload :Loaders , 'arkaan/utils/loaders'
|
|
8
|
+
autoload :MicroService, 'arkaan/utils/micro_service'
|
|
9
9
|
end
|
|
10
10
|
end
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
module Arkaan
|
|
2
|
+
module Utils
|
|
3
|
+
module Controllers
|
|
4
|
+
# Base controller to handle the standard error when accessing the API.
|
|
5
|
+
# @author Vincent Courtois <courtois.vincenet@outlook.com>
|
|
6
|
+
class Base < Sinatra::Base
|
|
7
|
+
register Sinatra::ConfigFile
|
|
8
|
+
helpers Sinatra::CustomLogger
|
|
9
|
+
|
|
10
|
+
configure do
|
|
11
|
+
set :logger, Logger.new(STDOUT)
|
|
12
|
+
logger.level = Logger::ERROR if ENV['RACK_ENV'] == 'test'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
16
|
+
# @param verb [String] the HTTP method used to create this route.
|
|
17
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
|
18
|
+
def self.declare_route(verb, path, options: {}, &block)
|
|
19
|
+
self.declare_route_with(verb, path, false, options, &block)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
23
|
+
# @param verb [String] the HTTP method used to create this route.
|
|
24
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
|
25
|
+
def self.declare_premium_route(verb, path, options: {}, &block)
|
|
26
|
+
self.declare_route_with(verb, path, true, options, &block)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
30
|
+
# @param verb [String] the HTTP method used to create this route.
|
|
31
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
|
32
|
+
# @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
|
|
33
|
+
def self.declare_route_with(verb, path, premium, options, &block)
|
|
34
|
+
service = Arkaan::Utils::MicroService.instance.service
|
|
35
|
+
complete_path = "#{Arkaan::Utils::MicroService.instance.path}#{path == '/' ? '' : path}"
|
|
36
|
+
|
|
37
|
+
unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
|
|
38
|
+
route = Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
|
|
39
|
+
if !options.nil? && !options[:authenticated].nil?
|
|
40
|
+
route.update_attribute(:authenticated, false)
|
|
41
|
+
end
|
|
42
|
+
Arkaan::Permissions::Group.where(is_superuser: true).each do |group|
|
|
43
|
+
group.routes << route
|
|
44
|
+
group.save!
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
if premium
|
|
48
|
+
self.public_send(verb, complete_path) do
|
|
49
|
+
@sinatra_route = parse_current_route
|
|
50
|
+
if !@application.premium?
|
|
51
|
+
custom_error(403, 'common.app_key.forbidden')
|
|
52
|
+
end
|
|
53
|
+
instance_eval(&block)
|
|
54
|
+
end
|
|
55
|
+
else
|
|
56
|
+
logger.info "#{verb} #{complete_path}"
|
|
57
|
+
self.public_send(verb, complete_path, &block)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Loads the errors configuration file from the config folder.
|
|
62
|
+
# @param file [String] send __FILE__
|
|
63
|
+
def self.load_errors_from(file)
|
|
64
|
+
config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def before_checks
|
|
68
|
+
add_body_to_params
|
|
69
|
+
|
|
70
|
+
check_presence('token', 'app_key', route: 'common')
|
|
71
|
+
|
|
72
|
+
gateway = Arkaan::Monitoring::Gateway.where(token: params['token']).first
|
|
73
|
+
@application = Arkaan::OAuth::Application.where(key: params['app_key']).first
|
|
74
|
+
|
|
75
|
+
if gateway.nil?
|
|
76
|
+
custom_error(404, 'common.token.unknown')
|
|
77
|
+
elsif @application.nil?
|
|
78
|
+
custom_error(404, 'common.app_key.unknown')
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Checks the presence of several fields given as parameters and halts the execution if it's not present.
|
|
83
|
+
# @param fields [Array<String>] an array of fields names to search in the parameters
|
|
84
|
+
# @param route [String] the name of the route you're requiring to put in the error message.
|
|
85
|
+
def check_presence(*fields, route:)
|
|
86
|
+
fields.each do |field|
|
|
87
|
+
custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == ''
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Checks the presence of either fields given in parameters. It halts with an error only if ALL parameters are not given.
|
|
92
|
+
# @param fields [Array<String>] an array of fields names to search in the parameters
|
|
93
|
+
# @param route [String] the name of the route you're requiring to put in the error message.
|
|
94
|
+
# @param key [String] the key to search in the errors configuration file.
|
|
95
|
+
def check_either_presence(*fields, route:, key:)
|
|
96
|
+
fields.each do |field|
|
|
97
|
+
return true if !params[field].nil? && params[field] != ''
|
|
98
|
+
end
|
|
99
|
+
custom_error 400, "#{route}.#{key}.required"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Checks if the session ID is given in the parameters and if the session exists.
|
|
103
|
+
# @param action [String] the action used to get the errors from the errors file.
|
|
104
|
+
# @return [Arkaan::Authentication::Session] the session when it exists.
|
|
105
|
+
def check_session(action)
|
|
106
|
+
check_presence('session_id', route: action)
|
|
107
|
+
session = Arkaan::Authentication::Session.where(token: params['session_id']).first
|
|
108
|
+
if session.nil?
|
|
109
|
+
custom_error(404, "#{action}.session_id.unknown")
|
|
110
|
+
end
|
|
111
|
+
return session
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def check_application(action)
|
|
115
|
+
check_presence('app_key', route: action)
|
|
116
|
+
application = Arkaan::OAuth::Application.where(key: params['app_key']).first
|
|
117
|
+
custom_error(404, "#{action}.app_key.unknown") if application.nil?
|
|
118
|
+
return application
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
|
|
122
|
+
# of the SON body if they have similar keys.
|
|
123
|
+
def add_body_to_params
|
|
124
|
+
if request.body.respond_to?(:rewind) && request.body.respond_to?(:read)
|
|
125
|
+
request.body.rewind
|
|
126
|
+
parsed_body = JSON.parse(request.body.read.to_s) rescue {}
|
|
127
|
+
parsed_body.keys.each do |key|
|
|
128
|
+
params[key] = parsed_body[key]
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Gets the current route in the database from the sinatra route.
|
|
134
|
+
# @return [Arkaan::Monitoring::Route] the route declared in the services registry.
|
|
135
|
+
def parse_current_route
|
|
136
|
+
splitted = request.env['sinatra.route'].split(' ')
|
|
137
|
+
return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Halts the application and creates the returned body from the parameters and the errors config file.
|
|
141
|
+
# @param status [Integer] the HTTP status to halt the application with.
|
|
142
|
+
# @param path [String] the path in the configuration file to access the URL.
|
|
143
|
+
def custom_error(status, path)
|
|
144
|
+
route, field, error = path.split('.')
|
|
145
|
+
docs = settings.errors[route][field][error] rescue ''
|
|
146
|
+
halt status, {status: status, field: field, error: error, docs: docs}.to_json
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Halts the application with a Bad Request error affecting a field of a model.
|
|
150
|
+
# @param instance [Mongoid::Document] the document having a field in error.
|
|
151
|
+
# @param route [String] the type of action you're currently doing (e.g: 'creation')
|
|
152
|
+
def model_error(instance, route)
|
|
153
|
+
messages = instance.errors.messages
|
|
154
|
+
field = messages.keys.first
|
|
155
|
+
custom_error(400, "#{route}.#{field}.#{messages[field].first}")
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Select parameters in the params hash, by its keys.
|
|
159
|
+
# @param fields [Array<String>] the keys to select in the params hash.
|
|
160
|
+
# @return [Hash] the selected chunk of the params hash.
|
|
161
|
+
def select_params(*fields)
|
|
162
|
+
return params.select { |key, value| fields.include?(key) }
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Creates a custom error from an existing Arkaan exception class.
|
|
166
|
+
# @param exception {StandardError} the exception to transform in a usable error.
|
|
167
|
+
def handle_arkaan_exception(exception)
|
|
168
|
+
custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}")
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
error Arkaan::Utils::Errors::BadRequest do |exception|
|
|
172
|
+
handle_arkaan_exception(exception)
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
error Arkaan::Utils::Errors::Forbidden do |exception|
|
|
176
|
+
handle_arkaan_exception(exception)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
error Arkaan::Utils::Errors::NotFound do |exception|
|
|
180
|
+
handle_arkaan_exception(exception)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
error Arkaan::Factories::Errors::GatewayNotFound do |exception|
|
|
184
|
+
handle_arkaan_exception(exception)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
error StandardError do |exception|
|
|
188
|
+
custom_error(500, 'system_error.unknown_field.unknown_error')
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Arkaan
|
|
2
|
+
module Utils
|
|
3
|
+
module Controllers
|
|
4
|
+
# Base controller to handle the standard error when accessing the API.
|
|
5
|
+
# @author Vincent Courtois <courtois.vincenet@outlook.com>
|
|
6
|
+
class Checked < Arkaan::Utils::Controllers::Base
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
before_checks
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Arkaan
|
|
2
|
+
module Utils
|
|
3
|
+
module Plugins
|
|
4
|
+
class Heroku
|
|
5
|
+
# Loads the heroku informations inside the data of the instance.
|
|
6
|
+
# @param instance [Arkaan::Monitoring::Instance] the instance to put the enrichment inside.
|
|
7
|
+
def self.load!(instance)
|
|
8
|
+
if !ENV['OAUTH_TOKEN'].nil? && instance != false && instance.persisted?
|
|
9
|
+
heroku = PlatformAPI.connect_oauth(ENV['OAUTH_TOKEN'])
|
|
10
|
+
regex = /\Ahttps?:\/\/([a-z\-]+).herokuapp.com\/?\z/
|
|
11
|
+
if instance.url.match(regex)
|
|
12
|
+
app_name = instance.url.scan(regex).first.first
|
|
13
|
+
instance.update_attribute(:data, heroku.app.info(app_name))
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -108,10 +108,10 @@ module Arkaan
|
|
|
108
108
|
# @return [Arkaan::Monitoring::Instance] the instance of the micro service currently running.
|
|
109
109
|
def register_instance
|
|
110
110
|
@instance = @service.instances.where(url: ENV['SERVICE_URL']).first
|
|
111
|
-
if instance.nil?
|
|
111
|
+
if @instance.nil?
|
|
112
112
|
@instance = Arkaan::Monitoring::Instance.create(service: @service, url: ENV['SERVICE_URL'], type: type)
|
|
113
113
|
end
|
|
114
|
-
instance.update_attribute(:running, true)
|
|
114
|
+
@instance.update_attribute(:running, true)
|
|
115
115
|
return @instance
|
|
116
116
|
end
|
|
117
117
|
|
|
@@ -128,7 +128,7 @@ module Arkaan
|
|
|
128
128
|
@service.update_attribute(:test_mode, true)
|
|
129
129
|
end
|
|
130
130
|
register_instance
|
|
131
|
-
|
|
131
|
+
load_plugin!
|
|
132
132
|
if service
|
|
133
133
|
load_standard_files
|
|
134
134
|
load_test_files if test_mode
|
|
@@ -137,16 +137,11 @@ module Arkaan
|
|
|
137
137
|
return self
|
|
138
138
|
end
|
|
139
139
|
|
|
140
|
-
def
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if instance.url.match(regex)
|
|
145
|
-
app_name = instance.url.scan(regex).first.first
|
|
146
|
-
instance.update_attribute(:data, heroku.app.info(app_name))
|
|
147
|
-
end
|
|
140
|
+
def load_plugin!
|
|
141
|
+
plugin = ENV['ADDITIONAL_PLUGIN']
|
|
142
|
+
if !plugin.nil? && Arkaan::Utils::Plugins.constants.include?(plugin)
|
|
143
|
+
Arkaan::Utils::Plugins.const_get(plugin).load!(@instance)
|
|
148
144
|
end
|
|
149
|
-
return self
|
|
150
145
|
end
|
|
151
146
|
|
|
152
147
|
def load_mongoid_configuration(test_mode: false)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: arkaan
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vincent Courtois
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-05
|
|
11
|
+
date: 2019-06-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|
|
@@ -325,15 +325,19 @@ files:
|
|
|
325
325
|
- lib/arkaan/rulesets/fields/integer.rb
|
|
326
326
|
- lib/arkaan/specs.rb
|
|
327
327
|
- lib/arkaan/utils.rb
|
|
328
|
-
- lib/arkaan/utils/
|
|
329
|
-
- lib/arkaan/utils/
|
|
328
|
+
- lib/arkaan/utils/controllers.rb
|
|
329
|
+
- lib/arkaan/utils/controllers/base.rb
|
|
330
|
+
- lib/arkaan/utils/controllers/checked.rb
|
|
330
331
|
- lib/arkaan/utils/errors.rb
|
|
331
332
|
- lib/arkaan/utils/errors/bad_request.rb
|
|
332
333
|
- lib/arkaan/utils/errors/forbidden.rb
|
|
333
334
|
- lib/arkaan/utils/errors/http_error.rb
|
|
334
335
|
- lib/arkaan/utils/errors/not_found.rb
|
|
336
|
+
- lib/arkaan/utils/loaders.rb
|
|
337
|
+
- lib/arkaan/utils/loaders/heroku.rb
|
|
335
338
|
- lib/arkaan/utils/micro_service.rb
|
|
336
339
|
- lib/arkaan/utils/seeder.rb
|
|
340
|
+
- lib/arkaan/version.rb
|
|
337
341
|
homepage: https://rubygems.org/gems/arkaan
|
|
338
342
|
licenses:
|
|
339
343
|
- MIT
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module Arkaan
|
|
2
|
-
module Utils
|
|
3
|
-
# Base controller to handle the standard error when accessing the API.
|
|
4
|
-
# @author Vincent Courtois <courtois.vincenet@outlook.com>
|
|
5
|
-
class Controller < Arkaan::Utils::ControllerWithoutFilter
|
|
6
|
-
|
|
7
|
-
before do
|
|
8
|
-
before_checks
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
end
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
module Arkaan
|
|
2
|
-
module Utils
|
|
3
|
-
# Base controller to handle the standard error when accessing the API.
|
|
4
|
-
# @author Vincent Courtois <courtois.vincenet@outlook.com>
|
|
5
|
-
class ControllerWithoutFilter < Sinatra::Base
|
|
6
|
-
register Sinatra::ConfigFile
|
|
7
|
-
helpers Sinatra::CustomLogger
|
|
8
|
-
|
|
9
|
-
configure do
|
|
10
|
-
set :logger, Logger.new(STDOUT)
|
|
11
|
-
logger.level = Logger::ERROR if ENV['RACK_ENV'] == 'test'
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
# Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
15
|
-
# @param verb [String] the HTTP method used to create this route.
|
|
16
|
-
# @param path [String] the path, beginning with a /, of the route to create.
|
|
17
|
-
def self.declare_route(verb, path, options: {}, &block)
|
|
18
|
-
self.declare_route_with(verb, path, false, options, &block)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
# Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
22
|
-
# @param verb [String] the HTTP method used to create this route.
|
|
23
|
-
# @param path [String] the path, beginning with a /, of the route to create.
|
|
24
|
-
def self.declare_premium_route(verb, path, options: {}, &block)
|
|
25
|
-
self.declare_route_with(verb, path, true, options, &block)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
|
29
|
-
# @param verb [String] the HTTP method used to create this route.
|
|
30
|
-
# @param path [String] the path, beginning with a /, of the route to create.
|
|
31
|
-
# @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
|
|
32
|
-
def self.declare_route_with(verb, path, premium, options, &block)
|
|
33
|
-
service = Arkaan::Utils::MicroService.instance.service
|
|
34
|
-
complete_path = "#{Arkaan::Utils::MicroService.instance.path}#{path == '/' ? '' : path}"
|
|
35
|
-
|
|
36
|
-
unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
|
|
37
|
-
route = Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
|
|
38
|
-
if !options.nil? && !options[:authenticated].nil?
|
|
39
|
-
route.update_attribute(:authenticated, false)
|
|
40
|
-
end
|
|
41
|
-
Arkaan::Permissions::Group.where(is_superuser: true).each do |group|
|
|
42
|
-
group.routes << route
|
|
43
|
-
group.save!
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
if premium
|
|
47
|
-
self.public_send(verb, complete_path) do
|
|
48
|
-
@sinatra_route = parse_current_route
|
|
49
|
-
if !@application.premium?
|
|
50
|
-
custom_error(403, 'common.app_key.forbidden')
|
|
51
|
-
end
|
|
52
|
-
instance_eval(&block)
|
|
53
|
-
end
|
|
54
|
-
else
|
|
55
|
-
logger.info "#{verb} #{complete_path}"
|
|
56
|
-
self.public_send(verb, complete_path, &block)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Loads the errors configuration file from the config folder.
|
|
61
|
-
# @param file [String] send __FILE__
|
|
62
|
-
def self.load_errors_from(file)
|
|
63
|
-
config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def before_checks
|
|
67
|
-
add_body_to_params
|
|
68
|
-
|
|
69
|
-
check_presence('token', 'app_key', route: 'common')
|
|
70
|
-
|
|
71
|
-
gateway = Arkaan::Monitoring::Gateway.where(token: params['token']).first
|
|
72
|
-
@application = Arkaan::OAuth::Application.where(key: params['app_key']).first
|
|
73
|
-
|
|
74
|
-
if gateway.nil?
|
|
75
|
-
custom_error(404, 'common.token.unknown')
|
|
76
|
-
elsif @application.nil?
|
|
77
|
-
custom_error(404, 'common.app_key.unknown')
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# Checks the presence of several fields given as parameters and halts the execution if it's not present.
|
|
82
|
-
# @param fields [Array<String>] an array of fields names to search in the parameters
|
|
83
|
-
# @param route [String] the name of the route you're requiring to put in the error message.
|
|
84
|
-
def check_presence(*fields, route:)
|
|
85
|
-
fields.each do |field|
|
|
86
|
-
custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == ''
|
|
87
|
-
end
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# Checks the presence of either fields given in parameters. It halts with an error only if ALL parameters are not given.
|
|
91
|
-
# @param fields [Array<String>] an array of fields names to search in the parameters
|
|
92
|
-
# @param route [String] the name of the route you're requiring to put in the error message.
|
|
93
|
-
# @param key [String] the key to search in the errors configuration file.
|
|
94
|
-
def check_either_presence(*fields, route:, key:)
|
|
95
|
-
fields.each do |field|
|
|
96
|
-
return true if !params[field].nil? && params[field] != ''
|
|
97
|
-
end
|
|
98
|
-
custom_error 400, "#{route}.#{key}.required"
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# Checks if the session ID is given in the parameters and if the session exists.
|
|
102
|
-
# @param action [String] the action used to get the errors from the errors file.
|
|
103
|
-
# @return [Arkaan::Authentication::Session] the session when it exists.
|
|
104
|
-
def check_session(action)
|
|
105
|
-
check_presence('session_id', route: action)
|
|
106
|
-
session = Arkaan::Authentication::Session.where(token: params['session_id']).first
|
|
107
|
-
if session.nil?
|
|
108
|
-
custom_error(404, "#{action}.session_id.unknown")
|
|
109
|
-
end
|
|
110
|
-
return session
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def check_application(action)
|
|
114
|
-
check_presence('app_key', route: action)
|
|
115
|
-
application = Arkaan::OAuth::Application.where(key: params['app_key']).first
|
|
116
|
-
custom_error(404, "#{action}.app_key.unknown") if application.nil?
|
|
117
|
-
return application
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
# Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
|
|
121
|
-
# of the SON body if they have similar keys.
|
|
122
|
-
def add_body_to_params
|
|
123
|
-
if request.body.respond_to?(:rewind) && request.body.respond_to?(:read)
|
|
124
|
-
request.body.rewind
|
|
125
|
-
parsed_body = JSON.parse(request.body.read.to_s) rescue {}
|
|
126
|
-
parsed_body.keys.each do |key|
|
|
127
|
-
params[key] = parsed_body[key]
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
# Gets the current route in the database from the sinatra route.
|
|
133
|
-
# @return [Arkaan::Monitoring::Route] the route declared in the services registry.
|
|
134
|
-
def parse_current_route
|
|
135
|
-
splitted = request.env['sinatra.route'].split(' ')
|
|
136
|
-
return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
# Halts the application and creates the returned body from the parameters and the errors config file.
|
|
140
|
-
# @param status [Integer] the HTTP status to halt the application with.
|
|
141
|
-
# @param path [String] the path in the configuration file to access the URL.
|
|
142
|
-
def custom_error(status, path)
|
|
143
|
-
route, field, error = path.split('.')
|
|
144
|
-
docs = settings.errors[route][field][error] rescue ''
|
|
145
|
-
halt status, {status: status, field: field, error: error, docs: docs}.to_json
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
# Halts the application with a Bad Request error affecting a field of a model.
|
|
149
|
-
# @param instance [Mongoid::Document] the document having a field in error.
|
|
150
|
-
# @param route [String] the type of action you're currently doing (e.g: 'creation')
|
|
151
|
-
def model_error(instance, route)
|
|
152
|
-
messages = instance.errors.messages
|
|
153
|
-
field = messages.keys.first
|
|
154
|
-
custom_error(400, "#{route}.#{field}.#{messages[field].first}")
|
|
155
|
-
end
|
|
156
|
-
|
|
157
|
-
# Select parameters in the params hash, by its keys.
|
|
158
|
-
# @param fields [Array<String>] the keys to select in the params hash.
|
|
159
|
-
# @return [Hash] the selected chunk of the params hash.
|
|
160
|
-
def select_params(*fields)
|
|
161
|
-
return params.select { |key, value| fields.include?(key) }
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
# Creates a custom error from an existing Arkaan exception class.
|
|
165
|
-
# @param exception {StandardError} the exception to transform in a usable error.
|
|
166
|
-
def handle_arkaan_exception(exception)
|
|
167
|
-
custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}")
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
error Arkaan::Utils::Errors::BadRequest do |exception|
|
|
171
|
-
handle_arkaan_exception(exception)
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
error Arkaan::Utils::Errors::Forbidden do |exception|
|
|
175
|
-
handle_arkaan_exception(exception)
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
error Arkaan::Utils::Errors::NotFound do |exception|
|
|
179
|
-
handle_arkaan_exception(exception)
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
error Arkaan::Factories::Errors::GatewayNotFound do |exception|
|
|
183
|
-
handle_arkaan_exception(exception)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
error StandardError do |exception|
|
|
187
|
-
custom_error(500, 'system_error.unknown_field.unknown_error')
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
end
|