virtuaservices 0.4.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb059e789bd366ef871e57251808b7d6f7a7f1605008f7ab7afe12169becf477
4
- data.tar.gz: 277f3210c31440030234400b95cc1af645afbefab05fa194b1a20e61fdeef088
3
+ metadata.gz: 429f143252f897b7157b1e21f3c8cbc703a5aeb521af6554ea44d76ada0f2671
4
+ data.tar.gz: 76f1f59d9aafb5a4ad2c4e1c7d1c7e887e9bb48c4c11d2a98c356af2651c34a6
5
5
  SHA512:
6
- metadata.gz: b5fdca969d3b8280c9ef4e76b34682fab5ed70431d08150ee08b10b99e36f2160339ad0b1e008013ff4e83b7fd5c051b75986505a4014c0685097ea7252eed05
7
- data.tar.gz: 996825708dfd4fbc9b6dc1f52d723d92f8e9ef7be414fed6fb4feec16aff9dcc041ec30c8036c7ec5d0b1c1cbd21bed4abc7e0930705aef66c85ce1bf7bc9968
6
+ metadata.gz: cff262582fc0fac91cf20e140a24a44f5ddf9511e3e00286fead412f2000eedb5a0782756fe6126952794af3bc78f994eb990842b244dab3160a8a820c9da301
7
+ data.tar.gz: f0e114472f942b4a45fab68414ca1a39b0e3adbc858682a6cf6574e2d0c871e25fe7c064f5574e37cf90dcd090e596e997437001045feb8f1f6bb439234d1809
@@ -1,9 +1,19 @@
1
1
  require 'mongoid'
2
+ require 'active_model'
3
+ require 'active_support'
4
+ require 'sinatra/base'
5
+ require 'sinatra/config_file'
6
+ require 'platform-api'
7
+ require 'faraday'
8
+ require 'sinatra/custom_logger'
9
+ require 'dotenv/load'
2
10
 
3
11
  module Virtuaservices
4
12
  autoload :Account , 'virtuaservices/account'
5
13
  autoload :Authentication, 'virtuaservices/authentication'
6
14
  autoload :Concerns , 'virtuaservices/concerns'
15
+ autoload :Decorators , 'virtuaservices/decorators'
16
+ autoload :Factories , 'virtuaservices/factories'
7
17
  autoload :Monitoring , 'virtuaservices/monitoring'
8
18
  autoload :OAuth , 'virtuaservices/oauth'
9
19
  autoload :Permissions , 'virtuaservices/permissions'
@@ -0,0 +1,8 @@
1
+ module Virtuaservices
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, 'virtuaservices/decorators/gateway'
6
+ autoload :Errors , 'virtuaservices/decorators/errors'
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Virtuaservices
2
+ module Decorators
3
+ # Module holding all the errors concerning the code of the decorators.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Errors
6
+ autoload :EnvVariableMissing, 'virtuaservices/decorators/errors/env_variable_missing'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Virtuaservices
2
+ module Decorators
3
+ module Errors
4
+ # Error raised if the application key variable is missing.
5
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
6
+ class EnvVariableMissing < Virtuaservices::Utils::Errors::HTTPError
7
+
8
+ def initialize(action:)
9
+ super(action, 'app_key', 'not_found', 404)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,109 @@
1
+ module Virtuaservices
2
+ module Decorators
3
+ # Decorator for a service, providing methods to make requests on it.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ class Gateway < Draper::Decorator
6
+ delegate_all
7
+
8
+ # @!attribute [rw] action
9
+ # @return [String] the action of the route using this API.
10
+ attr_accessor :action
11
+
12
+ attr_accessor :logger
13
+
14
+ def initialize(action, _object)
15
+ super(_object)
16
+ @logger = Logger.new(STDOUT)
17
+ @action = action
18
+ end
19
+
20
+ # Shortcut to make a DELETE request on the API.
21
+ # @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API.
22
+ # @param url [String] the URL you want to reach on the service.
23
+ # @param params [Hash] the additional parameters to pass in the JSON body.
24
+ def delete(session:, url:, params:)
25
+ return make_request_without_body(verb: 'delete', session: session, url: url, params: params)
26
+ end
27
+
28
+ # Shortcut to make a GET request on the API.
29
+ # @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API.
30
+ # @param url [String] the URL you want to reach on the service.
31
+ # @param params [Hash] the additional parameters to pass in the JSON body.
32
+ def get(session:, url:, params:)
33
+ return make_request_without_body(verb: 'get', session: session, url: url, params: params)
34
+ end
35
+
36
+ # Shortcut to make a POST request on the API.
37
+ # @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API.
38
+ # @param url [String] the URL you want to reach on the service.
39
+ # @param params [Hash] the additional parameters to pass in the JSON body.
40
+ def post(session:, url:, params:)
41
+ return make_request(verb: 'post', session: session, url: url, params: params)
42
+ end
43
+
44
+ # Shortcut to make a PUT request on the API.
45
+ # @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API.
46
+ # @param url [String] the URL you want to reach on the service.
47
+ # @param params [Hash] the additional parameters to pass in the JSON body.
48
+ def put(session:, url:, params:)
49
+ return make_request(verb: 'put', session: session, url: url, params: params)
50
+ end
51
+
52
+ private
53
+
54
+ # Makes a POST request to the given service with the following steps :
55
+ # 1. Gets an active and running instance of the service to make the request.
56
+ # 2. Creates a Faraday connection to use it as a pipeline for the request.
57
+ # 3. Makes the actual request and returns an object with the status and body of the response.
58
+ #
59
+ # @param verb [String] the HTTP verb to use for this request.
60
+ # @param session [Virtuaservices::Authentication::Session] the session of the user requesting the API.
61
+ # @param url [String] the URL you want to reach on the service.
62
+ # @param params [Hash] the additional parameters to pass in the JSON body.
63
+ #
64
+ # @return [Hash, Boolean] FALSE if no instance are found, or an object with :status and :body keys correspding
65
+ # to the status and body of the response to the request
66
+ def make_request(verb:, session:, url:, params:)
67
+ params = before_requests(session, params)
68
+ connection = get_connection
69
+
70
+ response = connection.send(verb) do |req|
71
+ req.url url
72
+ req.headers['Content-Type'] = 'application/json'
73
+ req.body = params.to_json
74
+ end
75
+
76
+ return {
77
+ status: response.status,
78
+ body: JSON.parse(response.body)
79
+ }
80
+ end
81
+
82
+ def make_request_without_body(verb:, session:, url:, params:)
83
+ params = before_requests(session, params)
84
+ connection = get_connection
85
+ response = connection.send(verb) do |req|
86
+ req.url url, params
87
+ req.headers['Content-Type'] = 'application/json'
88
+ end
89
+ end
90
+
91
+ def before_requests(session, params)
92
+ if ENV['APP_KEY'].nil?
93
+ raise Virtuaservices::Decorators::Errors::EnvVariableMissing.new(action: action)
94
+ end
95
+ params[:app_key] = ENV['APP_KEY']
96
+ params[:session_id] = session.token
97
+ return params
98
+ end
99
+
100
+ def get_connection
101
+ Faraday.new(object.url) do |faraday|
102
+ faraday.request :url_encoded
103
+ faraday.response :logger
104
+ faraday.adapter Faraday.default_adapter
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,8 @@
1
+ module Virtuaservices
2
+ # Static factories are used to create decorated objects easily.
3
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
4
+ module Factories
5
+ autoload :Gateways, 'virtuaservices/factories/gateways'
6
+ autoload :Errors , 'virtuaservices/factories/errors'
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Virtuaservices
2
+ module Factories
3
+ # Module holding all the errors concerning the code of the factories.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ module Errors
6
+ autoload :GatewayNotFound, 'virtuaservices/factories/errors/gateway_not_found'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Virtuaservices
2
+ module Factories
3
+ module Errors
4
+ # Error raised when not gateway active and running is found in the factory.
5
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
6
+ class GatewayNotFound < Virtuaservices::Utils::Errors::HTTPError
7
+
8
+ def initialize(action:)
9
+ super(action, 'gateway_id', 'not_found', 404)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module Virtuaservices
2
+ module Factories
3
+ # This class provides methods to create decorated services.
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
+ class Gateways
6
+
7
+ # Searches for a gateway via its key and returns it decorated.
8
+ # @param key [String] the key of the server you want to find.
9
+ # @return [Virtuaservices::Decorators::Gateway, NilClass] nil if the gateway is not found, or the decorated gateway.
10
+ def self.random(action)
11
+ gateway = Virtuaservices::Monitoring::Gateway.where(active: true, running: true).first
12
+ if gateway.nil?
13
+ raise Virtuaservices::Factories::Errors::GatewayNotFound.new(action: action)
14
+ end
15
+ return Virtuaservices::Decorators::Gateway.new(action, gateway)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,9 +1,9 @@
1
- module Arkaan
1
+ module Virtuaservices
2
2
  module Utils
3
3
  module Errors
4
4
  # A forbidden error occurs when a user tries to perform an action he's not allowed to.
5
5
  # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class Forbidden < Arkaan::Utils::Errors::HTTPError
6
+ class Forbidden < Virtuaservices::Utils::Errors::HTTPError
7
7
 
8
8
  def initialize (field:, action:, error:)
9
9
  super(action, field, error, 403)
@@ -1,4 +1,4 @@
1
- module Arkaan
1
+ module Virtuaservices
2
2
  module Utils
3
3
  module Errors
4
4
  # Standard class parent to all specialized http errors.
@@ -1,9 +1,9 @@
1
- module Arkaan
1
+ module Virtuaservices
2
2
  module Utils
3
3
  module Errors
4
4
  # A not found error occurs when a user tries to reach a resource that does not exist.
5
5
  # @author Vincent Courtois <courtois.vincent@outlook.com>
6
- class NotFound < Arkaan::Utils::Errors::HTTPError
6
+ class NotFound < Virtuaservices::Utils::Errors::HTTPError
7
7
 
8
8
  def initialize (field:, action:, error:)
9
9
  super(action, field, error, 404)
@@ -1,3 +1,3 @@
1
1
  module Virtuaservices
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtuaservices
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Courtois
@@ -267,6 +267,14 @@ files:
267
267
  - lib/virtuaservices/concerns/premiumable.rb
268
268
  - lib/virtuaservices/concerns/sluggable.rb
269
269
  - lib/virtuaservices/concerns/typable.rb
270
+ - lib/virtuaservices/decorators.rb
271
+ - lib/virtuaservices/decorators/errors.rb
272
+ - lib/virtuaservices/decorators/errors/env_variable_missing.rb
273
+ - lib/virtuaservices/decorators/gateway.rb
274
+ - lib/virtuaservices/factories.rb
275
+ - lib/virtuaservices/factories/errors.rb
276
+ - lib/virtuaservices/factories/errors/gateway_not_found.rb
277
+ - lib/virtuaservices/factories/gateways.rb
270
278
  - lib/virtuaservices/monitoring.rb
271
279
  - lib/virtuaservices/monitoring/gateway.rb
272
280
  - lib/virtuaservices/monitoring/instance.rb