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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26681f16a21e628a78982e6ceb0db964e808c6db
4
- data.tar.gz: 89e2db73e4df160b41784753b4dec3fa6be4f47e
3
+ metadata.gz: 04047b0312017627aceefba58ac696f8a4f1e64f
4
+ data.tar.gz: d2e87a677bad2818399f9b837c09a40dc12c36c5
5
5
  SHA512:
6
- metadata.gz: 41f35beb21037d9839ca341d2b5bebecd160d00289500f1b1fc59275c6d5c11a3007aa2ba6dc433f8539cd2d2ac650f15e7209f544693da6b5565ddcc457dc20
7
- data.tar.gz: f7a21459b0ff201966778c9eb0e295538e1aa728b9e57c884f69d05f54d3a33dfb09096f6736a26738081031038fb1ceb8a2d8c6c6d04a1caedf664efc15ba2c
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
- 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
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
- # 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
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
- # 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
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
- # 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
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
- # 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
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
@@ -1,29 +1,40 @@
1
1
  require 'time'
2
2
 
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
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
- # 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
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
@@ -1,5 +1,8 @@
1
- class MegliHelper
2
- end
3
-
1
+ require 'helpers/assert_helper'
4
2
  require 'helpers/common_helper'
5
3
  require 'helpers/time_helper'
4
+
5
+ module MegliHelper
6
+ class << self
7
+ end
8
+ end
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.3
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