kybus-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b529750655edb3e735cdb5e3faa8a271b4f17eb5bb87aedbbf6b597fa5455c07
4
+ data.tar.gz: 73c8d3cdd8b32e064bd89301245b1a7bceb86d641b25d50fe6b48e8ff5c4ba09
5
+ SHA512:
6
+ metadata.gz: 166f6bbd1a7328400eb4a8fec8dd3e0d9b69e7f7f2688cdca44a4aed7e9da52113c6a139b7e074994e83007b784a0f241b7ae7b89e6791a3bb3356de1284aca9
7
+ data.tar.gz: bcf6ce87492aedc4f44e9d41213d52059519a4d93f83ee424efd3804819ff55e1f880d67e078b07d3bb7058fa73a84fde5e2035ce567d91a3be10d6f11917e7c
data/lib/kybus/core.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ module DRY
5
+ autoload(:Daemon, 'kybus/dry/daemon.rb')
6
+ autoload(:ResourceInjector, 'kybus/dry/resource_injector.rb')
7
+ end
8
+ autoload(:Exceptions, 'kybus/exceptions.rb')
9
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kybus/logger'
4
+
5
+ module Kybus
6
+ module DRY
7
+ # Use this for running tasks that will be looping by only sending a lambda
8
+ # TODO: Improve this class.
9
+ # - Add a maechanism for killing the process
10
+ # - Add a function to test how many threads are alive
11
+ # - Make it testable
12
+ # - Add unit tests
13
+ class Daemon
14
+ include Kybus::Logger
15
+
16
+ def initialize(wait_time, attach, retry_on_failure = false)
17
+ @proc = -> { yield }
18
+ @wait_time = wait_time
19
+ @attach = attach
20
+ @retry_on_failure = retry_on_failure
21
+ @finish = false
22
+ end
23
+
24
+ def task
25
+ log_info 'starting daemon'
26
+ loop do
27
+ begin
28
+ @proc.call
29
+ rescue StandardError => e
30
+ raise unless @retry_on_failure
31
+
32
+ # :nocov: #
33
+ log_error('Unexpected error', error: e)
34
+ # :nocov: #
35
+ end
36
+ sleep(@wait_time)
37
+ end
38
+ end
39
+
40
+ def run
41
+ if @attach
42
+ task
43
+ else
44
+ # :nocov: #
45
+ @thread = Thread.new { task }
46
+ # :nocov: #
47
+ end
48
+ end
49
+
50
+ # :nocov: #
51
+ def await
52
+ @thread&.join
53
+ end
54
+ # :nocov: #
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ module DRY
5
+ ##
6
+ # Provides a method for sending external dependencies
7
+ # to classes, like database connections, configurations,
8
+ # and other objects that can vary but does not modify
9
+ # the class functionality.
10
+ # This class works a decorator to be just extended
11
+ # by classes
12
+ #
13
+ # class Controller
14
+ # extend ResourceInjector
15
+ # end
16
+ module ResourceInjector
17
+ # Initialices the resources value and returns the object.
18
+ # This method should not be used from the out context.
19
+ # Resources are grouped by key
20
+ def resources(key)
21
+ @resources ||= {}
22
+ @resources[key] ||= {}
23
+ @resources[key]
24
+ end
25
+
26
+ # Provides the interface for sending objects inside the class.
27
+ # The resources have a group and sub group.
28
+ # When no group is given, it will be added to the :root group
29
+ # ==== Examples
30
+ # Controller.inject(:magic_number, 42)
31
+ # Controller.inject(:databases, :database_conection, Sequel.connect)
32
+ def register(key, subkey, value = nil)
33
+ if value.nil?
34
+ value = subkey
35
+ subkey = key
36
+ key = :root
37
+ end
38
+ resources(key)[subkey] = value
39
+ end
40
+
41
+ # Provides the inside interface for fetching the objects that were
42
+ # previously provided from the external world.
43
+ # Also, when no subgroup is given, the key is fetched from :root
44
+ # ==== Examples
45
+ # class Controller
46
+ # def initialize
47
+ # @magic_number = self.class.resource(:magic_number)
48
+ # end
49
+ #
50
+ # def self.factory(id)
51
+ # db = resource(:databases, :database_conection)
52
+ # end
53
+ # end
54
+ def resource(key, subkey = nil)
55
+ if subkey.nil?
56
+ subkey = key
57
+ key = :root
58
+ end
59
+ res = resources(key)[subkey]
60
+ raise("Resource `#{key}::#{subkey}` Not Found") if res.nil?
61
+
62
+ res
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ module Exceptions
5
+ autoload(:HTTP, 'kybus/http_exceptions.rb')
6
+ ##
7
+ # Exception used as standard error on this gem.
8
+ # @param message. This param is meant to be read by another developer
9
+ # It would be nice if the message were self descriptive
10
+ # enough so the developer won't need to ask the creator
11
+ # of the app for help.
12
+ # @param code. The code is similar to message, but for this case
13
+ # it is meant to be used for the program to handle
14
+ # exceptions and to make easier to identify the exception..
15
+ # The default is the exception class, so it won't change
16
+ # almost never.
17
+ # @param data. Contains additional data to detail the error.
18
+ class AntBaseException < StandardError
19
+ attr_reader :message, :code, :data
20
+ def initialize(message, code, data)
21
+ @message = message
22
+ @code = code
23
+ @data = data
24
+ end
25
+
26
+ def to_log_format
27
+ to_h.merge(class: self.class.name)
28
+ end
29
+
30
+ def to_h
31
+ {
32
+ message: message,
33
+ code: code,
34
+ data: data,
35
+ backtrace: backtrace
36
+ }
37
+ end
38
+ end
39
+
40
+ ##
41
+ # Is used to express a success with the client's request.
42
+ class AntSuccess < AntBaseException
43
+ def initialize(message, code = nil, data = {})
44
+ code ||= self.class.name.split('::').last
45
+ super(message, code, data)
46
+ end
47
+ end
48
+
49
+ ##
50
+ # Is used to express a problem with the client's request.
51
+ class AntFail < AntBaseException
52
+ def initialize(message, code = nil, data = {})
53
+ code ||= self.class.name.split('::').last
54
+ code = 'BadRequest' if code == 'AntFail'
55
+ super(message, code, data)
56
+ end
57
+ end
58
+
59
+ ##
60
+ # Is used to express an error that was found during the execution of the
61
+ # program but it also means that the invoked endpoint has not the power to
62
+ # fix it, so it will only complain.
63
+ class AntError < AntBaseException
64
+ def initialize(message, code = nil, data = {})
65
+ code ||= self.class.name.split('::').last
66
+ code = 'ServerError' if code == 'AntError'
67
+ super(message, code, data)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'exceptions'
4
+
5
+ module Kybus
6
+ module Exceptions
7
+ # Provides basic execptions for http common errors.
8
+ module HTTP
9
+ class << self
10
+ EXCEPTION_TYPES = {
11
+ success: Kybus::Exceptions::AntSuccess,
12
+ fail: Kybus::Exceptions::AntFail,
13
+ error: Kybus::Exceptions::AntError
14
+ }.freeze
15
+
16
+ def new_http_exception(class_name, http_code, type)
17
+ parent = exception_type(type)
18
+ http_exception_class = Class.new(parent) do
19
+ def initialize(message, object = {})
20
+ super(message, nil, object)
21
+ end
22
+
23
+ define_method 'http_code' do
24
+ http_code
25
+ end
26
+ end
27
+
28
+ const_set(class_name, http_exception_class)
29
+ end
30
+
31
+ private
32
+
33
+ def exception_type(type)
34
+ EXCEPTION_TYPES[type.to_sym]
35
+ end
36
+ end
37
+
38
+ http_codes = [
39
+ { code_name: 'Ok', code: 200, type: :success },
40
+ { code_name: 'Created', code: 201, type: :success },
41
+ { code_name: 'Accepted', code: 202, type: :success },
42
+ { code_name: 'NoContent', code: 204, type: :success },
43
+ { code_name: 'BadRequest', code: 400, type: :fail },
44
+ { code_name: 'Unauthorized', code: 401, type: :fail },
45
+ { code_name: 'Forbidden', code: 403, type: :fail },
46
+ { code_name: 'NotFound', code: 404, type: :fail },
47
+ { code_name: 'NotValid', code: 422, type: :fail }
48
+ ]
49
+
50
+ http_codes.each do |code|
51
+ Exceptions::HTTP.new_http_exception(
52
+ code[:code_name],
53
+ code[:code],
54
+ code[:type]
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kybus
4
+ module Core
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kybus-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gilberto Vargas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kybus-logger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '12.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '12.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '6.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '6.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.16'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.16'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.5'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.5'
139
+ description: |2
140
+ Kybus::Core will be used across all the kybus gems. Provides the most basic
141
+ functionality or what might be used along with more than one gem.
142
+ Currently this only exposes the basic exceptions and the DRY patterns.
143
+ email:
144
+ - tachoguitar@gmail.com
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - lib/kybus/core.rb
150
+ - lib/kybus/dry/daemon.rb
151
+ - lib/kybus/dry/resource_injector.rb
152
+ - lib/kybus/exceptions.rb
153
+ - lib/kybus/http_exceptions.rb
154
+ - lib/kybus/version.rb
155
+ homepage: https://github.com/tachomex/kybus
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubygems_version: 3.1.4
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Kybus framework core functionality
178
+ test_files: []