fconverter 0.0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/fconverter +5 -0
  3. data/lib/fconverter.rb +51 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7cfd79420d70139a493fefb631aba77bcba6f08ecccc7b41e704175c4d4b80a
4
+ data.tar.gz: '0778c789bf898d104d75faa3ec2499411115f905721e3f0ec1130ca4d0252bda'
5
+ SHA512:
6
+ metadata.gz: 92ccb982cc2e44354d24e9f090b3154ba28785167085f7e477b84c71bc9d7bccbebacb912e8df3e4f81a1dec077124afb179e2dc6d622ce92108b29e9ee82504
7
+ data.tar.gz: 1222981c53ee4963f339dbec8338efe5945758ec550bd1eea9ad99e3874b9cf6eeb46bb7edc6ba58e22befa7ed845a3ef7f3b55f3b858d5f2decc3b1b1211375
data/bin/fconverter ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/fconverter'
4
+
5
+ ConverterCLI.start(ARGV)
data/lib/fconverter.rb ADDED
@@ -0,0 +1,51 @@
1
+ # convert.rb
2
+
3
+ require 'thor'
4
+ require 'csv'
5
+ require 'json'
6
+
7
+ def convert_csv_to_json(input_path, output_path)
8
+ json_array = []
9
+
10
+ CSV.foreach(input_path, headers: true) do |row|
11
+ json_object = {}
12
+
13
+ row.each do |header, field|
14
+ json_object[header] = field
15
+ end
16
+
17
+ json_array << json_object
18
+ end
19
+
20
+ File.write(output_path, JSON.pretty_generate(json_array))
21
+ end
22
+
23
+ def convert_json_to_csv(input_path, output_path)
24
+ json_content = File.read(input_path)
25
+ json_array = JSON.parse(json_content)
26
+ headers = json_array.first.keys
27
+
28
+ CSV.open(output_path, 'w') do |csv|
29
+ # Écrire les en-têtes dans le fichier CSV
30
+ csv << headers
31
+
32
+ # Parcourir chaque objet dans le tableau JSON
33
+ json_array.each do |json_object|
34
+ # Écrire une ligne dans le fichier CSV pour chaque objet
35
+ csv << json_object.values
36
+ end
37
+ end
38
+ end
39
+
40
+ class ConverterCLI < Thor
41
+
42
+ desc "convert INPUT TYPE OUTPUT", "convert a file at INPUT path to a new TYPE and save at OUTPUT path"
43
+ def convert(input, type, output)
44
+ if type === 'json'
45
+ convert_csv_to_json(input, output)
46
+ elsif type === 'csv'
47
+ convert_json_to_csv(input, output)
48
+ end
49
+ puts "Converting file at #{input} to type #{type}, will save result at #{output}"
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fconverter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexis VIVIER
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A file converter with many options.
14
+ email: alexis@nevertoolate.studio
15
+ executables:
16
+ - fconverter
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/fconverter
21
+ - lib/fconverter.rb
22
+ homepage: https://rubygems.org/gems/fconverter
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: File converter
45
+ test_files: []