megli_common_helper 1.0.3 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/helpers/assert_helper.rb +21 -0
- data/lib/helpers/common_helper.rb +53 -51
- data/lib/helpers/time_helper.rb +34 -23
- data/lib/megli_helper.rb +6 -3
- 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: 04047b0312017627aceefba58ac696f8a4f1e64f
|
4
|
+
data.tar.gz: d2e87a677bad2818399f9b837c09a40dc12c36c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7836de0c003887452b08aa39243c3549f0f63262dfbbad9ef8b14180f2ca4cde78ed837b2a497cddff76276c920a596e2c79d9809faa4f066a5215f09a9e8c05
|
7
|
+
data.tar.gz: bf657429923916e875cc5e842f064ad4bc20a5c15f4a91c1b324e9ea93f28a03949d0d1aec4cf046b783c21fe30804d3be501f6a2332e7933319adad3c25a6a1
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
module MegliHelper
|
4
|
+
class AssertHelper
|
5
|
+
# Assert a range of time according to min and max range
|
6
|
+
# @param time to be compared
|
7
|
+
# @param time_base to be a base in the comparison
|
8
|
+
# @param min_range to decrease the base time
|
9
|
+
# @param max_range to increase the base time
|
10
|
+
# If time is not inside the ranges, then it will raise an exception to fail the test.
|
11
|
+
def self.assert_time_range(time, time_base, min_range, max_range)
|
12
|
+
time = Time.parse(time) if time.class == String
|
13
|
+
time_base = Time.parse(time_base) if time_base.class == String
|
14
|
+
time_base ||= Time.now
|
15
|
+
min = (time_base - 60 * min_range)
|
16
|
+
max = (time_base + 60 * max_range)
|
17
|
+
raise("The time #{time} is not inside the range #{min} and #{max}") if time < min || time > max
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,62 +1,64 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'timeout'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
module MegliHelper
|
5
|
+
class CommonHelper
|
6
|
+
# Check if text is null or empty
|
7
|
+
# @param text [String] text to be checked
|
8
|
+
# @return [Boolean] true if text is nil or empty and false if not
|
9
|
+
def self.nil_or_empty?(text)
|
10
|
+
(text.nil? || text.to_s.empty? || text.empty?)
|
11
|
+
rescue
|
12
|
+
false
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
# Convert string value to boolean
|
16
|
+
# @param value [String] value to be converted
|
17
|
+
# @return [Boolean][nil] if string is not a valid boolean it will return nil
|
18
|
+
def self.to_boolean(value)
|
19
|
+
return false if nil_or_empty? value
|
20
|
+
return true if %w(true 1 y yes).include? value.to_s.downcase.strip
|
21
|
+
return false if %w(false 0 n no).include? value.to_s.downcase.strip
|
22
|
+
false
|
23
|
+
rescue
|
24
|
+
false
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
27
|
+
# Transform float value to a decimal formatted
|
28
|
+
# @param decimal [Float] value to be formatted
|
29
|
+
# @param decimal_format formmated used to format the value
|
30
|
+
# @param currency [String] currency signal to be included in the decimal like: $, £, R$, etc.
|
31
|
+
# @param currency_before [Boolean] where the currency must be included (before or after decimal)
|
32
|
+
# @return [String] decimal formatted
|
33
|
+
def self.to_decimal(decimal, decimal_format = '%8.2f', currency = nil, currency_before = true)
|
34
|
+
new_decimal = (decimal_format % decimal).strip
|
35
|
+
unless nil_or_empty?(currency)
|
36
|
+
return currency + new_decimal if currency_before
|
37
|
+
return new_decimal + currency
|
38
|
+
end
|
39
|
+
new_decimal
|
37
40
|
end
|
38
|
-
new_decimal
|
39
|
-
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
42
|
+
# Check if the JSON passed is valid or not
|
43
|
+
# @param json [String] json to be verified
|
44
|
+
# @return [Boolean] is valid or not
|
45
|
+
def self.valid_json?(json)
|
46
|
+
return false if nil_or_empty? json
|
47
|
+
JSON.parse(json)
|
48
|
+
true
|
49
|
+
rescue
|
50
|
+
false
|
51
|
+
end
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
# Wait until a condition passed happen
|
54
|
+
# @param condition [Proc] condition block to be validated
|
55
|
+
# @param condition_status [Boolean] condition status to be used to define if condition is satisfied or not
|
56
|
+
# @param timeout [Integer] timeout to wait for the condition happen
|
57
|
+
# @param sleep_for [Float] value used to call the condition block
|
58
|
+
def self.wait_until_condition(condition, condition_status = true, timeout = 20, sleep_for = 0.25)
|
59
|
+
Timeout.timeout(timeout) do
|
60
|
+
sleep sleep_for until condition.call == condition_status
|
61
|
+
end
|
60
62
|
end
|
61
63
|
end
|
62
64
|
end
|
data/lib/helpers/time_helper.rb
CHANGED
@@ -1,29 +1,40 @@
|
|
1
1
|
require 'time'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
module MegliHelper
|
4
|
+
class TimeHelper
|
5
|
+
# Change timezone of a time passed
|
6
|
+
# @param time [Time][String] time to be changed
|
7
|
+
# @param timezone [String] timezone to be set in the time
|
8
|
+
# @return [Time] time with a new timezone
|
9
|
+
def self.change_timezone(time, timezone = '+00:00')
|
10
|
+
time = Time.parse(time) if time.class == String
|
11
|
+
time.localtime(timezone)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Format a time according to format passed by param
|
15
|
+
# @param time [Time | String] time to be formatted
|
16
|
+
# @param format [String] format used
|
17
|
+
# @return [String] time formatted
|
18
|
+
def self.fortmat_date(time, format)
|
19
|
+
time = Time.parse(time) if time.class == String
|
20
|
+
time.strftime(format)
|
21
|
+
end
|
12
22
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
# Get time now with addinional minutes and convert it to format passed
|
24
|
+
# @param format [Symbol] time format. Default value: :iso8601
|
25
|
+
# @param add_minutes [Integer] quantity of minutes to sum
|
26
|
+
# @return [String] time formatted
|
27
|
+
def self.get_time(format = :iso8601, add_minutes = 0)
|
28
|
+
return nil if add_minutes.nil?
|
29
|
+
add_minutes = add_minutes.to_i if add_minutes.class == String
|
30
|
+
time = Time.now + (60 * add_minutes)
|
31
|
+
unless format.nil?
|
32
|
+
time = time.iso8601 if format == :iso8601
|
33
|
+
time = time.utc if format == :utc
|
34
|
+
time = time.rfc822 if format == :rfc822 || format == :rfc2822
|
35
|
+
time = time.ctime if format == :ctime
|
36
|
+
end
|
37
|
+
time
|
26
38
|
end
|
27
|
-
time
|
28
39
|
end
|
29
40
|
end
|
data/lib/megli_helper.rb
CHANGED
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduardo Gomes Heinen
|
@@ -16,6 +16,7 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- lib/helpers/assert_helper.rb
|
19
20
|
- lib/helpers/common_helper.rb
|
20
21
|
- lib/helpers/time_helper.rb
|
21
22
|
- lib/megli_helper.rb
|