toolrack 0.6.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddf90d364454f44c6fa22c6922b4139ffeec29ae7d8e6522e71c23dcf0e175fc
4
- data.tar.gz: '081a85a5f8e7f8afa5082a65c9cf2462884e069728a9b28de0896aace92b9a5d'
3
+ metadata.gz: 82f8e6d4659caa4af54db81593a98505aeb00348e104bc796fe6acf4278f7ca6
4
+ data.tar.gz: a3fed1a24257ac29df830858a03a7c41c575b31407b90f8414e73324d890a094
5
5
  SHA512:
6
- metadata.gz: df0f8256b236bb56ca19e77e91b53ae1be1b909d5339f3b315b255e7f416490c7f3fec0d8443ddc7c59131475869bf75b733b29f2f52e867702bf1ee6caee061
7
- data.tar.gz: fd1ec6a7429bafec01d07386884af758270aab9df59c10ffd6fb0192294aef814a929c577a0a13816bc06499626c3bb3523487babae9730071069a644af181d4
6
+ metadata.gz: 61304df1637d1c47f49612361a26a7bed6e7b79243c5769546104a3aec5c5517c95a09e6dc8da339f92c75de932d6db9240d2ba0d01bc86d6fa0eedf6e690af0
7
+ data.tar.gz: 670b9c3ee72b20e470b1ba1f1850b58765072712b6dda0049e712cf7217244882696f4b7bb8e41195a240aa6bb8ec2cf7a87fe7ed0b81777b3303961574b3b5b
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
- Toolrack is meant to be utilities hence it is prefixed with an entity.
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 Antrapol::MyToolRack::ConditionUtils
26
+ module ToolRack::ConditionUtils
28
27
  end
29
28
  ```
30
29
 
31
30
  ```ruby
32
- module Antrapol::MyToolRack::ExceptionUtils
31
+ module ToolRack::ExceptionUtils
33
32
  end
34
33
  ```
35
34
 
36
- Besides, all the utilities so far is a module by itself, which means it is meant to be included/extended into application classes.
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
- include Antrapol::MyToolRack::ConditionUtils
42
- include Antrapol::MyToolRack::ExceptionUtils
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
- * is_empty?(obj) - I find rather effort intensive to call if not (obj.nil? or obj.empty?) for items that I want to check for validity before process. Hence this method shall test for .nil?. If the object also respond to :empty? it shall be called again. For example integer type does not support .empty?
49
- * not_empty?(obj) - Reverse of the is\_empty?(obj) above
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
- * 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.
53
- * obj is the object to test for is_empty
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
- * raise_if_false(obj, message, error) - As the name implied
57
- * raise_if_true(obj, message, error) - As the name implied
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
@@ -27,6 +31,22 @@ module Antrapol
27
31
  def is_boolean?(val)
28
32
  !!val == val
29
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?
30
50
 
31
51
  end # ConditionUtils
32
52
  end # MyToolRack
@@ -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
@@ -1,6 +1,6 @@
1
1
  module Antrapol
2
2
  module ToolRack
3
- VERSION = "0.6.1"
3
+ VERSION = "0.8.2"
4
4
  end
5
5
  end
6
6
 
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
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
 
29
29
  spec.add_dependency "tlogger", "~> 0.21"
30
+ spec.add_dependency "base58"
30
31
 
31
32
  spec.add_development_dependency "devops_helper", "~> 0.1.0"
32
33
  end
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.6.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-17 00:00:00.000000000 Z
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