megli_common_helper 1.0.0 → 1.0.3
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 +4 -4
- data/lib/helpers/common_helper.rb +53 -58
- data/lib/helpers/time_helper.rb +24 -29
- data/lib/megli_helper.rb +5 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26681f16a21e628a78982e6ceb0db964e808c6db
|
4
|
+
data.tar.gz: 89e2db73e4df160b41784753b4dec3fa6be4f47e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41f35beb21037d9839ca341d2b5bebecd160d00289500f1b1fc59275c6d5c11a3007aa2ba6dc433f8539cd2d2ac650f15e7209f544693da6b5565ddcc457dc20
|
7
|
+
data.tar.gz: f7a21459b0ff201966778c9eb0e295538e1aa728b9e57c884f69d05f54d3a33dfb09096f6736a26738081031038fb1ceb8a2d8c6c6d04a1caedf664efc15ba2c
|
@@ -1,67 +1,62 @@
|
|
1
|
-
|
1
|
+
require 'json'
|
2
|
+
require 'timeout'
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
def self.nil_or_empty?(text)
|
13
|
-
(text.nil? || text.to_s.empty? || text.empty?)
|
14
|
-
rescue
|
15
|
-
false
|
16
|
-
end
|
4
|
+
class MegliHelper::CommonHelper
|
5
|
+
# Check if text is null or empty
|
6
|
+
# @param text [String] text to be checked
|
7
|
+
# @return [Boolean] true if text is nil or empty and false if not
|
8
|
+
def self.nil_or_empty?(text)
|
9
|
+
(text.nil? || text.to_s.empty? || text.empty?)
|
10
|
+
rescue
|
11
|
+
false
|
12
|
+
end
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
# Convert string value to boolean
|
15
|
+
# @param value [String] value to be converted
|
16
|
+
# @return [Boolean][nil] if string is not a valid boolean it will return nil
|
17
|
+
def self.to_boolean(value)
|
18
|
+
return false if nil_or_empty? value
|
19
|
+
return true if %w(true 1 y yes).include? value.to_s.downcase.strip
|
20
|
+
return false if %w(false 0 n no).include? value.to_s.downcase.strip
|
21
|
+
false
|
22
|
+
rescue
|
23
|
+
false
|
24
|
+
end
|
29
25
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
new_decimal
|
26
|
+
# Transform float value to a decimal formatted
|
27
|
+
# @param decimal [Float] value to be formatted
|
28
|
+
# @param decimal_format formmated used to format the value
|
29
|
+
# @param currency [String] currency signal to be included in the decimal like: $, £, R$, etc.
|
30
|
+
# @param currency_before [Boolean] where the currency must be included (before or after decimal)
|
31
|
+
# @return [String] decimal formatted
|
32
|
+
def self.to_decimal(decimal, decimal_format = '%8.2f', currency = nil, currency_before = true)
|
33
|
+
new_decimal = (decimal_format % decimal).strip
|
34
|
+
unless nil_or_empty?(currency)
|
35
|
+
return currency + new_decimal if currency_before
|
36
|
+
return new_decimal + currency
|
43
37
|
end
|
38
|
+
new_decimal
|
39
|
+
end
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
41
|
+
# Check if the JSON passed is valid or not
|
42
|
+
# @param json [String] json to be verified
|
43
|
+
# @return [Boolean] is valid or not
|
44
|
+
def self.valid_json?(json)
|
45
|
+
return false if nil_or_empty? json
|
46
|
+
JSON.parse(json)
|
47
|
+
true
|
48
|
+
rescue
|
49
|
+
false
|
50
|
+
end
|
55
51
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
52
|
+
# Wait until a condition passed happen
|
53
|
+
# @param condition [Proc] condition block to be validated
|
54
|
+
# @param condition_status [Boolean] condition status to be used to define if condition is satisfied or not
|
55
|
+
# @param timeout [Integer] timeout to wait for the condition happen
|
56
|
+
# @param sleep_for [Float] value used to call the condition block
|
57
|
+
def self.wait_until_condition(condition, condition_status = true, timeout = 20, sleep_for = 0.25)
|
58
|
+
Timeout.timeout(timeout) do
|
59
|
+
sleep sleep_for until condition.call == condition_status
|
65
60
|
end
|
66
61
|
end
|
67
62
|
end
|
data/lib/helpers/time_helper.rb
CHANGED
@@ -1,34 +1,29 @@
|
|
1
|
-
|
2
|
-
require 'time'
|
3
|
-
class TimeHelper
|
1
|
+
require 'time'
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
3
|
+
class MegliHelper::TimeHelper
|
4
|
+
# Change timezone of a time passed
|
5
|
+
# @param time [Time][String] time to be changed
|
6
|
+
# @param timezone [String] timezone to be set in the time
|
7
|
+
# @return [Time] time with a new timezone
|
8
|
+
def self.change_timezone(time, timezone = '+00:00')
|
9
|
+
time = Time.parse(time) if time.class == String
|
10
|
+
time.localtime(timezone)
|
11
|
+
end
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
time
|
13
|
+
# Get time now with addinional minutes and convert it to format passed
|
14
|
+
# @param format [Symbol] time format. Default value: :iso8601
|
15
|
+
# @param add_minutes [Integer] quantity of minutes to sum
|
16
|
+
# @return [String] time formatted
|
17
|
+
def self.get_time(format = :iso8601, add_minutes = 0)
|
18
|
+
return nil if add_minutes.nil?
|
19
|
+
add_minutes = add_minutes.to_i if add_minutes.class == String
|
20
|
+
time = Time.now + (60 * add_minutes)
|
21
|
+
unless format.nil?
|
22
|
+
time = time.iso8601 if format == :iso8601
|
23
|
+
time = time.utc if format == :utc
|
24
|
+
time = time.rfc822 if format == :rfc822 || format == :rfc2822
|
25
|
+
time = time.ctime if format == :ctime
|
31
26
|
end
|
32
|
-
|
27
|
+
time
|
33
28
|
end
|
34
29
|
end
|
data/lib/megli_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: megli_common_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduardo Gomes Heinen
|
@@ -18,6 +18,7 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- lib/helpers/common_helper.rb
|
20
20
|
- lib/helpers/time_helper.rb
|
21
|
+
- lib/megli_helper.rb
|
21
22
|
homepage: http://rubygems.org/gems/megli-common-helper
|
22
23
|
licenses:
|
23
24
|
- MIT
|