arkaan 0.10.3 → 0.10.4
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 -3
- data/lib/arkaan/utils/controller.rb +1 -131
- data/lib/arkaan/utils/controller_without_filter.rb +138 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87ef1ed2c56986510788857b6985687a48969daf
|
4
|
+
data.tar.gz: 56877508868545d823c8711c8b9c14acb9b9e00a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90e08e088e3395e18278a7081bfc7dce953e7d040fd2f3378d349397ec5526038d3941b7c28c9ed1bc70d6eb082a5477a51ed7593de2e38d61028e9496f45b30
|
7
|
+
data.tar.gz: 1e95b66e78c16d30c45b384478ab189f457616db9ba98c63280d27b1add9ca9153901f35b689712bcee73b21c1d5590e11dd10bb8eb06e8cbfba9cccc0c64a8e
|
data/lib/arkaan/utils.rb
CHANGED
@@ -2,8 +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 :Controller
|
6
|
-
autoload :
|
7
|
-
autoload :
|
5
|
+
autoload :Controller , 'arkaan/utils/controller'
|
6
|
+
autoload :ControllerWithoutFilter, 'arkaan/utils/controller_without_filter'
|
7
|
+
autoload :Errors , 'arkaan/utils/errors'
|
8
|
+
autoload :MicroService , 'arkaan/utils/micro_service'
|
8
9
|
end
|
9
10
|
end
|
@@ -2,8 +2,7 @@ module Arkaan
|
|
2
2
|
module Utils
|
3
3
|
# Base controller to handle the standard error when accessing the API.
|
4
4
|
# @author Vincent Courtois <courtois.vincenet@outlook.com>
|
5
|
-
class Controller <
|
6
|
-
register Sinatra::ConfigFile
|
5
|
+
class Controller < Arkaan::Utils::ControllerWithoutFilter
|
7
6
|
|
8
7
|
before do
|
9
8
|
add_body_to_params
|
@@ -18,135 +17,6 @@ module Arkaan
|
|
18
17
|
custom_error(404, 'common.app_key.unknown')
|
19
18
|
end
|
20
19
|
end
|
21
|
-
|
22
|
-
# Creates a 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_route(verb, path, &block)
|
26
|
-
self.declare_route_with(verb, path, false, &block)
|
27
|
-
end
|
28
|
-
|
29
|
-
# Creates a non premium 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
|
-
def self.declare_premium_route(verb, path, &block)
|
33
|
-
self.declare_route_with(verb, path, true, &block)
|
34
|
-
end
|
35
|
-
|
36
|
-
# Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
37
|
-
# @param verb [String] the HTTP method used to create this route.
|
38
|
-
# @param path [String] the path, beginning with a /, of the route to create.
|
39
|
-
# @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
|
40
|
-
def self.declare_route_with(verb, path, premium, &block)
|
41
|
-
service = Arkaan::Utils::MicroService.instance.service
|
42
|
-
unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
|
43
|
-
Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
|
44
|
-
end
|
45
|
-
if premium
|
46
|
-
self.public_send(verb, path) do
|
47
|
-
@sinatra_route = parse_current_route
|
48
|
-
if !@application.premium?
|
49
|
-
custom_error(403, 'common.app_key.forbidden')
|
50
|
-
end
|
51
|
-
instance_eval(&block)
|
52
|
-
end
|
53
|
-
else
|
54
|
-
self.public_send(verb, path, &block)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# Loads the errors configuration file from the config folder.
|
59
|
-
# @param file [String] send __FILE__
|
60
|
-
def self.load_errors_from(file)
|
61
|
-
config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
|
62
|
-
end
|
63
|
-
|
64
|
-
# Checks the presence of several fields given as parameters and halts the execution if it's not present.
|
65
|
-
# @param fields [Array<String>] an array of fields names to search in the parameters
|
66
|
-
def check_presence(*fields, route:)
|
67
|
-
fields.each do |field|
|
68
|
-
custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == ''
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# Checks if the session ID is given in the parameters and if the session exists.
|
73
|
-
# @param action [String] the action used to get the errors from the errors file.
|
74
|
-
# @return [Arkaan::Authentication::Session] the session when it exists.
|
75
|
-
def check_session(action)
|
76
|
-
check_presence('session_id', route: action)
|
77
|
-
session = Arkaan::Authentication::Session.where(token: params['session_id']).first
|
78
|
-
if session.nil?
|
79
|
-
custom_error(404, "#{action}.session_id.unknown")
|
80
|
-
end
|
81
|
-
return session
|
82
|
-
end
|
83
|
-
|
84
|
-
def check_application(action)
|
85
|
-
check_presence('app_key', route: action)
|
86
|
-
application = Arkaan::OAuth::Appliation.where(keu: params['app_key']).first
|
87
|
-
if application.nil?
|
88
|
-
custom_error(404, "#{action}.app_key.unknown")
|
89
|
-
end
|
90
|
-
return application
|
91
|
-
end
|
92
|
-
|
93
|
-
# Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
|
94
|
-
# of the SON body if they have similar keys.
|
95
|
-
def add_body_to_params
|
96
|
-
parsed_body = JSON.parse(request.body.read.to_s) rescue {}
|
97
|
-
parsed_body.keys.each do |key|
|
98
|
-
params[key] = parsed_body[key]
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# Gets the current route in the database from the sinatra route.
|
103
|
-
# @return [Arkaan::Monitoring::Route] the route declared in the services registry.
|
104
|
-
def parse_current_route
|
105
|
-
splitted = request.env['sinatra.route'].split(' ')
|
106
|
-
return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
|
107
|
-
end
|
108
|
-
|
109
|
-
# Halts the application and creates the returned body from the parameters and the errors config file.
|
110
|
-
# @param status [Integer] the HTTP status to halt the application with.
|
111
|
-
# @param path [String] the path in the configuration file to access the URL.
|
112
|
-
def custom_error(status, path)
|
113
|
-
route, field, error = path.split('.')
|
114
|
-
halt status, {status: status, field: field, error: error, docs: settings.errors[route][field][error]}.to_json
|
115
|
-
end
|
116
|
-
|
117
|
-
# Halts the application with a Bad Request error affecting a field of a model.
|
118
|
-
# @param instance [Mongoid::Document] the document having a field in error.
|
119
|
-
# @param route [String] the type of action you're currently doing (e.g: 'creation')
|
120
|
-
def model_error(instance, route)
|
121
|
-
messages = instance.errors.messages
|
122
|
-
field = messages.keys.first
|
123
|
-
custom_error(400, "#{route}.#{field}.#{messages[field].first}")
|
124
|
-
end
|
125
|
-
|
126
|
-
# Select parameters in the params hash, by its keys.
|
127
|
-
# @param fields [Array<String>] the keys to select in the params hash.
|
128
|
-
# @return [Hash] the selected chunk of the params hash.
|
129
|
-
def select_params(*fields)
|
130
|
-
return params.select { |key, value| fields.include?(key) }
|
131
|
-
end
|
132
|
-
|
133
|
-
# Creates a custom error from an existing Arkaan exception class.
|
134
|
-
# @param exception {StandardError} the exception to transform in a usable error.
|
135
|
-
def handle_arkaan_exception(exception)
|
136
|
-
custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}")
|
137
|
-
end
|
138
|
-
|
139
|
-
error Arkaan::Utils::Errors::BadRequest do |exception|
|
140
|
-
handle_arkaan_exception(exception)
|
141
|
-
end
|
142
|
-
|
143
|
-
error Arkaan::Utils::Errors::Forbidden do |exception|
|
144
|
-
handle_arkaan_exception(exception)
|
145
|
-
end
|
146
|
-
|
147
|
-
error Arkaan::Utils::Errors::NotFound do |exception|
|
148
|
-
handle_arkaan_exception(exception)
|
149
|
-
end
|
150
20
|
end
|
151
21
|
end
|
152
22
|
end
|
@@ -0,0 +1,138 @@
|
|
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
|
+
|
8
|
+
# Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
|
9
|
+
# @param verb [String] the HTTP method used to create this route.
|
10
|
+
# @param path [String] the path, beginning with a /, of the route to create.
|
11
|
+
def self.declare_route(verb, path, &block)
|
12
|
+
self.declare_route_with(verb, path, false, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Creates a non 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_premium_route(verb, path, &block)
|
19
|
+
self.declare_route_with(verb, path, true, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Creates a 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
|
+
# @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
|
26
|
+
def self.declare_route_with(verb, path, premium, &block)
|
27
|
+
service = Arkaan::Utils::MicroService.instance.service
|
28
|
+
unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
|
29
|
+
Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
|
30
|
+
end
|
31
|
+
if premium
|
32
|
+
self.public_send(verb, path) do
|
33
|
+
@sinatra_route = parse_current_route
|
34
|
+
if !@application.premium?
|
35
|
+
custom_error(403, 'common.app_key.forbidden')
|
36
|
+
end
|
37
|
+
instance_eval(&block)
|
38
|
+
end
|
39
|
+
else
|
40
|
+
self.public_send(verb, path, &block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Loads the errors configuration file from the config folder.
|
45
|
+
# @param file [String] send __FILE__
|
46
|
+
def self.load_errors_from(file)
|
47
|
+
config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
|
48
|
+
end
|
49
|
+
|
50
|
+
# Checks the presence of several fields given as parameters and halts the execution if it's not present.
|
51
|
+
# @param fields [Array<String>] an array of fields names to search in the parameters
|
52
|
+
def check_presence(*fields, route:)
|
53
|
+
fields.each do |field|
|
54
|
+
custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == ''
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Checks if the session ID is given in the parameters and if the session exists.
|
59
|
+
# @param action [String] the action used to get the errors from the errors file.
|
60
|
+
# @return [Arkaan::Authentication::Session] the session when it exists.
|
61
|
+
def check_session(action)
|
62
|
+
check_presence('session_id', route: action)
|
63
|
+
session = Arkaan::Authentication::Session.where(token: params['session_id']).first
|
64
|
+
if session.nil?
|
65
|
+
custom_error(404, "#{action}.session_id.unknown")
|
66
|
+
end
|
67
|
+
return session
|
68
|
+
end
|
69
|
+
|
70
|
+
def check_application(action)
|
71
|
+
check_presence('app_key', route: action)
|
72
|
+
application = Arkaan::OAuth::Appliation.where(keu: params['app_key']).first
|
73
|
+
if application.nil?
|
74
|
+
custom_error(404, "#{action}.app_key.unknown")
|
75
|
+
end
|
76
|
+
return application
|
77
|
+
end
|
78
|
+
|
79
|
+
# Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
|
80
|
+
# of the SON body if they have similar keys.
|
81
|
+
def add_body_to_params
|
82
|
+
parsed_body = JSON.parse(request.body.read.to_s) rescue {}
|
83
|
+
parsed_body.keys.each do |key|
|
84
|
+
params[key] = parsed_body[key]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Gets the current route in the database from the sinatra route.
|
89
|
+
# @return [Arkaan::Monitoring::Route] the route declared in the services registry.
|
90
|
+
def parse_current_route
|
91
|
+
splitted = request.env['sinatra.route'].split(' ')
|
92
|
+
return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
|
93
|
+
end
|
94
|
+
|
95
|
+
# Halts the application and creates the returned body from the parameters and the errors config file.
|
96
|
+
# @param status [Integer] the HTTP status to halt the application with.
|
97
|
+
# @param path [String] the path in the configuration file to access the URL.
|
98
|
+
def custom_error(status, path)
|
99
|
+
route, field, error = path.split('.')
|
100
|
+
halt status, {status: status, field: field, error: error, docs: settings.errors[route][field][error]}.to_json
|
101
|
+
end
|
102
|
+
|
103
|
+
# Halts the application with a Bad Request error affecting a field of a model.
|
104
|
+
# @param instance [Mongoid::Document] the document having a field in error.
|
105
|
+
# @param route [String] the type of action you're currently doing (e.g: 'creation')
|
106
|
+
def model_error(instance, route)
|
107
|
+
messages = instance.errors.messages
|
108
|
+
field = messages.keys.first
|
109
|
+
custom_error(400, "#{route}.#{field}.#{messages[field].first}")
|
110
|
+
end
|
111
|
+
|
112
|
+
# Select parameters in the params hash, by its keys.
|
113
|
+
# @param fields [Array<String>] the keys to select in the params hash.
|
114
|
+
# @return [Hash] the selected chunk of the params hash.
|
115
|
+
def select_params(*fields)
|
116
|
+
return params.select { |key, value| fields.include?(key) }
|
117
|
+
end
|
118
|
+
|
119
|
+
# Creates a custom error from an existing Arkaan exception class.
|
120
|
+
# @param exception {StandardError} the exception to transform in a usable error.
|
121
|
+
def handle_arkaan_exception(exception)
|
122
|
+
custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}")
|
123
|
+
end
|
124
|
+
|
125
|
+
error Arkaan::Utils::Errors::BadRequest do |exception|
|
126
|
+
handle_arkaan_exception(exception)
|
127
|
+
end
|
128
|
+
|
129
|
+
error Arkaan::Utils::Errors::Forbidden do |exception|
|
130
|
+
handle_arkaan_exception(exception)
|
131
|
+
end
|
132
|
+
|
133
|
+
error Arkaan::Utils::Errors::NotFound do |exception|
|
134
|
+
handle_arkaan_exception(exception)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
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: 0.10.
|
4
|
+
version: 0.10.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vincent Courtois
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -245,6 +245,7 @@ files:
|
|
245
245
|
- lib/arkaan/specs.rb
|
246
246
|
- lib/arkaan/utils.rb
|
247
247
|
- lib/arkaan/utils/controller.rb
|
248
|
+
- lib/arkaan/utils/controller_without_filter.rb
|
248
249
|
- lib/arkaan/utils/errors.rb
|
249
250
|
- lib/arkaan/utils/errors/bad_request.rb
|
250
251
|
- lib/arkaan/utils/errors/forbidden.rb
|