toolrack 0.6.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/README.md +34 -19
- data/lib/toolrack/condition_utils.rb +24 -0
- data/lib/toolrack/data_conversion_utils.rb +120 -0
- data/lib/toolrack/version.rb +1 -1
- data/lib/toolrack.rb +2 -0
- data/toolrack.gemspec +1 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d2de080a19bcf745dd39bc1125df5cbc061ca67af65a1068847fe01c4c7af6a
|
4
|
+
data.tar.gz: 45ab47d974811ea0c491bdc592e743c2b1e5afa3f7b74b3141c3b3bf6d1fb3c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a7ebfc77d5319f7d3b768d7c27c213b858a414663f1bfa5992fa8c2ac0b8f775348a2298618cc7d84141269c383be0390c3bfe00fafe5a8c377cc67b89066c
|
7
|
+
data.tar.gz: a9a4d41057b2328f03e407d84ef1350ee8a1ad575d2c80c7b166d818c6172ec92fb338a22726acdd27dc99ab5d0098a4ccfe2f620b1d7de4a3a8a475f14ba076
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/README.md
CHANGED
@@ -20,48 +20,63 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
Therefore the two utilities are both packaged in namespace:
|
23
|
+
The utilities are those commonly being used that I think should be group together to save some effort in my other projects.
|
25
24
|
|
26
25
|
```ruby
|
27
|
-
module
|
26
|
+
module ToolRack::ConditionUtils
|
28
27
|
end
|
29
28
|
```
|
30
29
|
|
31
30
|
```ruby
|
32
|
-
module
|
31
|
+
module ToolRack::ExceptionUtils
|
33
32
|
end
|
34
33
|
```
|
35
34
|
|
36
|
-
|
35
|
+
All the utilities so far is a module by itself, which means it is meant to be included/extended into application classes.
|
37
36
|
|
38
37
|
For example:
|
39
38
|
```ruby
|
40
39
|
class EditController
|
41
|
-
|
42
|
-
include
|
40
|
+
# enable instance method to access those uitilities
|
41
|
+
include ToolRack::ConditionUtils
|
42
|
+
include ToolRack::ExceptionUtils
|
43
43
|
end
|
44
44
|
```
|
45
45
|
|
46
46
|
Currently it has 3 modules:
|
47
47
|
* Condition Utilities
|
48
|
-
*
|
49
|
-
|
48
|
+
* is\_empty?(obj) - I found that I've type the condition if not (x.nil? and x.empty?) too frequent that I think this is very best to turn this into a function. The empty test shall take the following test in sequence:
|
49
|
+
* First test is x.nil?. If it is return true
|
50
|
+
* Then test if x respond\_to? :empty?. If it is call it.
|
51
|
+
* Then test if x respond\_to? :length. If it is call it to see if the value is it 0 and return its comparison result
|
52
|
+
* Then test if x respond\_to? :size. If it is call it to see if the value is it 0 and return its comparison result
|
53
|
+
Once any of the test is successful, it will not proceed to test the subseqent tests and return its' result.
|
54
|
+
* not\_empty?(obj) - Reverse of the is\_empty?(obj) above
|
55
|
+
* is\_boolean?(obj) - Test given value is it a boolean value. Note this does not test if the obj is a string of 'true' or 'false'. Those shall return false by this API. If you want to test for string, use is\_string\_boolean?(str) or is\_str\_bool?(str)
|
56
|
+
* is\_bool?(obj) - Shorcut to is\_boolean?()
|
57
|
+
* is\_string\_boolean?(str) - Test given string is it "true" or "false". Note the comparison is case insensitive. Note that both "true" and "false" shall return with value true from this API. This API does not convert the string value it to boolean. Just a test if the string carry the boolean value. Refers data conversion section if you want to convert string to boolean
|
58
|
+
|
59
|
+
|
60
|
+
* Data Conversion Utilities
|
61
|
+
This is meant to convert data from a format to another format
|
62
|
+
* to\_b64(bin) - convert given binary to typical RFC 2045 Base64 output which includes line break in specific position
|
63
|
+
* to\_b64\_strict(bin) - Convert binary to RFC 4648 Base64 format, means there is no line break in between.
|
64
|
+
* from\_b64(str) - Convert given Base64 input into binary back. Tested this with both RFC2045 and RFC4648 Base64 output seems no issue.
|
65
|
+
* from\_b64\_strict(str) - Although seems the from\_b64 able to cater the RFC4648 output, prepare this in case...
|
66
|
+
* to\_hex(bin) - Convert binary into hex output. Note this function also convert integer into hex.
|
67
|
+
* from\_hex(str) - Convert hex string into binary
|
68
|
+
* hex\_to\_int(str) - Convert given hex string into integer. Alias method: hex\_to\_num, hex\_to\_integer, hex\_to\_number
|
69
|
+
* string\_to\_boolean(str) - Convert the string into appropriate boolean true/false. Note if the given string is not "true" or "false", nil shall be returned and can test it with is\_empty?
|
70
|
+
|
50
71
|
|
51
72
|
* Exception Utilities
|
52
|
-
*
|
53
|
-
* obj is the object to test for
|
73
|
+
* raise\_if\_empty(obj, message, error) - Extension from the is\_empty?() above, usually if it is empty, an exception shall be raised. It is just combined the conditions with raise of exception.
|
74
|
+
* obj is the object to test for is\_empty?()
|
54
75
|
* message is the one that shall be thrown with the exception.
|
55
76
|
* error is the exception type
|
56
|
-
*
|
57
|
-
*
|
58
|
-
|
59
|
-
* Process Utilities
|
60
|
-
* exec(command, options = { ), &block) - Passing in the command into the method and result is returned to caller. If options is not given shall go to basic execution which is the backtick (\`). If block empty, result of the command is returned to caller. If block is given, result (true or false of the execution of command and any result from STDOUT is call to block provider(e.g. block.call($?, result)
|
61
|
-
|
62
|
-
# Note on Process Utilities
|
77
|
+
* raise\_if\_false(obj, message, error) - As the name implied
|
78
|
+
* raise\_if\_true(obj, message, error) - As the name implied
|
63
79
|
|
64
|
-
The command is passed as it is to the operating system which includes fatal operation such as "rm -rf". Library at current stage made no filtering on system harmful command hence the caller of the library has to take extra precaution on what command is being passed into the library
|
65
80
|
|
66
81
|
|
67
82
|
|
@@ -15,6 +15,10 @@ module Antrapol
|
|
15
15
|
else
|
16
16
|
obj.empty?
|
17
17
|
end
|
18
|
+
#elsif obj.respond_to?(:length)
|
19
|
+
# obj.length == 0
|
20
|
+
#elsif obj.respond_to?(:size)
|
21
|
+
# obj.size == 0
|
18
22
|
else
|
19
23
|
false
|
20
24
|
end
|
@@ -24,6 +28,26 @@ module Antrapol
|
|
24
28
|
!is_empty?(obj)
|
25
29
|
end # not empty
|
26
30
|
|
31
|
+
def is_boolean?(val)
|
32
|
+
!!val == val
|
33
|
+
end
|
34
|
+
alias_method :is_bool?, :is_boolean?
|
35
|
+
|
36
|
+
def is_string_boolean?(str)
|
37
|
+
if not_empty?(str)
|
38
|
+
s = str.to_s.strip.downcase
|
39
|
+
case s
|
40
|
+
when "true", "false"
|
41
|
+
true
|
42
|
+
else
|
43
|
+
false
|
44
|
+
end
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
alias_method :is_str_bool?, :is_string_boolean?
|
50
|
+
|
27
51
|
end # ConditionUtils
|
28
52
|
end # MyToolRack
|
29
53
|
end # Antrapol
|
@@ -0,0 +1,120 @@
|
|
1
|
+
|
2
|
+
require 'base64'
|
3
|
+
require 'base58'
|
4
|
+
|
5
|
+
module Antrapol
|
6
|
+
module ToolRack
|
7
|
+
module DataConversionUtils
|
8
|
+
include ConditionUtils
|
9
|
+
|
10
|
+
def from_b64(str)
|
11
|
+
if not_empty?(str)
|
12
|
+
# decode operation supports both strict or non strict output
|
13
|
+
Base64.decode64(str)
|
14
|
+
else
|
15
|
+
str
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_b64(bin)
|
20
|
+
if not_empty?(bin)
|
21
|
+
# Base64 standard output which has new line in the middle of the output
|
22
|
+
Base64.encode64(bin)
|
23
|
+
else
|
24
|
+
bin
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_b64_strict(bin)
|
29
|
+
if not_empty?(bin)
|
30
|
+
# strict base64 shall have NO new line in the middle of the output
|
31
|
+
Base64.strict_encode64(bin)
|
32
|
+
else
|
33
|
+
bin
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def from_b64_strict(str)
|
38
|
+
if not_empty?(str)
|
39
|
+
Base64.strict_decode64(str)
|
40
|
+
else
|
41
|
+
str
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_b58(val)
|
46
|
+
return "" if is_empty?(val)
|
47
|
+
|
48
|
+
case val
|
49
|
+
when Integer
|
50
|
+
Base58.int_to_base58(val)
|
51
|
+
else
|
52
|
+
Base58.binary_to_base58(val.force_encoding('BINARY'))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def from_b58(str)
|
57
|
+
return "" if is_empty?(str)
|
58
|
+
Base58.base58.to_binary(str)
|
59
|
+
end
|
60
|
+
|
61
|
+
def b58_to_number(str)
|
62
|
+
return 0 if is_empty?(str)
|
63
|
+
Base58.base58_to_int(str)
|
64
|
+
end
|
65
|
+
|
66
|
+
def from_hex(str)
|
67
|
+
if not_empty?(str)
|
68
|
+
str.scan(/../).map { |x| x.hex.chr }.join
|
69
|
+
else
|
70
|
+
str
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias_method :from_hex_string, :from_hex
|
74
|
+
|
75
|
+
def hex_to_number(str)
|
76
|
+
if not_empty?(str)
|
77
|
+
str.to_i(16)
|
78
|
+
else
|
79
|
+
str
|
80
|
+
end
|
81
|
+
end
|
82
|
+
alias_method :hex_to_num, :hex_to_number
|
83
|
+
alias_method :hex_to_int, :hex_to_number
|
84
|
+
|
85
|
+
def to_hex(bin)
|
86
|
+
if not_empty?(bin)
|
87
|
+
case bin
|
88
|
+
when String
|
89
|
+
bin.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
|
90
|
+
when Integer
|
91
|
+
bin.to_s(16)
|
92
|
+
else
|
93
|
+
bin
|
94
|
+
end
|
95
|
+
else
|
96
|
+
bin
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def string_to_bool(str)
|
101
|
+
if not_empty?(str) and is_str_bool?(str)
|
102
|
+
s = str.to_s.strip.downcase
|
103
|
+
case s
|
104
|
+
when "true"
|
105
|
+
true
|
106
|
+
when "false"
|
107
|
+
false
|
108
|
+
else
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
else
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
end
|
115
|
+
alias_method :str_to_bool, :string_to_bool
|
116
|
+
alias_method :string_to_boolean, :string_to_bool
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/lib/toolrack/version.rb
CHANGED
data/lib/toolrack.rb
CHANGED
@@ -10,6 +10,7 @@ require_relative 'toolrack/exception_utils'
|
|
10
10
|
require_relative 'toolrack/condition_utils'
|
11
11
|
require_relative 'toolrack/process_utils'
|
12
12
|
require_relative 'toolrack/runtime_utils'
|
13
|
+
require_relative 'toolrack/data_conversion_utils'
|
13
14
|
|
14
15
|
module Antrapol
|
15
16
|
module ToolRack
|
@@ -20,5 +21,6 @@ module Antrapol
|
|
20
21
|
end
|
21
22
|
|
22
23
|
ToolRack = Antrapol::ToolRack
|
24
|
+
ToolRack::DataConv = Antrapol::ToolRack::DataConversionUtils
|
23
25
|
|
24
26
|
|
data/toolrack.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toolrack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tlogger
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.21'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base58
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: devops_helper
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -46,6 +60,7 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- ".gitignore"
|
63
|
+
- ".rspec"
|
49
64
|
- ".travis.yml"
|
50
65
|
- CODE_OF_CONDUCT.md
|
51
66
|
- Gemfile
|
@@ -55,6 +70,7 @@ files:
|
|
55
70
|
- bin/setup
|
56
71
|
- lib/toolrack.rb
|
57
72
|
- lib/toolrack/condition_utils.rb
|
73
|
+
- lib/toolrack/data_conversion_utils.rb
|
58
74
|
- lib/toolrack/exception_utils.rb
|
59
75
|
- lib/toolrack/global.rb
|
60
76
|
- lib/toolrack/process_utils.rb
|
@@ -86,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
102
|
- !ruby/object:Gem::Version
|
87
103
|
version: '0'
|
88
104
|
requirements: []
|
89
|
-
rubygems_version: 3.
|
105
|
+
rubygems_version: 3.2.22
|
90
106
|
signing_key:
|
91
107
|
specification_version: 4
|
92
108
|
summary: Collection of simple utilities but I find it increase clarity
|