megli_common_helper 1.0.5 → 1.1.0

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: 04047b0312017627aceefba58ac696f8a4f1e64f
4
- data.tar.gz: d2e87a677bad2818399f9b837c09a40dc12c36c5
3
+ metadata.gz: 5633127fbe2f8ab61424e02fb3ae2202998216d8
4
+ data.tar.gz: 7f2357713a86361061a2129a6c00f5126c612905
5
5
  SHA512:
6
- metadata.gz: 7836de0c003887452b08aa39243c3549f0f63262dfbbad9ef8b14180f2ca4cde78ed837b2a497cddff76276c920a596e2c79d9809faa4f066a5215f09a9e8c05
7
- data.tar.gz: bf657429923916e875cc5e842f064ad4bc20a5c15f4a91c1b324e9ea93f28a03949d0d1aec4cf046b783c21fe30804d3be501f6a2332e7933319adad3c25a6a1
6
+ metadata.gz: e2724732d56e514f0d09d9419ff6b453528c254690fe556081b10f36ead17d785ff2b27897a0ed17ae2f30ffdca544d30fdd5d035e4178bf225b05e761596ddb
7
+ data.tar.gz: 76170b8a253a75d81bf9863421193309554b8c8d0de63d6f8cccce6f3d89fc7c1fd3862e612e8573d0be847802d64b2ccd509173c3eca72a26b5cf18984373b8
data/lib/ext/string.rb ADDED
@@ -0,0 +1,13 @@
1
+ class String
2
+ # Convert a string in snake case
3
+ # Example: "testTest" => "test_test"
4
+ # @return [String]
5
+ def underscore
6
+ gsub(/::/, '/')
7
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
8
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
9
+ .tr('-', '_')
10
+ .tr(' ', '_')
11
+ .downcase
12
+ end
13
+ end
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'timeout'
3
+ require_relative '../ext/string'
3
4
 
4
5
  module MegliHelper
5
6
  class CommonHelper
@@ -39,6 +40,20 @@ module MegliHelper
39
40
  new_decimal
40
41
  end
41
42
 
43
+ # Transform keys of hash or array of hashes in snake case
44
+ # @param hash_object [Array/Hash] object that will be formatted
45
+ # @return [Array/Hash] keys converted in snake case
46
+ def self.transform_hash_keys_in_snake_case(hash_object)
47
+ case hash_object
48
+ when Array
49
+ hash_object.map { |v| transform_hash_keys_in_snake_case(v) }
50
+ when Hash
51
+ Hash[hash_object.map { |k, v| [k.to_s.underscore, transform_hash_keys_in_snake_case(v)] }]
52
+ else
53
+ hash_object
54
+ end
55
+ end
56
+
42
57
  # Check if the JSON passed is valid or not
43
58
  # @param json [String] json to be verified
44
59
  # @return [Boolean] is valid or not
data/lib/megli_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'helpers/assert_helper'
2
2
  require 'helpers/common_helper'
3
3
  require 'helpers/time_helper'
4
+ require 'ext/string'
4
5
 
5
6
  module MegliHelper
6
7
  class << self
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.5
4
+ version: 1.1.0
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/ext/string.rb
19
20
  - lib/helpers/assert_helper.rb
20
21
  - lib/helpers/common_helper.rb
21
22
  - lib/helpers/time_helper.rb