arkaan 1.10.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3f79b8da492a5bfaf83581bd87a361d6037294f185ce6c2f2287f1d2a65b0e7
4
- data.tar.gz: fc6f180aff359447096bbda6b5b7014c0b87de9f448d536024eac852a8581954
3
+ metadata.gz: edf2d8ccd56f2083589f47086566a40400b98295249d3b2cefe0dc980584cb86
4
+ data.tar.gz: 1c27bc8b6fae7cec149f26f89ec9265892ff29d09e51a4b346cc57e2b171e6f1
5
5
  SHA512:
6
- metadata.gz: 6dfb33ab03f87d8a8b8dadffa1c3ed4111924eb23c3cfb97863a3cfa104cae4f98ff4167b3c2aef0d88456db78d04ea13dad69c3e046aadf3e04259e3c4ec2ee
7
- data.tar.gz: 99db55a4ae62a79d4c5aaab2f3922b30db0455aafa3083255ebd6be60f912c473c8d1445183b8d803b6bd4fba0ce7f501b3dfb5e5f0c77c69fc7c1df84db104c
6
+ metadata.gz: 063cde9a9b391fcea46b43829e251c25649f98e063fa8344294b15168b5d0c90b63248e4102d503e19a6624cd082023a5ed279d95fb679c315eaeb4fed6525b2
7
+ data.tar.gz: ee49a9fda2912e8efc873207914aec9e61c575290d742012458d86dfa75ec50b1631a3a65a4fb87d262ecb6bcecdd405204bdb5798de0cd086a244493dd2f897
@@ -17,7 +17,6 @@ module Arkaan
17
17
  autoload :Campaign , 'arkaan/campaign'
18
18
  autoload :Campaigns , 'arkaan/campaigns'
19
19
  autoload :Concerns , 'arkaan/concerns'
20
- autoload :Decorators , 'arkaan/decorators'
21
20
  autoload :Factories , 'arkaan/factories'
22
21
  autoload :Monitoring , 'arkaan/monitoring'
23
22
  autoload :Notification , 'arkaan/notification'
@@ -25,5 +24,4 @@ module Arkaan
25
24
  autoload :Permissions , 'arkaan/permissions'
26
25
  autoload :Phone , 'arkaan/phone'
27
26
  autoload :Ruleset , 'arkaan/ruleset'
28
- autoload :Utils , 'arkaan/utils'
29
27
  end
@@ -61,9 +61,6 @@ module Arkaan
61
61
  # @return [Array<Arkaan::Monitoring::Websocket>] the websockets created by the owner of this account.
62
62
  has_many :websockets, class_name: 'Arkaan::Monitoring::Websocket', inverse_of: :creator
63
63
 
64
- # @!attribute [rw] phones
65
- # @return [Array<Arkaan::Phone>] the phone numbers given by the user.
66
- embeds_many :phones, class_name: 'Arkaan::Phone', inverse_of: :account
67
64
  # @!attribute [rw] notifications
68
65
  # @return [Array<Arkaan::Notification>] the notifications linked to this user.
69
66
  embeds_many :notifications, class_name: 'Arkaan::Notification', inverse_of: :account
@@ -1,3 +1,3 @@
1
1
  module Arkaan
2
- VERSION = '1.10.0'
2
+ VERSION = '2.0.0'
3
3
  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: 1.10.0
4
+ version: 2.0.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-11-22 00:00:00.000000000 Z
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -319,7 +319,6 @@ files:
319
319
  - lib/arkaan/concerns/premiumable.rb
320
320
  - lib/arkaan/concerns/sluggable.rb
321
321
  - lib/arkaan/concerns/typable.rb
322
- - lib/arkaan/decorators.rb
323
322
  - lib/arkaan/decorators/errors.rb
324
323
  - lib/arkaan/decorators/errors/env_variable_missing.rb
325
324
  - lib/arkaan/decorators/gateway.rb
@@ -348,22 +347,7 @@ files:
348
347
  - lib/arkaan/permissions/category.rb
349
348
  - lib/arkaan/permissions/group.rb
350
349
  - lib/arkaan/permissions/right.rb
351
- - lib/arkaan/phone.rb
352
350
  - lib/arkaan/ruleset.rb
353
- - lib/arkaan/specs.rb
354
- - lib/arkaan/utils.rb
355
- - lib/arkaan/utils/controllers.rb
356
- - lib/arkaan/utils/controllers/base.rb
357
- - lib/arkaan/utils/controllers/checked.rb
358
- - lib/arkaan/utils/errors.rb
359
- - lib/arkaan/utils/errors/bad_request.rb
360
- - lib/arkaan/utils/errors/forbidden.rb
361
- - lib/arkaan/utils/errors/http_error.rb
362
- - lib/arkaan/utils/errors/not_found.rb
363
- - lib/arkaan/utils/loaders.rb
364
- - lib/arkaan/utils/loaders/heroku.rb
365
- - lib/arkaan/utils/micro_service.rb
366
- - lib/arkaan/utils/seeder.rb
367
351
  - lib/arkaan/version.rb
368
352
  homepage: https://rubygems.org/gems/arkaan
369
353
  licenses:
@@ -1,8 +0,0 @@
1
- module Arkaan
2
- # Decorators are used to enrich the features of the model classes without making it too big.
3
- # @author Vincent Courtois <courtois.vincent@outlook.com>
4
- module Decorators
5
- autoload :Gateway, 'arkaan/decorators/gateway'
6
- autoload :Errors , 'arkaan/decorators/errors'
7
- end
8
- end
@@ -1,24 +0,0 @@
1
- module Arkaan
2
- # A phone number is given by a user so that the persons he has selected can have it to contact him.
3
- # @author Vincent Courtois <courtois.vincent@outlook.com>
4
- class Phone
5
- include Mongoid::Document
6
- include Mongoid::Timestamps
7
- include Arkaan::Concerns::Enumerable
8
-
9
- # @!attribute [rw] privacy
10
- # @return [Symbol] the level of privacy you want for this phone number.
11
- enum_field :privacy, [:players, :public, :private], default: :private
12
- # @!attribute [rw] number
13
- # @return [Integer] the phone number the user has given.
14
- field :number, type: String
15
-
16
- # @!attribute [rw] account
17
- # @return [Arkaan::Account] the account the phone number is associated to.
18
- embedded_in :account, class_name: 'Arkaan::Account', inverse_of: :phones
19
-
20
- validates :number,
21
- presence: {message: 'required'},
22
- format: {with: /\A[0-9\-\._\/]+\z/, message: 'pattern'}
23
- end
24
- end
@@ -1,89 +0,0 @@
1
- module Arkaan
2
- # This module holds all the logic for the specs tools for all micro services (shared examples and other things).
3
- # @author Vincent Courtois <courtois.vincent@outlook.com>
4
- module Specs
5
-
6
- # Includes all the shared examples you could need, describing the basic behaviour of a route.
7
- def self.include_shared_examples
8
- RSpec.shared_examples 'a route' do |_verb, _path|
9
- let(:verb) { _verb }
10
- let(:path) { _path }
11
-
12
- def do_request(parameters)
13
- public_send verb.to_sym, path, ['get', 'delete'].include?(verb) ? parameters : parameters.to_json
14
- end
15
-
16
- describe 'common errors' do
17
- describe 'bad request errors' do
18
- describe 'no token error' do
19
- before do
20
- do_request({app_key: 'test_key'})
21
- end
22
- it 'Raises a bad request (400) error when the parameters don\'t contain the token of the gateway' do
23
- expect(last_response.status).to be 400
24
- end
25
- it 'returns the correct response if the parameters do not contain a gateway token' do
26
- expect(JSON.parse(last_response.body)).to eq({
27
- 'status' => 400,
28
- 'field' => 'token',
29
- 'error' => 'required',
30
- 'docs' => 'https://github.com/jdr-tools/wiki/wiki/Common-errors#gateway-token-not-given'
31
- })
32
- end
33
- end
34
- describe 'no application key error' do
35
- before do
36
- do_request({token: 'test_token'})
37
- end
38
- it 'Raises a bad request (400) error when the parameters don\'t contain the application key' do
39
- expect(last_response.status).to be 400
40
- end
41
- it 'returns the correct response if the parameters do not contain a application key' do
42
- expect(JSON.parse(last_response.body)).to eq({
43
- 'status' => 400,
44
- 'field' => 'app_key',
45
- 'error' => 'required',
46
- 'docs' => 'https://github.com/jdr-tools/wiki/wiki/Common-errors#application-key-not-given'
47
- })
48
- end
49
- end
50
- end
51
- describe 'not_found errors' do
52
- describe 'application not found' do
53
- before do
54
- do_request({token: 'test_token', app_key: 'another_key'})
55
- end
56
- it 'Raises a not found (404) error when the key doesn\'t belong to any application' do
57
- expect(last_response.status).to be 404
58
- end
59
- it 'returns the correct body when the application doesn\'t exist' do
60
- expect(JSON.parse(last_response.body)).to eq({
61
- 'status' => 404,
62
- 'field' => 'app_key',
63
- 'error' => 'unknown',
64
- 'docs' => 'https://github.com/jdr-tools/wiki/wiki/Common-errors#application-key-not-found'
65
- })
66
- end
67
- end
68
- describe 'gateway not found' do
69
- before do
70
- do_request({token: 'other_token', app_key: 'test_key'})
71
- end
72
- it 'Raises a not found (404) error when the gateway does\'nt exist' do
73
- expect(last_response.status).to be 404
74
- end
75
- it 'returns the correct body when the gateway doesn\'t exist' do
76
- expect(JSON.parse(last_response.body)).to eq({
77
- 'status' => 404,
78
- 'field' => 'token',
79
- 'error' => 'unknown',
80
- 'docs' => 'https://github.com/jdr-tools/wiki/wiki/Common-errors#gateway-token-not-found'
81
- })
82
- end
83
- end
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,10 +0,0 @@
1
- module Arkaan
2
- # Utility classes for the different micro-services of the suite.
3
- # @author Vincent Courtois <courtois.vincent@outlook.com>
4
- module Utils
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
- end
10
- end
@@ -1,8 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Controllers
4
- autoload :Base , 'arkaan/utils/controllers/base'
5
- autoload :Checked, 'arkaan/utils/controllers/checked'
6
- end
7
- end
8
- end
@@ -1,215 +0,0 @@
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
- # Declares the status route of the service, used by the Vigilante to check its status
30
- # @param path [String] the path used to map the route on.
31
- def self.declare_status_route(path = '/status')
32
- self.declare_route_with('get', path, false, { authanticated: false }) do
33
- if params['token'].nil?
34
- custom_error 400, 'vigilante.token.required'
35
- elsif Arkaan::Monitoring::Vigilante.where(token: params[:token]).first.nil?
36
- custom_error 404, 'vigilante.token.unknown'
37
- end
38
- service = Arkaan::Utils::MicroService.instance.service
39
- informations = {
40
- key: service.key,
41
- path: service.path,
42
- health: 'ok'
43
- }
44
- halt 200, informations.to_json
45
- end
46
- Arkaan::Utils::MicroService.instance.service.update_attribute(:diagnostic, path)
47
- end
48
-
49
- # Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
50
- # @param verb [String] the HTTP method used to create this route.
51
- # @param path [String] the path, beginning with a /, of the route to create.
52
- # @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
53
- def self.declare_route_with(verb, path, premium, options, &block)
54
- service = Arkaan::Utils::MicroService.instance.service
55
- complete_path = "#{Arkaan::Utils::MicroService.instance.path}#{path == '/' ? '' : path}"
56
-
57
- unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
58
- route = Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
59
- if !options.nil? && !options[:authenticated].nil?
60
- route.update_attribute(:authenticated, false)
61
- end
62
- Arkaan::Permissions::Group.where(is_superuser: true).each do |group|
63
- group.routes << route
64
- group.save!
65
- end
66
- end
67
- if premium
68
- self.public_send(verb, complete_path) do
69
- @sinatra_route = parse_current_route
70
- if !@application.premium?
71
- custom_error(403, 'common.app_key.forbidden')
72
- end
73
- instance_eval(&block)
74
- end
75
- else
76
- self.public_send(verb, complete_path, &block)
77
- end
78
- complete_path
79
- end
80
-
81
- # Loads the errors configuration file from the config folder.
82
- # @param file [String] send __FILE__
83
- def self.load_errors_from(file)
84
- config_file File.join(File.dirname(file), '..', 'config', 'errors.yml')
85
- end
86
-
87
- def before_checks
88
- add_body_to_params
89
-
90
- check_presence('token', 'app_key', route: 'common')
91
-
92
- gateway = Arkaan::Monitoring::Gateway.where(token: params['token']).first
93
- @application = Arkaan::OAuth::Application.where(key: params['app_key']).first
94
-
95
- if gateway.nil?
96
- custom_error(404, 'common.token.unknown')
97
- elsif @application.nil?
98
- custom_error(404, 'common.app_key.unknown')
99
- end
100
- end
101
-
102
- # Checks the presence of several fields given as parameters and halts the execution if it's not present.
103
- # @param fields [Array<String>] an array of fields names to search in the parameters
104
- # @param route [String] the name of the route you're requiring to put in the error message.
105
- def check_presence(*fields, route:)
106
- fields.each do |field|
107
- custom_error(400, "#{route}.#{field}.required") if params[field].nil? || params[field] == ''
108
- end
109
- end
110
-
111
- # Checks the presence of either fields given in parameters. It halts with an error only if ALL parameters are not given.
112
- # @param fields [Array<String>] an array of fields names to search in the parameters
113
- # @param route [String] the name of the route you're requiring to put in the error message.
114
- # @param key [String] the key to search in the errors configuration file.
115
- def check_either_presence(*fields, route:, key:)
116
- fields.each do |field|
117
- return true if !params[field].nil? && params[field] != ''
118
- end
119
- custom_error 400, "#{route}.#{key}.required"
120
- end
121
-
122
- # Checks if the session ID is given in the parameters and if the session exists.
123
- # @param action [String] the action used to get the errors from the errors file.
124
- # @return [Arkaan::Authentication::Session] the session when it exists.
125
- def check_session(action)
126
- check_presence('session_id', route: action)
127
- session = Arkaan::Authentication::Session.where(token: params['session_id']).first
128
- if session.nil?
129
- custom_error(404, "#{action}.session_id.unknown")
130
- end
131
- return session
132
- end
133
-
134
- def check_application(action)
135
- check_presence('app_key', route: action)
136
- application = Arkaan::OAuth::Application.where(key: params['app_key']).first
137
- custom_error(404, "#{action}.app_key.unknown") if application.nil?
138
- return application
139
- end
140
-
141
- # Adds the parsed body to the parameters, overwriting the parameters of the querystring with the values
142
- # of the SON body if they have similar keys.
143
- def add_body_to_params
144
- if request.body.respond_to?(:rewind) && request.body.respond_to?(:read)
145
- request.body.rewind
146
- parsed_body = JSON.parse(request.body.read.to_s) rescue {}
147
- parsed_body.keys.each do |key|
148
- params[key] = parsed_body[key]
149
- end
150
- end
151
- end
152
-
153
- # Gets the current route in the database from the sinatra route.
154
- # @return [Arkaan::Monitoring::Route] the route declared in the services registry.
155
- def parse_current_route
156
- splitted = request.env['sinatra.route'].split(' ')
157
- return Arkaan::Monitoring::Route.where(verb: splitted.first.downcase, path: splitted.last).first
158
- end
159
-
160
- # Halts the application and creates the returned body from the parameters and the errors config file.
161
- # @param status [Integer] the HTTP status to halt the application with.
162
- # @param path [String] the path in the configuration file to access the URL.
163
- def custom_error(status, path)
164
- route, field, error = path.split('.')
165
- docs = settings.errors[route][field][error] rescue ''
166
- halt status, {status: status, field: field, error: error, docs: docs}.to_json
167
- end
168
-
169
- # Halts the application with a Bad Request error affecting a field of a model.
170
- # @param instance [Mongoid::Document] the document having a field in error.
171
- # @param route [String] the type of action you're currently doing (e.g: 'creation')
172
- def model_error(instance, route)
173
- messages = instance.errors.messages
174
- field = messages.keys.first
175
- custom_error(400, "#{route}.#{field}.#{messages[field].first}")
176
- end
177
-
178
- # Select parameters in the params hash, by its keys.
179
- # @param fields [Array<String>] the keys to select in the params hash.
180
- # @return [Hash] the selected chunk of the params hash.
181
- def select_params(*fields)
182
- return params.select { |key, value| fields.include?(key) }
183
- end
184
-
185
- # Creates a custom error from an existing Arkaan exception class.
186
- # @param exception {StandardError} the exception to transform in a usable error.
187
- def handle_arkaan_exception(exception)
188
- custom_error(exception.status, "#{exception.action}.#{exception.field}.#{exception.error}")
189
- end
190
-
191
- error Arkaan::Utils::Errors::BadRequest do |exception|
192
- handle_arkaan_exception(exception)
193
- end
194
-
195
- error Arkaan::Utils::Errors::Forbidden do |exception|
196
- handle_arkaan_exception(exception)
197
- end
198
-
199
- error Arkaan::Utils::Errors::NotFound do |exception|
200
- handle_arkaan_exception(exception)
201
- end
202
-
203
- error Arkaan::Factories::Errors::GatewayNotFound do |exception|
204
- handle_arkaan_exception(exception)
205
- end
206
-
207
- if ENV['RACK_ENV'] != 'test'
208
- error StandardError do |exception|
209
- custom_error(500, 'system_error.unknown_field.unknown_error')
210
- end
211
- end
212
- end
213
- end
214
- end
215
- end
@@ -1,21 +0,0 @@
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
- pass if route_is_diagnostic?
10
- before_checks
11
- end
12
-
13
- def route_is_diagnostic?
14
- service = Arkaan::Utils::MicroService.instance.service
15
- diagnostic = "#{service.path}#{service.diagnostic}"
16
- request.path_info == diagnostic
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,12 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- # Module gathering all the exception classes used throughout the utils module, mainly linked to HTTP errors.
4
- # @author Vincent Courtois <courtois.vincent@outlook.com>
5
- module Errors
6
- autoload :BadRequest, 'arkaan/utils/errors/bad_request'
7
- autoload :Forbidden , 'arkaan/utils/errors/forbidden'
8
- autoload :NotFound , 'arkaan/utils/errors/not_found'
9
- autoload :HTTPError , 'arkaan/utils/errors/http_error'
10
- end
11
- end
12
- end
@@ -1,14 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Errors
4
- # A bad request error is raised when the data given to a model makes this model invalid.
5
- # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class BadRequest < Arkaan::Utils::Errors::HTTPError
7
-
8
- def initialize(action:, field:, error:)
9
- super(action, field, error, 400)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Errors
4
- # A forbidden error occurs when a user tries to perform an action he's not allowed to.
5
- # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class Forbidden < Arkaan::Utils::Errors::HTTPError
7
-
8
- def initialize (field:, action:, error:)
9
- super(action, field, error, 403)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,30 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Errors
4
- # Standard class parent to all specialized http errors.
5
- # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class HTTPError < StandardError
7
-
8
- # @!attribute [rw] field
9
- # @return [String, Symbol] the name of the field in error in the model.
10
- attr_accessor :field
11
- # @!attribute [rw] action
12
- # @return [String] the name of the action the user was trying to perform on the model (often crate or update).
13
- attr_accessor :action
14
- # @attribute [rw] error
15
- # @return [String] the label of the error returned by the model.
16
- attr_accessor :error
17
- # @attribute [rw] status
18
- # @return [Integer] the HTTP status code as a number (eg: 400, 422 or 500)
19
- attr_accessor :status
20
-
21
- def initialize (action, field, error, status)
22
- @action = action
23
- @field = field.to_s
24
- @error = error
25
- @status = status
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,14 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Errors
4
- # A not found error occurs when a user tries to reach a resource that does not exist.
5
- # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class NotFound < Arkaan::Utils::Errors::HTTPError
7
-
8
- def initialize (field:, action:, error:)
9
- super(action, field, error, 404)
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,7 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- module Loaders
4
- autoload :Heroku, 'arkaaan/utils/loaders/heroku'
5
- end
6
- end
7
- end
@@ -1,20 +0,0 @@
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
@@ -1,170 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- # This class is a singleton to load and save parameters for the whole application.
4
- # @author Vincent Courtois <courtois.vincent@outlook.com>
5
- class MicroService
6
- include Singleton
7
-
8
- # @!attribute [r] name
9
- # @return [String] the name of the service you want to load.
10
- attr_reader :service
11
- # @!attribute [r] location
12
- # @return [String] the path to the file loading the whole application, used to deduce the loading paths.
13
- attr_reader :location
14
- # @!attribute [r] name
15
- # @return [String] the name of the service, used later to instantiate it when the mongoid configuration is fully loaded.
16
- attr_reader :name
17
- # @!attribute [r] instance
18
- # @return [Arkaan::Monitoring::Instance] the instance of the service currently deployed.
19
- attr_reader :instance
20
- # @!attribute [r] type
21
- # @return [Symbol] the type of instance the application is declaring
22
- attr_reader :type
23
-
24
- def initialize
25
- @location = false
26
- @service = false
27
- @instance = false
28
- @name = false
29
- @type = ENV['INSTANCE_TYPE'] || :heroku
30
- @controller_classes = []
31
- end
32
-
33
- def get_controllers
34
- return [] if defined?(Controllers).nil?
35
- classes = Controllers.constants.map { |symbol| get_const(symbol) }
36
- return classes.select { |symbol| symbol.is_a? Class }
37
- end
38
-
39
- def get_const(symbol)
40
- return Object.const_get("Controllers::#{symbol.to_s}")
41
- end
42
-
43
- # Determines if the application can be loaded (all the parameters have been correctly set)
44
- # @return [Boolean] TRUE if the application can be safely loaded, FALSE otherwise.
45
- def loadable?
46
- return !!(service && location)
47
- end
48
-
49
- # Getter for the path on which the service is mapped.
50
- # @return [String, Boolean] the absolute path in the URL on which the service is mapped upon, or FALSE if it's not set already.
51
- def path
52
- return service ? service.path : false
53
- end
54
-
55
- # Look for the service and sets it if it's found in the database, or set it to nil if not found.
56
- # @param [String] service_name - the name of the service to look for in the database.
57
- # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
58
- def register_as(service_name)
59
- @name = service_name
60
- return self
61
- end
62
-
63
- # Sets the location of the file calling the micro service and initializing it so that it's used as root.
64
- # @param filename [String] the full naame of the file with the extension.
65
- # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
66
- def from_location(filename)
67
- @location = File.dirname(filename)
68
- return self
69
- end
70
-
71
- # Loads the application in standard (production/development) mode, without the test files.
72
- # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
73
- def in_standard_mode
74
- return load_application(test_mode: false)
75
- end
76
-
77
- # Loads the application in test mode, by adding the needed files to run the test suite to the standard loading process.
78
- # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
79
- def in_test_mode
80
- @location = File.join(location, '..')
81
- return load_application(test_mode: true)
82
- end
83
-
84
- # Loads the application as a websockets service. Only the websockets application should use that.
85
- # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
86
- def in_websocket_mode
87
- load_mongoid_configuration
88
- load_standard_files
89
-
90
- Arkaan::Monitoring::Websocket.find_or_create_by(url: ENV['WEBSOCKET_URL']).save
91
- return self
92
- end
93
-
94
- # Deactivates the current instance and the associated service if no more instances are available.
95
- def deactivate!
96
- instance.update_attribute(:running, false)
97
- service.update_attribute(:test_mode, false) if ENV['TEST_MODE']
98
- end
99
-
100
- private
101
-
102
- # Registers the service in the database if it has not been created already.
103
- def register_service
104
- @service = Arkaan::Monitoring::Service.create(key: @name, path: "/#{@name}")
105
- end
106
-
107
- # Register the instance of the currently deployed service in the database.
108
- # @return [Arkaan::Monitoring::Instance] the instance of the micro service currently running.
109
- def register_instance
110
- @instance = @service.instances.where(url: ENV['SERVICE_URL']).first
111
- if @instance.nil?
112
- @instance = Arkaan::Monitoring::Instance.create(service: @service, url: ENV['SERVICE_URL'], type: type)
113
- end
114
- @instance.update_attribute(:running, true)
115
- return @instance
116
- end
117
-
118
- # Loads the configuration for Mongoid, the files of the application, and registers the service and the instance in the database.
119
- # @param test_mode [Boolean] TRUE to run in test mode (from /spec), FALSE otherwise.
120
- # @return [Arkaan::Utils::MicroService] the current instance of the micro service to chain other calls.
121
- def load_application(test_mode: false)
122
- Dotenv.load
123
- load_mongoid_configuration(test_mode: test_mode)
124
- if !!(@name && location)
125
- @service = Arkaan::Monitoring::Service.where(key: @name).first
126
- register_service if @service.nil?
127
- if ENV['TEST_MODE']
128
- @service.update_attribute(:test_mode, true)
129
- end
130
- register_instance
131
- load_plugin!
132
- if service
133
- load_standard_files
134
- load_test_files if test_mode
135
- end
136
- end
137
- return self
138
- end
139
-
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)
144
- end
145
- end
146
-
147
- def load_mongoid_configuration(test_mode: false)
148
- environment = test_mode ? 'test' : (ENV['RACK_ENV'] || 'development')
149
- Mongoid.load!(File.join(@location, 'config', 'mongoid.yml'), environment)
150
- end
151
-
152
- def load_standard_files
153
- require_folder('decorators')
154
- require_folder('services')
155
- require_folder('controllers')
156
- end
157
-
158
- def load_test_files
159
- require_folder('spec', 'support')
160
- require_folder('spec', 'shared')
161
- end
162
-
163
- def require_folder(*folders)
164
- Dir[File.join(location, folders, '**', '*.rb')].each do |filename|
165
- require filename
166
- end
167
- end
168
- end
169
- end
170
- end
@@ -1,25 +0,0 @@
1
- module Arkaan
2
- module Utils
3
- # This class loads the necessary data in the database if they don't exist yet.
4
- # @author Vincent Courtois <courtois.vincent@outlook.com>
5
- class Seeder
6
- include Singleton
7
-
8
- # Creates the service if it does not exist, and the instance if it does not exist.
9
- # @return [Arkaan::Monitoring::Service] the created, or found, service corresponding to this micro-service.
10
- def create_service(key)
11
- service = Arkaan::Monitoring::Service.where(key: key).first
12
-
13
- if service.nil?
14
- service = Arkaan::Monitoring::Service.create!(key: key, path: "/#{key}", premium: true, active: true)
15
- end
16
-
17
- if service.instances.where(url: ENV['SERVICE_URL']).first.nil?
18
- Arkaan::Monitoring::Instance.create!(url: ENV['SERVICE_URL'], running: true, service: service, active: true)
19
- end
20
-
21
- return service
22
- end
23
- end
24
- end
25
- end