decombobulate 0.1.1 → 0.1.3

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: 8f81125e8444dc639f39dbf435e38b2a29ca8fa539539347008920e2aa38923f
4
- data.tar.gz: d77582c5434901c923c30c7e88c4204c7fbac499e32cfd1b8e5a4b0812314e51
3
+ metadata.gz: 153e6f51eadf84455fdd682c8c6eb3fb986f9dfafaa833f3c60e0e491a08fc94
4
+ data.tar.gz: '08e60c60fab9867131aa075177bdd7e470e628804ab51232a1a4bc259844c6ca'
5
5
  SHA512:
6
- metadata.gz: 00ad0e953bbaf1d7f5c5d95c596f2bfb5b61f62e64a8a5e6311d71a194eb0f088ee04178b4505e573170c95e39ed3ca9ee10e2f50e557f241fc87f561dd0a83b
7
- data.tar.gz: bbf36aeb166e52e8c8c201e3b553e67dd02401346571c12315361032a83b3d64e12a85f21b2326dd3bd84d536c9c7a87fa506bcdef237f3ee411b85c05252d55
6
+ metadata.gz: 7ae3eba70539c1d56e98c69e190f1eeb6e4c726dcb3c0a2f390e80c163ef6cb95010bc04908b3fd274d3481de89e271db2f71bd8d14c0d697b13deaa201636a0
7
+ data.tar.gz: a32b6a3f54a1afb2b939470ed1b33cf29cf04b32c4f5955552e508a512d76a24832d6f7781e8c5b86c9d06839cbed7009bc9d3d16af76eab36b9555d7faed7f2
data/Gemfile.lock ADDED
@@ -0,0 +1,129 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ decombobulate (0.1.3)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (7.0.7)
10
+ actionview (= 7.0.7)
11
+ activesupport (= 7.0.7)
12
+ rack (~> 2.0, >= 2.2.4)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
16
+ actionview (7.0.7)
17
+ activesupport (= 7.0.7)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
22
+ activesupport (7.0.7)
23
+ concurrent-ruby (~> 1.0, >= 1.0.2)
24
+ i18n (>= 1.6, < 2)
25
+ minitest (>= 5.1)
26
+ tzinfo (~> 2.0)
27
+ ast (2.4.2)
28
+ base64 (0.1.1)
29
+ builder (3.2.4)
30
+ concurrent-ruby (1.2.2)
31
+ crass (1.0.6)
32
+ erubi (1.12.0)
33
+ i18n (1.14.1)
34
+ concurrent-ruby (~> 1.0)
35
+ json (2.6.3)
36
+ language_server-protocol (3.17.0.3)
37
+ loofah (2.21.3)
38
+ crass (~> 1.0.2)
39
+ nokogiri (>= 1.12.0)
40
+ method_source (1.0.0)
41
+ minitest (5.19.0)
42
+ nokogiri (1.15.4-arm64-darwin)
43
+ racc (~> 1.4)
44
+ nokogiri (1.15.4-x86_64-linux)
45
+ racc (~> 1.4)
46
+ parallel (1.23.0)
47
+ parser (3.2.2.3)
48
+ ast (~> 2.4.1)
49
+ racc
50
+ racc (1.7.1)
51
+ rack (2.2.8)
52
+ rack-test (2.1.0)
53
+ rack (>= 1.3)
54
+ rails-dom-testing (2.2.0)
55
+ activesupport (>= 5.0.0)
56
+ minitest
57
+ nokogiri (>= 1.6)
58
+ rails-html-sanitizer (1.6.0)
59
+ loofah (~> 2.21)
60
+ nokogiri (~> 1.14)
61
+ railties (7.0.7)
62
+ actionpack (= 7.0.7)
63
+ activesupport (= 7.0.7)
64
+ method_source
65
+ rake (>= 12.2)
66
+ thor (~> 1.0)
67
+ zeitwerk (~> 2.5)
68
+ rainbow (3.1.1)
69
+ rake (13.0.6)
70
+ regexp_parser (2.8.1)
71
+ rexml (3.2.6)
72
+ rubocop (1.56.0)
73
+ base64 (~> 0.1.1)
74
+ json (~> 2.3)
75
+ language_server-protocol (>= 3.17.0)
76
+ parallel (~> 1.10)
77
+ parser (>= 3.2.2.3)
78
+ rainbow (>= 2.2.2, < 4.0)
79
+ regexp_parser (>= 1.8, < 3.0)
80
+ rexml (>= 3.2.5, < 4.0)
81
+ rubocop-ast (>= 1.28.1, < 2.0)
82
+ ruby-progressbar (~> 1.7)
83
+ unicode-display_width (>= 2.4.0, < 3.0)
84
+ rubocop-ast (1.29.0)
85
+ parser (>= 3.2.1.0)
86
+ rubocop-md (1.2.0)
87
+ rubocop (>= 1.0)
88
+ rubocop-minitest (0.31.0)
89
+ rubocop (>= 1.39, < 2.0)
90
+ rubocop-packaging (0.5.2)
91
+ rubocop (>= 1.33, < 2.0)
92
+ rubocop-performance (1.19.0)
93
+ rubocop (>= 1.7.0, < 2.0)
94
+ rubocop-ast (>= 0.4.0)
95
+ rubocop-rails (2.20.2)
96
+ activesupport (>= 4.2.0)
97
+ rack (>= 1.1)
98
+ rubocop (>= 1.33.0, < 2.0)
99
+ rubocop-rails_config (1.13.0)
100
+ railties (>= 5.0)
101
+ rubocop (>= 1.48.0)
102
+ rubocop-ast (>= 1.26.0)
103
+ rubocop-md
104
+ rubocop-minitest (~> 0.22)
105
+ rubocop-packaging (~> 0.5)
106
+ rubocop-performance (~> 1.11)
107
+ rubocop-rails (~> 2.0)
108
+ ruby-progressbar (1.13.0)
109
+ thor (1.2.2)
110
+ tzinfo (2.0.6)
111
+ concurrent-ruby (~> 1.0)
112
+ unicode-display_width (2.4.2)
113
+ zeitwerk (2.6.11)
114
+
115
+ PLATFORMS
116
+ arm64-darwin-22
117
+ x86_64-linux
118
+
119
+ DEPENDENCIES
120
+ decombobulate!
121
+ minitest (~> 5.0)
122
+ rake (~> 13.0)
123
+ rubocop (~> 1.21)
124
+ rubocop-performance
125
+ rubocop-rails
126
+ rubocop-rails_config
127
+
128
+ BUNDLED WITH
129
+ 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.3"
5
5
  end
data/lib/decombobulate.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "decombobulate/version"
4
- require "JSON"
5
- require "CSV"
4
+ require "json"
5
+ require "csv"
6
6
 
7
7
  class Decombobulate
8
8
  class Error < StandardError; end
@@ -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.3
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
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-rails_config
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Convert JSON structures to CSVs
56
70
  email:
57
71
  - cguess@gmail.com
@@ -63,6 +77,7 @@ files:
63
77
  - CHANGELOG.md
64
78
  - CODE_OF_CONDUCT.md
65
79
  - Gemfile
80
+ - Gemfile.lock
66
81
  - LICENSE.txt
67
82
  - README.md
68
83
  - Rakefile
@@ -84,14 +99,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
99
  requirements:
85
100
  - - ">="
86
101
  - !ruby/object:Gem::Version
87
- version: 2.6.0
102
+ version: 2.7.0
88
103
  required_rubygems_version: !ruby/object:Gem::Requirement
89
104
  requirements:
90
105
  - - ">="
91
106
  - !ruby/object:Gem::Version
92
107
  version: '0'
93
108
  requirements: []
94
- rubygems_version: 3.4.9
109
+ rubygems_version: 3.4.10
95
110
  signing_key:
96
111
  specification_version: 4
97
112
  summary: Convert JSON structures to CSVs