ant-core 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ant/core.rb +9 -0
- data/lib/ant/dry/daemon.rb +57 -0
- data/lib/ant/dry/resource_injector.rb +66 -0
- data/lib/ant/exceptions.rb +71 -0
- data/lib/ant/http_exceptions.rb +59 -0
- data/lib/ant/version.rb +7 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 684d845bdd989ba73fe3b462f2ce7806bdbdb1bd0f972d5c7be23904c97bba02
|
4
|
+
data.tar.gz: ccd3efc7a6dbaeddf2b4f57ad897d769cb1ba42c58089a6fee59d8ccded248c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1603d2802d7aab513178e4fd9769e4380afaa18ed3bbe3e0ec8849ca523629695173d9dd28df939680a032d6f9727c2824122edfd7dd59c70ee5510acf3e7c0
|
7
|
+
data.tar.gz: 26c7583a6da8c1cf0c61834ecafbc1352f982b123eb9d36f482bdfefe7a4e5fefc24146b7ca4de0327a586c5dbd91ccce18d68ec89cf5c9728a3d4a6693cc060
|
data/lib/ant/core.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ant/logger'
|
4
|
+
|
5
|
+
module Ant
|
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 Ant::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 => ex
|
30
|
+
raise unless @retry_on_failure
|
31
|
+
|
32
|
+
# :nocov: #
|
33
|
+
log_error('Unexpected error', error: ex)
|
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 Ant
|
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 Ant
|
4
|
+
module Exceptions
|
5
|
+
autoload(:HTTP, 'ant/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 Ant
|
6
|
+
module Exceptions
|
7
|
+
# Provides basic execptions for http common errors.
|
8
|
+
module HTTP
|
9
|
+
class << self
|
10
|
+
EXCEPTION_TYPES = {
|
11
|
+
success: Ant::Exceptions::AntSuccess,
|
12
|
+
fail: Ant::Exceptions::AntFail,
|
13
|
+
error: Ant::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
|
data/lib/ant/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ant-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: 2019-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ant-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
|
+
Ant::Core will be used across all the ant 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/ant/core.rb
|
150
|
+
- lib/ant/dry/daemon.rb
|
151
|
+
- lib/ant/dry/resource_injector.rb
|
152
|
+
- lib/ant/exceptions.rb
|
153
|
+
- lib/ant/http_exceptions.rb
|
154
|
+
- lib/ant/version.rb
|
155
|
+
homepage: https://github.com/tachomex/ant
|
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.0.3
|
175
|
+
signing_key:
|
176
|
+
specification_version: 4
|
177
|
+
summary: Ant framework core functionality
|
178
|
+
test_files: []
|