k_util 0.0.19 → 0.0.25

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
  SHA256:
3
- metadata.gz: f49e3c0ac1db1cfaf2c310205d474b0e2c24c322fad152a172bed137f7f31d2f
4
- data.tar.gz: 2029369204a126de193b01e0219e4b37dc3e0e1745b45cdde01cdc2a159a58cf
3
+ metadata.gz: dcac547458215aa41518a6d6c5fa4ecfd5ef310219cfc5a09d371a5081e5c542
4
+ data.tar.gz: 5ab42859a7c3e72ad2a9f9a8309562dfa6a49259ef8ac9815723e3e91ae65f7f
5
5
  SHA512:
6
- metadata.gz: 3f0d2550416e6bd606a107b353cc7e2226f59cf22375fac03afac4f59306a2a40622af2a27d0ea0f5fe31577318cd6c39a671b26bd8503d5c8f1c31fe5622fe2
7
- data.tar.gz: 63a7cef3672ed4b80a38547272cce5564b68ae795b38d1972ba4d9f2d82ccb16006c96ee58f1ccdf5626f56608e3c45a78609953add05ae32747ba11d7ae3fde
6
+ metadata.gz: 483f2ef0d30165c68f5cad6cd0f1acd199a07ac44fedace7ffe5195c581159f020bb5847a6f009c4a732fac51f8d80c550352ebb4c46430e64521f7b99de4ecb
7
+ data.tar.gz: de0d1b1776acc57acc51f981dce2ac8c1c4b3a0504db0f4a1c18ba7b49fc6344080aed9532d93ac515d9bb141c60f202ec3d5e8638d82fe33c5b185cc1b98903
@@ -6,22 +6,27 @@ module KUtil
6
6
  class DataHelper
7
7
  # Convert JSON string into to_open_struct but designed to work with a JSON string
8
8
  #
9
+ # KUtil.data.parse_json(json, as: :symbolize_keys)
9
10
  # https://docs.ruby-lang.org/en/master/JSON.html
10
11
  # rubocop:disable Naming/MethodParameterName
11
- def json_parse(json, as: :hash)
12
+ def parse_json(json, as: :hash)
13
+ log.block(%i[hash hash_symbolized open_struct], title: 'Help as: ?') if as == :help
14
+
12
15
  case as
13
16
  when :hash
14
17
  JSON.parse(json)
15
- when :hash_symbolized
18
+ when :hash_symbolized, :symbolize_names, :symbolize_keys
16
19
  JSON.parse(json, symbolize_names: true)
17
20
  when :open_struct
18
21
  JSON.parse(json, object_class: OpenStruct)
19
22
  end
20
23
  end
24
+ alias json_parse parse_json
21
25
  # rubocop:enable Naming/MethodParameterName
22
26
 
23
27
  # Convert various data types (Hash, Array, Struct) into a deep nested OpenStruct
24
28
  #
29
+ # KUtil.data.to_open_struct(data)
25
30
  # or an array of deep nested OpenStruct
26
31
  def to_open_struct(data)
27
32
  return OpenStruct.new(data.transform_values { |v| to_open_struct(v) }) if data.is_a?(Hash)
@@ -34,7 +39,9 @@ module KUtil
34
39
 
35
40
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
36
41
  # Convert data to hash and deal with mixed data types such as Struct and OpenStruct
42
+ # KUtil.data.to_hash(data)
37
43
  def to_hash(data)
44
+ # This nil check is only for the root object
38
45
  return {} if data.nil?
39
46
 
40
47
  return data.map { |value| hash_convertible?(value) ? to_hash(value) : value } if data.is_a?(Array)
@@ -53,19 +60,41 @@ module KUtil
53
60
  value.is_a?(Symbol) ? value.to_s : value
54
61
  end
55
62
 
56
- # Add if needed
57
- # # Is the value a basic (aka primitive) type
58
- # def basic_type?(value)
59
- # value.is_a?(String) ||
60
- # value.is_a?(Symbol) ||
61
- # value.is_a?(FalseClass) ||
62
- # value.is_a?(TrueClass) ||
63
- # value.is_a?(Integer) ||
64
- # value.is_a?(Float)
63
+ def deep_symbolize_keys(input)
64
+ return input if input.nil?
65
+
66
+ return input unless input.is_a?(Hash)
67
+
68
+ input.each_with_object({}) do |key_value, new_hash|
69
+ key, value = key_value
70
+ value = deep_symbolize_keys(value) if value.is_a?(Hash)
71
+ value = value.map { |v| deep_symbolize_keys(v) } if value.is_a?(Array)
72
+ new_hash[key.to_sym] = value
73
+ end
74
+ end
75
+
76
+ # def deep_symbolize_keys(hash)
77
+ # return hash if hash.nil?
78
+
79
+ # hash.transform_keys(&:to_sym)
65
80
  # end
66
81
 
82
+ # Is the value a basic (aka primitive) type
83
+ def basic_type?(value)
84
+ value.is_a?(String) ||
85
+ value.is_a?(Symbol) ||
86
+ value.is_a?(FalseClass) ||
87
+ value.is_a?(TrueClass) ||
88
+ value.is_a?(Integer) ||
89
+ value.is_a?(Float)
90
+ end
91
+
67
92
  # Is the value a complex container type, but not a regular class.
68
93
  def hash_convertible?(value)
94
+ # Nil is a special case, it responds to :to_h but generally
95
+ # you only want to convert nil to {} in specific scenarios
96
+ return false if value.nil?
97
+
69
98
  value.is_a?(Array) ||
70
99
  value.is_a?(Hash) ||
71
100
  value.is_a?(Struct) ||
@@ -26,5 +26,12 @@ module KUtil
26
26
  def home_or_absolute?(path)
27
27
  home?(path) || absolute?(path)
28
28
  end
29
+
30
+ def parse_uri(uri)
31
+ return uri if uri.is_a?(URI)
32
+ return URI.parse(uri) if uri =~ URI::ABS_URI # https://stackoverflow.com/questions/1805761/how-to-check-if-a-url-is-valid
33
+
34
+ URI.join('file:///', uri)
35
+ end
29
36
  end
30
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KUtil
4
- VERSION = '0.0.19'
4
+ VERSION = '0.0.25'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k_util
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.19
4
+ version: 0.0.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-16 00:00:00.000000000 Z
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " KUtil provides simple utility methods, such as file helpers, data
14
14
  object helpers and more.\n"