decombobulate 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f81125e8444dc639f39dbf435e38b2a29ca8fa539539347008920e2aa38923f
4
- data.tar.gz: d77582c5434901c923c30c7e88c4204c7fbac499e32cfd1b8e5a4b0812314e51
3
+ metadata.gz: a59dbe52a60fb02fb778321ca0c94e0f1f0f9dcb398b9ae4eb99d742b796ec84
4
+ data.tar.gz: 398be19030a30753ee0edbbcd903eee8db90052c149b0867c64759eb1730f273
5
5
  SHA512:
6
- metadata.gz: 00ad0e953bbaf1d7f5c5d95c596f2bfb5b61f62e64a8a5e6311d71a194eb0f088ee04178b4505e573170c95e39ed3ca9ee10e2f50e557f241fc87f561dd0a83b
7
- data.tar.gz: bbf36aeb166e52e8c8c201e3b553e67dd02401346571c12315361032a83b3d64e12a85f21b2326dd3bd84d536c9c7a87fa506bcdef237f3ee411b85c05252d55
6
+ metadata.gz: 35ff913019ae6af28e0dd76cfb70510318fd3e2dc62a696d8ebf47308ba19d097f33e1a05af4c9875e8067ed1b2214c3609d6227e12c60c1d47840c663a93f87
7
+ data.tar.gz: 63531351bf5c51a5620439a6427a050f78bc541cf8cedf296d6018f92d5bdf9374673a9de39e220919ccb57a88ae1386b6691a3ca1df6050750ffe4d4f1b40d5
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ decombobulate (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.7)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ base64 (0.1.1)
16
+ concurrent-ruby (1.2.2)
17
+ i18n (1.14.1)
18
+ concurrent-ruby (~> 1.0)
19
+ json (2.6.3)
20
+ language_server-protocol (3.17.0.3)
21
+ minitest (5.19.0)
22
+ parallel (1.23.0)
23
+ parser (3.2.2.3)
24
+ ast (~> 2.4.1)
25
+ racc
26
+ racc (1.7.1)
27
+ rack (3.0.8)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.8.1)
31
+ rexml (3.2.6)
32
+ rubocop (1.56.0)
33
+ base64 (~> 0.1.1)
34
+ json (~> 2.3)
35
+ language_server-protocol (>= 3.17.0)
36
+ parallel (~> 1.10)
37
+ parser (>= 3.2.2.3)
38
+ rainbow (>= 2.2.2, < 4.0)
39
+ regexp_parser (>= 1.8, < 3.0)
40
+ rexml (>= 3.2.5, < 4.0)
41
+ rubocop-ast (>= 1.28.1, < 2.0)
42
+ ruby-progressbar (~> 1.7)
43
+ unicode-display_width (>= 2.4.0, < 3.0)
44
+ rubocop-ast (1.29.0)
45
+ parser (>= 3.2.1.0)
46
+ rubocop-performance (1.19.0)
47
+ rubocop (>= 1.7.0, < 2.0)
48
+ rubocop-ast (>= 0.4.0)
49
+ rubocop-rails (2.20.2)
50
+ activesupport (>= 4.2.0)
51
+ rack (>= 1.1)
52
+ rubocop (>= 1.33.0, < 2.0)
53
+ ruby-progressbar (1.13.0)
54
+ tzinfo (2.0.6)
55
+ concurrent-ruby (~> 1.0)
56
+ unicode-display_width (2.4.2)
57
+
58
+ PLATFORMS
59
+ arm64-darwin-22
60
+
61
+ DEPENDENCIES
62
+ decombobulate!
63
+ minitest (~> 5.0)
64
+ rake (~> 13.0)
65
+ rubocop (~> 1.21)
66
+ rubocop-performance
67
+ rubocop-rails
68
+
69
+ BUNDLED WITH
70
+ 2.4.9
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Decombobulate
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/decombobulate.rb CHANGED
@@ -12,48 +12,92 @@ class Decombobulate
12
12
  def initialize(json)
13
13
  # Parse the JSON if we need to
14
14
  json = Json.parse(json) if json.is_a?(String)
15
+
16
+
17
+ # Yes, I tried a case/when statement but for some reason that doesn't work when looking at class types
15
18
  if json.is_a?(Array)
16
19
  @headers = parse_headers_from_json(json.first)
17
20
  @rows = json.map do |json_row|
18
- parse_json_object_into_row(json_row)
21
+ collapse_json_object_row_to_csv_row(json_row)
19
22
  end
20
- else
23
+ elsif json.is_a?(Hash)
21
24
  @headers = parse_headers_from_json(json)
22
- @rows = [parse_json_object_into_row(json)]
25
+ @rows = [collapse_json_object_row_to_csv_row(json)]
26
+ else
27
+ raise "JSON object must have an array or object as the top level object."
23
28
  end
24
29
  end
25
30
 
26
- def to_csv
27
- CSV.generate do |csv|
28
- csv << @headers
29
- @rows.each do |row|
30
- csv << row
31
+ def to_csv(file_name = nil)
32
+ if file_name.nil?
33
+ csv_final = CSV.generate(encoding: "UTF-8") do |csv|
34
+ csv << @headers
35
+ @rows.each do |row|
36
+ csv << row
37
+ end
31
38
  end
39
+ else
40
+ CSV.open(file_name, "wb", encoding: "UTF-8") do |csv|
41
+ csv << @headers
42
+ @rows.each do |row|
43
+ csv << row
44
+ end
45
+ end
46
+
47
+ return true
32
48
  end
49
+
50
+ csv_final
33
51
  end
34
52
 
35
53
  def parse_headers_from_json(json, parent_key = nil)
36
54
  @headers ||= []
37
- json.keys.each do |key|
38
- flattened_key = parent_key.nil? ? key.to_s : "#{parent_key}.#{key}"
39
55
 
40
- unless json[key].is_a?(Hash)
41
- @headers << flattened_key
42
- else
43
- parse_headers_from_json(json[key], flattened_key)
56
+ if json.is_a?(Hash)
57
+ json.keys.each do |key|
58
+ flattened_key = parent_key.nil? ? key.to_s : "#{parent_key}.#{key}"
59
+
60
+ if json[key].is_a?(Hash) || json[key].is_a?(Array)
61
+ parse_headers_from_json(json[key], flattened_key)
62
+ else
63
+ @headers << flattened_key
64
+ end
65
+ end
66
+ elsif json.is_a?(Array)
67
+ json.each do |j_object|
68
+ parse_headers_from_json(j_object, parent_key)
44
69
  end
70
+ else
71
+ # This mean's we either have a parsing error or we're in an array.
72
+ # I'm assuming the JSON coming in is valid, so we're going to just skip this
73
+ return nil
45
74
  end
46
75
 
47
76
  @headers
48
77
  end
49
78
 
50
- def parse_json_object_into_row(json)
51
- @headers.map do |header|
52
- header_array = header.split(".").map(&:to_sym)
53
- value = json.dig(*header_array)
54
- value = value.join(", ") if value.is_a?(Array)
79
+ def collapse_json_object_row_to_csv_row(json)
80
+ # Time to do a graph traversal! And recursively! Java 103 is usefuL!!!!
81
+ row_array = []
55
82
 
56
- value
83
+ if json.is_a?(Array)
84
+ json.each do |j_object|
85
+ if j_object.is_a?(Array) || j_object.is_a?(Hash)
86
+ row_array << collapse_json_object_row_to_csv_row(j_object)
87
+ else
88
+ row_array << j_object
89
+ end
90
+ end
91
+ elsif json.is_a?(Hash)
92
+ json.keys.each do |key|
93
+ if json[key].is_a?(Array) || json[key].is_a?(Hash)
94
+ row_array << collapse_json_object_row_to_csv_row(json[key])
95
+ else
96
+ row_array << json[key]
97
+ end
98
+ end
57
99
  end
100
+
101
+ row_array.flatten
58
102
  end
59
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decombobulate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Guess
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-14 00:00:00.000000000 Z
11
+ date: 2023-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -63,6 +63,7 @@ files:
63
63
  - CHANGELOG.md
64
64
  - CODE_OF_CONDUCT.md
65
65
  - Gemfile
66
+ - Gemfile.lock
66
67
  - LICENSE.txt
67
68
  - README.md
68
69
  - Rakefile
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubygems_version: 3.4.9
95
+ rubygems_version: 3.4.10
95
96
  signing_key:
96
97
  specification_version: 4
97
98
  summary: Convert JSON structures to CSVs