megli_common_helper 1.0.0

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