decombobulate 0.1.0 → 0.1.2

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: 3521bbe4d0c90827388594699f52a894324cfd0956062f10d53e957e36a10588
4
- data.tar.gz: 1dfadd93f3af648e0cfa4bf2a2727e487cc13f70608582a55d6e6b736c2c01d3
3
+ metadata.gz: a59dbe52a60fb02fb778321ca0c94e0f1f0f9dcb398b9ae4eb99d742b796ec84
4
+ data.tar.gz: 398be19030a30753ee0edbbcd903eee8db90052c149b0867c64759eb1730f273
5
5
  SHA512:
6
- metadata.gz: 16e8b5e90acc3ab560d90cbe70f8fb9089d8d9e199aaa217d0af44ee758d3e4a4b20eeb737bce7efef975c8228b6d3a225932b444b250847dc59aea9be511de9
7
- data.tar.gz: 478690a48d8eb10195817e920ab0e2d4b0aaa5d90f67d773d7030bd35ad7f808b00aa36015e246738b6a72f332470f471fa62190342d04ed3b47440e59505f4b
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.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/decombobulate.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require_relative "decombobulate/version"
4
4
  require "JSON"
5
5
  require "CSV"
6
- require "debug"
7
6
 
8
7
  class Decombobulate
9
8
  class Error < StandardError; end
@@ -13,48 +12,92 @@ class Decombobulate
13
12
  def initialize(json)
14
13
  # Parse the JSON if we need to
15
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
16
18
  if json.is_a?(Array)
17
19
  @headers = parse_headers_from_json(json.first)
18
20
  @rows = json.map do |json_row|
19
- parse_json_object_into_row(json_row)
21
+ collapse_json_object_row_to_csv_row(json_row)
20
22
  end
21
- else
23
+ elsif json.is_a?(Hash)
22
24
  @headers = parse_headers_from_json(json)
23
- @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."
24
28
  end
25
29
  end
26
30
 
27
- def to_csv
28
- CSV.generate do |csv|
29
- csv << @headers
30
- @rows.each do |row|
31
- 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
32
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
33
48
  end
49
+
50
+ csv_final
34
51
  end
35
52
 
36
53
  def parse_headers_from_json(json, parent_key = nil)
37
54
  @headers ||= []
38
- json.keys.each do |key|
39
- flattened_key = parent_key.nil? ? key.to_s : "#{parent_key}.#{key}"
40
55
 
41
- unless json[key].is_a?(Hash)
42
- @headers << flattened_key
43
- else
44
- 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)
45
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
46
74
  end
47
75
 
48
76
  @headers
49
77
  end
50
78
 
51
- def parse_json_object_into_row(json)
52
- @headers.map do |header|
53
- header_array = header.split(".").map(&:to_sym)
54
- value = json.dig(*header_array)
55
- 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 = []
56
82
 
57
- 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
58
99
  end
100
+
101
+ row_array.flatten
59
102
  end
60
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.0
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