jsontocsv_MJG 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c1906fb718835c22d57d0dd870ead598c4991e6962eee7c2eeb3bb44d23ac9b2
4
+ data.tar.gz: 65e09aecf25eed05cdde0f9d6fa2b6183c612939504194a9c2aa9717749b62cf
5
+ SHA512:
6
+ metadata.gz: 6fc7e9809253ccdbd00e9e14e42e27f58b2ce74964ea027304894f8ffc52f0ac4175abda67435e0641af2087883d681e89a86d2cf317c3c7aa39b844804bda34
7
+ data.tar.gz: b94dae69cf6a601ba37b764b7d39a7358b5e1d0a63544bfd2829c28c9e562a72f3000ea121d385b3693dfc46981ab64ce1a550a94d1ac1587a5bc6e350e7ff89
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 3.0.3
6
+ before_install: gem install bundler -v 2.2.33
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-12-20
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at salaoui@onestock-retail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in jsontocsv.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Salah Alaoui
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Jsontocsv
2
+
3
+ This Ruby lib aims at converting JSON files composed of arrays of objects (all following the same schema) into CSV files where one line equals one object.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'jsontocsv'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install jsontocsv
20
+
21
+ ## Usage
22
+ ### From the console
23
+
24
+ $ jsontocsv convert data/users.json
25
+
26
+ This will convert your JSON into a CSV file, generated right in the directory from which your calling the method.
27
+
28
+ ### From your ruby file
29
+ ```ruby
30
+ require 'jsontocsv'
31
+
32
+ JsonToCsv::Convert.convert('your_file.json')
33
+ ```
34
+ This will convert your JSON into a CSV file, generated right in the directory from which your calling the method.
35
+
36
+
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/salahalaoui/jsontocsv.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "jsontocsv"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/jsontocsv ADDED
@@ -0,0 +1,5 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative '../lib/jsontocsv/lib'
4
+ require_relative '../lib/jsontocsv/version'
5
+ require_relative '../lib/jsontocsv/cli/cli'
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ [
2
+ {
3
+ "id": 0,
4
+ "email": "colleengriffith@quintity.com"
5
+ },
6
+ {
7
+ "id": 1,
8
+ "email": "maryellengriffin@ginkle.com"
9
+ }
10
+ ]
@@ -0,0 +1,3 @@
1
+ id;email
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
@@ -0,0 +1,10 @@
1
+ [
2
+ {
3
+ "key1": 0,
4
+ "key2": "colleengriffith@quintity.com"
5
+ },
6
+ {
7
+ "key1": 1,
8
+ "key2": "maryellengriffin@ginkle.com"
9
+ }
10
+ ]
@@ -0,0 +1,3 @@
1
+ key1;key2
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
data/data/output.csv ADDED
@@ -0,0 +1,3 @@
1
+ id;email
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
data/data/users.json ADDED
@@ -0,0 +1,188 @@
1
+ [
2
+ {
3
+ "id": 0,
4
+ "email": "colleengriffith@quintity.com",
5
+ "tags": [
6
+ "consectetur",
7
+ "quis"
8
+ ],
9
+ "pro.files": {
10
+ "facebook": {
11
+ "id": 0,
12
+ "picture": "//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg"
13
+ },
14
+ "twitter": {
15
+ "id": 0,
16
+ "picture": "//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg"
17
+ }
18
+ }
19
+ },
20
+ {
21
+ "id": 1,
22
+ "email": "maryellengriffin@ginkle.com",
23
+ "tags": [
24
+ "veniam",
25
+ "elit",
26
+ "mollit"
27
+ ],
28
+ "pro.files": {
29
+ "facebook": {
30
+ "id": 1,
31
+ "picture": "//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg"
32
+ },
33
+ "twitter": {
34
+ "id": 1,
35
+ "picture": "//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg"
36
+ }
37
+ }
38
+ },
39
+ {
40
+ "id": 2,
41
+ "email": "hesterherman@exozent.com",
42
+ "tags": [
43
+ "consectetur",
44
+ "et",
45
+ "irure"
46
+ ],
47
+ "pro.files": {
48
+ "facebook": {
49
+ "id": 2,
50
+ "picture": "//fbcdn.com/412a957a-6387-44a5-91ac-3cad3cc2d8c1.jpg"
51
+ },
52
+ "twitter": {
53
+ "id": 2,
54
+ "picture": "//twcdn.com/4731911c-bd25-43fa-a152-877cd65f1ecb.jpg"
55
+ }
56
+ }
57
+ },
58
+ {
59
+ "id": 3,
60
+ "email": "romeromarks@exovent.com",
61
+ "tags": [
62
+ "sint",
63
+ "laboris",
64
+ "ad"
65
+ ],
66
+ "pro.files": {
67
+ "facebook": {
68
+ "id": 3,
69
+ "picture": "//fbcdn.com/557209e7-d617-4483-ac77-013690f58b10.jpg"
70
+ },
71
+ "twitter": {
72
+ "id": 3,
73
+ "picture": "//twcdn.com/56c4683a-78f4-4981-9497-bd8d254d09ff.jpg"
74
+ }
75
+ }
76
+ },
77
+ {
78
+ "id": 4,
79
+ "email": "marlamartin@stucco.com",
80
+ "tags": [
81
+ "id",
82
+ "voluptate"
83
+ ],
84
+ "pro.files": {
85
+ "facebook": {
86
+ "id": 4,
87
+ "picture": "//fbcdn.com/1570cb6b-9384-411f-83bc-d933dc5d114d.jpg"
88
+ },
89
+ "twitter": {
90
+ "id": 4,
91
+ "picture": "//twcdn.com/aa247d25-a088-40f5-94ca-4ab6c90206e2.jpg"
92
+ }
93
+ }
94
+ },
95
+ {
96
+ "id": 5,
97
+ "email": "weekschase@maxemia.com",
98
+ "tags": [
99
+ "ullamco",
100
+ "consequat",
101
+ "do"
102
+ ],
103
+ "pro.files": {
104
+ "facebook": {
105
+ "id": 5,
106
+ "picture": "//fbcdn.com/c1ee744b-7962-48d3-b8fd-96ca55425ea0.jpg"
107
+ },
108
+ "twitter": {
109
+ "id": 5,
110
+ "picture": "//twcdn.com/c13d7066-1c78-480e-aa46-aa85b2ab3e48.jpg"
111
+ }
112
+ }
113
+ },
114
+ {
115
+ "id": 6,
116
+ "email": "grahamrichardson@marketoid.com",
117
+ "tags": [
118
+ "fugiat",
119
+ "sint",
120
+ "voluptate"
121
+ ],
122
+ "pro.files": {
123
+ "facebook": {
124
+ "id": 6,
125
+ "picture": "//fbcdn.com/67741ba9-53fb-4bd4-bb42-516ab8f3e1bd.jpg"
126
+ },
127
+ "twitter": {
128
+ "id": 6,
129
+ "picture": "//twcdn.com/d141e08e-ed47-46dd-bdba-6c634b903f7e.jpg"
130
+ }
131
+ }
132
+ },
133
+ {
134
+ "id": 7,
135
+ "email": "mendozavalentine@entality.com",
136
+ "tags": [
137
+ "nulla",
138
+ "sint"
139
+ ],
140
+ "pro.files": {
141
+ "facebook": {
142
+ "id": 7,
143
+ "picture": "//fbcdn.com/4ca3087b-3c36-4f33-8ced-85996c44a4b0.jpg"
144
+ },
145
+ "twitter": {
146
+ "id": 7,
147
+ "picture": "//twcdn.com/243d581a-4376-4ce6-80c1-12352856b5bf.jpg"
148
+ }
149
+ }
150
+ },
151
+ {
152
+ "id": 8,
153
+ "email": "wademcleod@updat.com",
154
+ "tags": [
155
+ "fugiat",
156
+ "duis",
157
+ "exercitation"
158
+ ],
159
+ "pro.files": {
160
+ "facebook": {
161
+ "id": 8,
162
+ "picture": "//fbcdn.com/b05115ad-fec0-4635-a8b5-af046805d2c9.jpg"
163
+ },
164
+ "twitter": {
165
+ "id": 8,
166
+ "picture": "//twcdn.com/3a73caa1-ae33-4527-bcc9-4d3b274c1ece.jpg"
167
+ }
168
+ }
169
+ },
170
+ {
171
+ "id": 9,
172
+ "email": "bakerwatson@geekosis.com",
173
+ "tags": [
174
+ "Lorem",
175
+ "cupidatat"
176
+ ],
177
+ "pro.files": {
178
+ "facebook": {
179
+ "id": 9,
180
+ "picture": "//fbcdn.com/548c4633-4f74-4803-ab19-e3cb5acd526d.jpg"
181
+ },
182
+ "twitter": {
183
+ "id": 9,
184
+ "picture": "//twcdn.com/f7a1e1d1-f96d-4274-9469-a99313bac94e.jpg"
185
+ }
186
+ }
187
+ }
188
+ ]
@@ -0,0 +1,11 @@
1
+ id,email,tags,pro.files.facebook.id,pro.files.facebook.picture,pro.files.twitter.id,pro.files.twitter.picture
2
+ 0,colleengriffith@quintity.com,"consectetur,quis",0,//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg,0,//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg
3
+ 1,maryellengriffin@ginkle.com,"veniam,elit,mollit",1,//fbcdn.com/12e070e0-21ea-4663-97d0-46bc9c7b67a4.jpg,1,//twcdn.com/3057792f-5dfb-4c4b-86b5-cce4d6bbf7ac.jpg
4
+ 2,hesterherman@exozent.com,"consectetur,et,irure",2,//fbcdn.com/412a957a-6387-44a5-91ac-3cad3cc2d8c1.jpg,2,//twcdn.com/4731911c-bd25-43fa-a152-877cd65f1ecb.jpg
5
+ 3,romeromarks@exovent.com,"sint,laboris,ad",3,//fbcdn.com/557209e7-d617-4483-ac77-013690f58b10.jpg,3,//twcdn.com/56c4683a-78f4-4981-9497-bd8d254d09ff.jpg
6
+ 4,marlamartin@stucco.com,"id,voluptate",4,//fbcdn.com/1570cb6b-9384-411f-83bc-d933dc5d114d.jpg,4,//twcdn.com/aa247d25-a088-40f5-94ca-4ab6c90206e2.jpg
7
+ 5,weekschase@maxemia.com,"ullamco,consequat,do",5,//fbcdn.com/c1ee744b-7962-48d3-b8fd-96ca55425ea0.jpg,5,//twcdn.com/c13d7066-1c78-480e-aa46-aa85b2ab3e48.jpg
8
+ 6,grahamrichardson@marketoid.com,"fugiat,sint,voluptate",6,//fbcdn.com/67741ba9-53fb-4bd4-bb42-516ab8f3e1bd.jpg,6,//twcdn.com/d141e08e-ed47-46dd-bdba-6c634b903f7e.jpg
9
+ 7,mendozavalentine@entality.com,"nulla,sint",7,//fbcdn.com/4ca3087b-3c36-4f33-8ced-85996c44a4b0.jpg,7,//twcdn.com/243d581a-4376-4ce6-80c1-12352856b5bf.jpg
10
+ 8,wademcleod@updat.com,"fugiat,duis,exercitation",8,//fbcdn.com/b05115ad-fec0-4635-a8b5-af046805d2c9.jpg,8,//twcdn.com/3a73caa1-ae33-4527-bcc9-4d3b274c1ece.jpg
11
+ 9,bakerwatson@geekosis.com,"Lorem,cupidatat",9,//fbcdn.com/548c4633-4f74-4803-ab19-e3cb5acd526d.jpg,9,//twcdn.com/f7a1e1d1-f96d-4274-9469-a99313bac94e.jpg
@@ -0,0 +1,3 @@
1
+ id;email
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
@@ -0,0 +1,3 @@
1
+ key1;key2
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
@@ -0,0 +1,3 @@
1
+ id;email
2
+ 0;colleengriffith@quintity.com
3
+ 1;maryellengriffin@ginkle.com
data/jsontocsv.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+
5
+ $:.push File.expand_path("../lib", __FILE__)
6
+
7
+ require 'jsontocsv/version.rb'
8
+
9
+
10
+ Gem::Specification.new do |spec|
11
+ spec.name = "jsontocsv_MJG"
12
+ spec.version = Jsontocsv::VERSION
13
+ spec.authors = ["Salah Alaoui"]
14
+ spec.email = ["salaoui@onestock-retail.com"]
15
+
16
+ spec.summary = "Convert JSON files to CSV files"
17
+ spec.description = "This Ruby lib aims at converting JSON files composed of arrays of objects (all following the same schema) into CSV files where one line equals one object."
18
+ spec.homepage = "https://github.com/salahalaoui"
19
+ spec.license = "MIT"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ spec.files = `git ls-files`.split("\n")
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ spec.add_dependency 'gli', '~> 2.20', '>= 2.20.1'
34
+ spec.add_dependency 'json', '~> 2.6.1'
35
+ spec.add_dependency 'rake', '~> 13.0', '>= 13.0.6'
36
+
37
+ spec.add_development_dependency 'coveralls', '~> 0.7', '>= 0.7.0r'
38
+ spec.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
39
+ spec.add_development_dependency 'rubocop', '~> 0.24', '>= 0.24.0'
40
+ spec.add_development_dependency 'simplecov', '~> 0.8', '>= 0.8.2'
41
+
42
+ # For more information and examples about making a new gem, checkout our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+
3
+ require 'gli'
4
+ require 'pathname'
5
+ require 'pp'
6
+
7
+ require_relative 'shared'
8
+ require_relative '../version'
9
+
10
+ def launch(argv = ARGV)
11
+ run(argv)
12
+ end
13
+
14
+ include GLI::App
15
+
16
+ program_desc "jsontocsv #{Jsontocsv::VERSION}"
17
+
18
+ module Jsontocsv
19
+ module Cli
20
+ # CLI Application
21
+ class App
22
+ extend Jsontocsv::Cli::Shared
23
+
24
+ cmds = File.absolute_path(File.join(File.dirname(__FILE__), 'cmd'))
25
+ Dir.glob(cmds + '/*.rb').each do |file|
26
+ require file
27
+ end
28
+
29
+ program_desc 'Apollon CLI'
30
+
31
+ def main(argv = ARGV)
32
+ launch(argv)
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ launch
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+
5
+ require_relative 'shared'
6
+
7
+ require_relative 'app'
@@ -0,0 +1,23 @@
1
+ require 'gli'
2
+
3
+ include GLI::App
4
+
5
+ require_relative '../shared'
6
+ require_relative '../../convert/convert'
7
+
8
+ desc 'Convert json file to csv file'
9
+
10
+ command :convert do |c|
11
+ c.flag [:d, :delimiter], type: String, default_value: ';'
12
+
13
+ c.action do |global_options, options, args|
14
+ fail ArgumentError, 'No file to convert specified' if args.empty?
15
+
16
+ opts = {}.merge(global_options).merge(options)
17
+ Jsontocsv::Convert.convert(args, opts)
18
+
19
+ puts "done!"
20
+ end
21
+ end
22
+
23
+ # default_command :convert
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'gli'
4
+
5
+ include GLI::App
6
+
7
+ require_relative '../../version'
8
+
9
+ require_relative '../shared'
10
+
11
+ desc 'Print version info'
12
+ command :version do |c|
13
+ c.action do |_global_options, _options, _args|
14
+ pp Jsontocsv::VERSION
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'gli'
4
+
5
+ module Jsontocsv
6
+ module Cli
7
+ # Shared CLI Stuff
8
+ module Shared
9
+ extend GLI::App
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,142 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'csv'
4
+ require 'pathname'
5
+ require 'pp'
6
+ require 'json'
7
+ require_relative '../version'
8
+
9
+ module Jsontocsv
10
+ # Apollon bootstrap module
11
+ module Convert
12
+ DEFAULT_OPTIONS = {
13
+ out_path: 'out.txt',
14
+ delimiter: ','
15
+ }
16
+
17
+ class << self
18
+ def convert(paths, opts = {})
19
+ # main function to convert a list of json files in to their respective csv files
20
+ # we first for each path convert the json file into an Array of Hash then we convert the array of hash to a csv file
21
+ # Example:
22
+ # >> Jsontocsv::Convert.convert('data/one_user_1.json', {"d"=>";",
23
+ # :d=>";",
24
+ # "delimiter"=>";",
25
+ # :delimiter=>";"})
26
+ # => nil
27
+ #
28
+ # Arguments:
29
+ # paths: (String or Array of String)
30
+ # opts: (Object)
31
+ paths = [paths] unless paths.is_a?(Array)
32
+ paths.each do |path|
33
+
34
+ begin
35
+ json = load_file(path)
36
+ if json.nil?
37
+ next
38
+ end
39
+
40
+ headings = (json.length > 0) ? get_recursive_keys(json[0]) : []
41
+ headings = headings.to_a
42
+
43
+ tmp_opts = { out_path: "#{path}.csv" }
44
+ process(json, headings, DEFAULT_OPTIONS.merge(opts).merge(tmp_opts))
45
+
46
+ rescue Exception => e # rubocop:disable RescueException
47
+ log_exception(e)
48
+ end
49
+
50
+ end
51
+ end
52
+
53
+ def get_recursive_keys(hash, nested_key=[])
54
+ # get list of nested keys from a hash, return an Array of Array
55
+ # Example:
56
+ # >> nested_hash = {
57
+ # "id": 0,
58
+ # "twitter": {
59
+ # "picture": "//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg"
60
+ # }
61
+ # }
62
+ # >> Jsontocsv::Convert.get_recursive_keys(nested_hash)
63
+ # => [[:id], [:twitter, :picture]]
64
+ #
65
+ # Arguments:
66
+ # hash: Hash
67
+ # nested_key: Array of previous parents keys
68
+ hash.each_with_object([]) do |(k,v),keys|
69
+ # Col filter
70
+ next if ["score", "original_name"].include? k
71
+ k = !nested_key.empty? ? [nested_key, k].flatten : [k]
72
+ if v.is_a? Hash
73
+ keys.concat(get_recursive_keys(v, k))
74
+ else
75
+ keys << k
76
+ end
77
+ end
78
+ end
79
+
80
+ def load_file(path)
81
+ # load a json file return an array
82
+ # Example:
83
+ # >> Jsontocsv::Convert.load_file('file.json')
84
+ # => {"id": 4}
85
+ #
86
+ # Arguments:
87
+ # path: Array of previous parents keys
88
+ begin
89
+ json = JSON.parse(File.open(path).read)
90
+
91
+ if json.class == Hash
92
+ raise "file " + path + " is an hash not an array of hash"
93
+ end
94
+
95
+ return json
96
+ rescue Exception => e # rubocop:disable RescueException
97
+ log_exception(e)
98
+ end
99
+
100
+ nil
101
+ end
102
+
103
+ def log_exception(e)
104
+ # log if invalid json
105
+ puts 'Invalid json, see error.txt'
106
+ File.open('error.txt', 'wt') { |f| f.write(e.to_s) }
107
+ end
108
+
109
+ def process(json, headings, opts = DEFAULT_OPTIONS)
110
+ # output csv file
111
+ # Example:
112
+ # Jsontocsv::Convert.process([
113
+ # {"id"=>0, "email"=>"colleengriffith@quintity.com"},
114
+ # {"id"=>1, "email"=>"maryellengriffin@ginkle.com"}
115
+ # ], 'data/output.csv',[["id"],
116
+ # ["email"]], {"help"=>false,
117
+ # :help=>false,
118
+ # "d"=>";",
119
+ # :d=>";",
120
+ # :out_path=> 'data/output.csv',
121
+ # "delimiter"=>";",
122
+ # :delimiter=>";"})
123
+ # => nil
124
+ # Arguments:
125
+ # json: Array of hash
126
+ # headings: Array of headers
127
+ # opts: Hash of options
128
+ CSV.open(opts[:out_path], 'w', :col_sep => opts[:d]) do |csv|
129
+ csv << headings.map { |n| n.join('.') }
130
+ json.map{ |x|
131
+ row = headings.map do |h|
132
+ v = x.dig(*h)
133
+ v.is_a?(Array) ? v.join(',') : v
134
+ end
135
+ csv << row
136
+ }
137
+ end
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+
5
+ base = Pathname(__FILE__).dirname.expand_path
6
+
7
+ res = []
8
+
9
+ Dir.glob(base + '*.rb').each do |file|
10
+ loaded_file = require file
11
+ res << {
12
+ file: file,
13
+ loaded: loaded_file
14
+ }
15
+ end
16
+
17
+ require_relative 'convert/convert'
18
+ res
@@ -0,0 +1,4 @@
1
+
2
+ module Jsontocsv
3
+ VERSION = '0.1.0'
4
+ end
data/lib/jsontocsv.rb ADDED
@@ -0,0 +1 @@
1
+ require "jsontocsv/lib"
data/sig/jsontocsv.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Jsontocsv
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Jsontocsv do
4
+ it "CLI displays help" do
5
+ expect { system %(jsontocsv help) }
6
+ .to output(a_string_including("NAME"))
7
+ .to_stdout_from_any_process
8
+ end
9
+
10
+ it "CLI displays convert help" do
11
+ expect { system %(jsontocsv help convert) }
12
+ .to output(a_string_including("NAME"))
13
+ .to_stdout_from_any_process
14
+ end
15
+
16
+ it "CLI displays version help" do
17
+ expect { system %(jsontocsv help) }
18
+ .to output(a_string_including("NAME"))
19
+ .to_stdout_from_any_process
20
+ end
21
+
22
+ it "CLI converts file" do
23
+ expect { system %(jsontocsv convert data/users.json) }
24
+ .to output(a_string_including("done"))
25
+ .to_stdout_from_any_process
26
+ end
27
+
28
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+ require 'fileutils'
3
+
4
+ require_relative '../../lib/jsontocsv/convert/convert'
5
+ RSpec.describe Jsontocsv::Convert do
6
+ it "can load json file" do
7
+ expect(Jsontocsv::Convert.load_file('data/one_user_1.json')).to eq([
8
+ {"id"=>0, "email"=>"colleengriffith@quintity.com"},
9
+ {"id"=>1, "email"=>"maryellengriffin@ginkle.com"}
10
+ ])
11
+ end
12
+
13
+ it "can get recursive keys" do
14
+ nested_hash = {
15
+ "id": 0,
16
+ "email": "colleengriffith@quintity.com",
17
+ "tags": [
18
+ "consectetur",
19
+ "quis"
20
+ ],
21
+ "profiles": {
22
+ "facebook": {
23
+ "id": 0,
24
+ "picture": "//fbcdn.com/a2244bc1-b10c-4d91-9ce8-184337c6b898.jpg"
25
+ },
26
+ "twitter": {
27
+ "id": 0,
28
+ "picture": "//twcdn.com/ad9e8cd3-3133-423e-8bbf-0602e4048c22.jpg"
29
+ }
30
+ }
31
+ }
32
+
33
+ expect(Jsontocsv::Convert.get_recursive_keys(nested_hash)).to eq([[:id],
34
+ [:email],
35
+ [:tags],
36
+ [:"profiles", :facebook, :id],
37
+ [:"profiles", :facebook, :picture],
38
+ [:"profiles", :twitter, :id],
39
+ [:"profiles", :twitter, :picture]])
40
+ end
41
+
42
+ it "output csv file from an array of hash" do
43
+
44
+ Jsontocsv::Convert.process([
45
+ {"id"=>0, "email"=>"colleengriffith@quintity.com"},
46
+ {"id"=>1, "email"=>"maryellengriffin@ginkle.com"}
47
+ ], 'data/output.csv',[["id"],
48
+ ["email"]], {"help"=>false,
49
+ :help=>false,
50
+ "d"=>";",
51
+ :d=>";",
52
+ :out_path=> 'data/output.csv',
53
+ "delimiter"=>";",
54
+ :delimiter=>";"})
55
+ expect(FileUtils.compare_file('data/output.csv', 'data/valid_output.csv')).to be_truthy
56
+ end
57
+
58
+ it "can convert json file to csv" do
59
+
60
+ Jsontocsv::Convert.convert('data/one_user_1.json', {"help"=>false,
61
+ :help=>false,
62
+ "d"=>";",
63
+ :d=>";",
64
+ "delimiter"=>";",
65
+ :delimiter=>";"})
66
+ expect(FileUtils.compare_file('data/one_user_1.json.csv', 'data/valid_one_user_1.json.csv')).to be_truthy
67
+ end
68
+
69
+ it "can convert multiple json files to csv files" do
70
+
71
+ Jsontocsv::Convert.convert('data/one_user_1.json', {"help"=>false,
72
+ :help=>false,
73
+ "d"=>";",
74
+ :d=>";",
75
+ "delimiter"=>";",
76
+ :delimiter=>";"})
77
+ expect(FileUtils.compare_file('data/one_user_1.json.csv', 'data/valid_one_user_1.json.csv')).to be_truthy
78
+
79
+ Jsontocsv::Convert.convert('data/one_user_2.json', {"help"=>false,
80
+ :help=>false,
81
+ "d"=>";",
82
+ :d=>";",
83
+ "delimiter"=>";",
84
+ :delimiter=>";"})
85
+ expect(FileUtils.compare_file('data/one_user_2.json.csv', 'data/valid_one_user_2.json.csv')).to be_truthy
86
+ end
87
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Jsontocsv do
4
+ it "has a version number" do
5
+ expect(Jsontocsv::VERSION).not_to be nil
6
+ end
7
+
8
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jsontocsv"
4
+
5
+ RSpec.configure do |config|
6
+ # Enable flags like --only-failures and --next-failure
7
+ config.example_status_persistence_file_path = ".rspec_status"
8
+
9
+ # Disable RSpec exposing methods globally on `Module` and `main`
10
+ config.disable_monkey_patching!
11
+
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = :expect
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsontocsv_MJG
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Salah Alaoui
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.20'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.20.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.20'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.20.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 2.6.1
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 13.0.6
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '13.0'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 13.0.6
67
+ - !ruby/object:Gem::Dependency
68
+ name: coveralls
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '0.7'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 0.7.0r
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.7'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 0.7.0r
87
+ - !ruby/object:Gem::Dependency
88
+ name: rspec
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3.0'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.0
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.0.0
107
+ - !ruby/object:Gem::Dependency
108
+ name: rubocop
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '0.24'
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 0.24.0
117
+ type: :development
118
+ prerelease: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.24'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 0.24.0
127
+ - !ruby/object:Gem::Dependency
128
+ name: simplecov
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '0.8'
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 0.8.2
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '0.8'
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: 0.8.2
147
+ description: This Ruby lib aims at converting JSON files composed of arrays of objects
148
+ (all following the same schema) into CSV files where one line equals one object.
149
+ email:
150
+ - salaoui@onestock-retail.com
151
+ executables:
152
+ - console
153
+ - jsontocsv
154
+ - setup
155
+ extensions: []
156
+ extra_rdoc_files: []
157
+ files:
158
+ - ".gitignore"
159
+ - ".rspec"
160
+ - ".travis.yml"
161
+ - CHANGELOG.md
162
+ - CODE_OF_CONDUCT.md
163
+ - Gemfile
164
+ - LICENSE.txt
165
+ - README.md
166
+ - Rakefile
167
+ - bin/console
168
+ - bin/jsontocsv
169
+ - bin/setup
170
+ - data/one_user_1.json
171
+ - data/one_user_1.json.csv
172
+ - data/one_user_2.json
173
+ - data/one_user_2.json.csv
174
+ - data/output.csv
175
+ - data/users.json
176
+ - data/users.json.csv
177
+ - data/valid_one_user_1.json.csv
178
+ - data/valid_one_user_2.json.csv
179
+ - data/valid_output.csv
180
+ - jsontocsv.gemspec
181
+ - lib/jsontocsv.rb
182
+ - lib/jsontocsv/cli/app.rb
183
+ - lib/jsontocsv/cli/cli.rb
184
+ - lib/jsontocsv/cli/cmd/convert_cmd.rb
185
+ - lib/jsontocsv/cli/cmd/version_cmd.rb
186
+ - lib/jsontocsv/cli/shared.rb
187
+ - lib/jsontocsv/convert/convert.rb
188
+ - lib/jsontocsv/lib.rb
189
+ - lib/jsontocsv/version.rb
190
+ - sig/jsontocsv.rbs
191
+ - spec/cli/cli_spec.rb
192
+ - spec/convert/convert_spec.rb
193
+ - spec/jsontocsv_spec.rb
194
+ - spec/spec_helper.rb
195
+ homepage: https://github.com/salahalaoui
196
+ licenses:
197
+ - MIT
198
+ metadata: {}
199
+ post_install_message:
200
+ rdoc_options: []
201
+ require_paths:
202
+ - lib
203
+ required_ruby_version: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ required_rubygems_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ requirements: []
214
+ rubygems_version: 3.2.32
215
+ signing_key:
216
+ specification_version: 4
217
+ summary: Convert JSON files to CSV files
218
+ test_files: []