arkaan 0.7.4 → 0.7.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c15ef80800e647f2a5f06482e9794678fc3bf05c
4
- data.tar.gz: e4b2c527ea546b7e42489e0182c559cecba3a6b0
3
+ metadata.gz: dc727a94560a7d9ab37e10b788a28e322ba0b346
4
+ data.tar.gz: 75877dfb1faff014c23f8e3afb828de8edfb071f
5
5
  SHA512:
6
- metadata.gz: 46934cd25c1c983e1a5b9af0a67bf200b589c3067ca2c91b30d93b0f0419d9247eee39a1eb2e189a9758f1fb9ee77bb3c63e9147a895b2cb2951c1d5ea597702
7
- data.tar.gz: 264858f396fa4371f48ea4cc97d009b2204faec9b04eba84c96715a77415ed67f60ba08f9c99568b5a8fb5a7118f01fa115548c2ad279b065402352804d27837
6
+ metadata.gz: 0636b1c58ce076e7594afc504232f7014e3a17021b337c96dd36a74702c98a45618516b3df20dd5ab7f21acc9d74e27eb1562c45272dd76f4951b871fe6f870c
7
+ data.tar.gz: 991da6be9cd21c206ab54c320eddb089be6a62e0c6eb84de842c6a98402fab5340e017c42fc9f6731dc43c251409e8215cd9ae228f002a675314013be506c135
@@ -20,24 +20,28 @@ module Arkaan
20
20
  end
21
21
  end
22
22
 
23
- def self.declare_service(key)
24
- @@micro_service = Arkaan::Monitoring::Service.find_by(key: key)
25
- end
26
-
23
+ # Creates a premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
24
+ # @param verb [String] the HTTP method used to create this route.
25
+ # @param path [String] the path, beginning with a /, of the route to create.
27
26
  def self.declare_route(verb, path, &block)
28
27
  self.declare_route_with(verb, path, false, &block)
29
28
  end
30
29
 
30
+ # Creates a non premium route whithin the Sinatra application, and registers it in the database if it does not already exists.
31
+ # @param verb [String] the HTTP method used to create this route.
32
+ # @param path [String] the path, beginning with a /, of the route to create.
31
33
  def self.declare_premium_route(verb, path, &block)
32
34
  self.declare_route_with(verb, path, true, &block)
33
35
  end
34
36
 
37
+ # Creates a route whithin the Sinatra application, and registers it in the database if it does not already exists.
38
+ # @param verb [String] the HTTP method used to create this route.
39
+ # @param path [String] the path, beginning with a /, of the route to create.
40
+ # @param premium [Boolean] TRUE to make the route premium, FALSE otherwise.
35
41
  def self.declare_route_with(verb, path, premium, &block)
36
- unless @@micro_service.nil?
37
- if @@micro_service.routes.where(path: path, verb: verb).first.nil?
38
- @@micro_service.routes << Arkaan::Monitoring::Route.new(path: path, verb: verb, premium: premium)
39
- @@micro_service.save!
40
- end
42
+ service = Arkaan::Utils::MicroService.instance.service
43
+ unless service.nil? || !service.routes.where(path: path, verb: verb).first.nil?
44
+ Arkaan::Monitoring::Route.create(path: path, verb: verb, premium: premium, service: service)
41
45
  end
42
46
  self.public_send(verb, path, &block)
43
47
  end
@@ -1,68 +1,112 @@
1
1
  module Arkaan
2
2
  module Utils
3
- # The MicroService class is the loader for a micro service in general.
3
+ # This class is a singleton to load and save parameters for the whole application.
4
4
  # @author Vincent Courtois <courtois.vincent@outlook.com>
5
5
  class MicroService
6
+ include Singleton
6
7
 
7
- # @!attribute [r] root
8
- # @return [String] the root path of the application, from where each path is deduced.
9
- attr_reader :root
10
8
  # @!attribute [r] name
11
- # @return [String] the name of the service, given when initializing it.
12
- attr_reader :name
13
- # @!attribute [r] test_mode
14
- # @return [Boolean] TRUE if the micro service is initialized from a test suite, FALSE otherwise.
15
- attr_reader :test_mode
16
- # @!attribute [r] service
17
- # @return [Arkaan::Monitoring::Service] the service stored in the database corresponding to this application.
9
+ # @return [String] the name of the service you want to load.
18
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
19
14
 
20
- # loads the application by requiring the files from the folders they're supposed to be in.
21
- # @param name [String] the snake-cased name of the application, for service registration purpose mainly.
22
- # @param root [String]
23
- def initialize(name:, root:, test_mode: false)
24
- @root = test_mode ? File.join(root, '..') : root
25
- @name = name
26
- @test_mode = test_mode
27
- require_mongoid_config(root)
28
- @service = register_service
15
+ def initialize
16
+ @location = false
17
+ @service = false
29
18
  end
30
19
 
31
- # Loads the necessary components for the application by requiring the needed files.
32
- # @param test_mode [Boolean] TRUE if the application i supposed to be launched from the spec_helper, FALSE otherwise.
33
- def load!
34
- require_folder(root, 'decorators')
35
- require_folder(root, 'controllers')
36
- if test_mode
37
- require_folder(root, 'spec', 'support')
38
- require_folder(root, 'spec', 'shared')
39
- end
40
- return
20
+ # Determines if the application can be loaded (all the parameters have been correctly set)
21
+ # @return [Boolean] TRUE if the application can be safely loaded, FALSE otherwise.
22
+ def loadable?
23
+ return !!(service && location)
41
24
  end
42
25
 
43
- # Creates the service instance if necessary, and returns it.
44
- # @return [Arkaan::Monitoring::Service] the service in the registry corresponding to this micro-service.
45
- def register_service
46
- service = Arkaan::Monitoring::Service.where(key: @name).first
47
- if service.nil?
48
- service = Arkaan::Monitoring::Service.create!(key: @name, path: "/#{@name}", premium: true, active: true)
49
- end
50
- if service.instances.where(url: ENV['SERVICE_URL']).first.nil?
51
- Arkaan::Monitoring::Instance.create!(url: ENV['SERVICE_URL'], running: true, service: service, active: true)
26
+ # Getter for the path on which the service is mapped.
27
+ # @return [String, Boolean] the absolute path in the URL on which the service is mapped upon, or FALSE if it's not set already.
28
+ def path
29
+ return service ? service.path : false
30
+ end
31
+
32
+ # Getter for the name of the service.
33
+ # @return [String, Boolean] the name of the service as it is registered in the database, or FALSE if it's not set already.
34
+ def name
35
+ return service ? service.key : false
36
+ end
37
+
38
+ # Look for the service and sets it if it's found in the database, or set it to nil if not found.
39
+ # @param [String] service_name - the name of the service to look for in the database.
40
+ # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
41
+ def register_as(service_name)
42
+ @service = Arkaan::Monitoring::Service.where(key: service_name).first
43
+ register_service if @service.nil?
44
+ register_instance
45
+ return self
46
+ end
47
+
48
+ # Sets the location of the file calling the micro service and initializing it so that it's used as root.
49
+ # @param filename [String] the full naame of the file with the extension.
50
+ # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
51
+ def from_location(filename)
52
+ @location = File.dirname(filename)
53
+ return self
54
+ end
55
+
56
+ # Loads the application in standard (production/development) mode, without the test files.
57
+ # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
58
+ def in_standard_mode
59
+ return load_application(test_mode: false)
60
+ end
61
+
62
+ # Loads the application in test mode, by adding the needed files to run the test suite to the standard loading process.
63
+ # @return [Arkaan::utils::MicroService] the instance of the micro-service to chain other calls.
64
+ def in_test_mode
65
+ @location = File.join(location, '..')
66
+ return load_application(test_mode: true)
67
+ end
68
+
69
+ private
70
+
71
+ def register_service(key)
72
+ @service = Arkaan::Service.create(key: key, path: "/#{key}")
73
+ end
74
+
75
+ def register_instance
76
+ instance = @service.instances.where(url: ENV['SERVICE_URL']).first
77
+ if instance.nil?
78
+ instance = Arkaan::Monitoring::Instance.create(service: @service, url: ENV['SERVICE_URL'])
52
79
  end
53
- return service
80
+ return instance
81
+ end
82
+
83
+ def load_application(test_mode: false)
84
+ @location = File.join(location, '..')
85
+ load_mongoid_configuration
86
+ load_standard_files
87
+ load_test_files if test_mode
88
+ return self
89
+ end
90
+
91
+ def load_mongoid_configuration
92
+ Mongoid.load!(File.join(location, 'config', 'mongoid.yml'))
54
93
  end
55
94
 
56
- # Require all the files from a folder iteratively by assembling the parts given as parameters.
57
- # @param paths_elements [Array<String>] the elements to assemble to form the path of the folder.
58
- def require_folder(*paths_elements)
59
- Dir[File.join(paths_elements, '**', '*.rb')].each { |filename| require filename }
95
+ def load_standard_files
96
+ require_folder('decorators')
97
+ require_folder('controllers')
60
98
  end
61
99
 
62
- # Requires and loads the mongoid configuration from its default location.
63
- # @param root [String] the root folder of the application from where require the configuration path;
64
- def require_mongoid_config(root)
65
- Mongoid.load!(File.join(root, 'config', 'mongoid.yml'))
100
+ def load_test_files
101
+ require_folder('spec', 'support')
102
+ require_folder('spec', 'factories')
103
+ require_folder('spec', 'shared')
104
+ end
105
+
106
+ def require_folder(*folders)
107
+ Dir[File.join(location, folders)].each do |filename|
108
+ require filename
109
+ end
66
110
  end
67
111
  end
68
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arkaan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.4
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Courtois