csv_json_converter 0.1.1 → 0.1.3

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: bb1717587cef433b03f7926fe88567ef3fc16b7a1a2a963881cd0750550b7d77
4
- data.tar.gz: 44169ae334d3df842aa410260e4dcc7880fab765d1d2d84709b78af5e3d28f16
3
+ metadata.gz: f4a861d0f37952316e996ed45a1838727f8639cbeac64b393b6f0f10b6444822
4
+ data.tar.gz: 712b4ac2af69d0e11441338e1ab190e2d93f0169ba8d21937c1dce41d1618c85
5
5
  SHA512:
6
- metadata.gz: b8e3ddacda3672913642fed427eb42c3127e5dd0bc6efb5d52e59b03927086b6b3bdba5121a146094bda2a57c779d2907a74542f5cf71d9166a5e3cc6650252e
7
- data.tar.gz: e9f5be03e570d64b791e4fa756f3da345593206416b1117e8d4c2bf1be76aac93810be35978bfa94752fc71d50f79bcb899258457b2eacb848d691b0a23418c7
6
+ metadata.gz: 668f30cfdfe949fdc2c4f6998428d9da52020ea5175682b7ef958c9f80c0f3cb61d1864b4a9a4d9df2d7b642e306b4bdb433632cad8815747d32545a0d76a748
7
+ data.tar.gz: 9ea7c0e6f7203587a295201f2871e918b8f40395eba99a2958be53267ec5055dc271d97892adf17fcd97912d863c67fa1768e77f44665c2ae1eaec873173c1f3
data/README.md CHANGED
@@ -13,8 +13,8 @@
13
13
  - [:busts_in_silhouette: Authors ](#-authors-)
14
14
  - [:handshake: Contributing ](#-contributing-)
15
15
  - [:star:️ Show your support ](#️-show-your-support-)
16
- # :book: json_or_ruby_to_csv <a name="about-project"></a>
17
- This gem converts data from a variety of sources into a comma-separated value (CSV) format. The gem can convert arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects. The CSV format is a common way to store data in a tabular format. It is often used for data exchange between different applications.
16
+ # :book: csv_json_convert <a name="about-project"></a>
17
+ This gem converts a csv string to json object.
18
18
  ## Installation <a name="tech-stack"></a>
19
19
  Add the following code to you Gemfile
20
20
  ```
@@ -32,25 +32,21 @@ If you want to convert `csv string`, to json you can use `to_json` method from `
32
32
 
33
33
  <i>For example </i>
34
34
  ```ruby
35
- #```
35
+
36
36
  require 'csv_json_converter'
37
37
 
38
38
  data_csv='file;text;number;hex
39
39
  test18.csv;CMkABfAGXvmSFV;9892576;jz40cbafbec8d6f92e93d22ea6ef5b'
40
40
 
41
- json_data = CsvJsonCoverter::Csv.to_json(data_csv)
41
+ json_data = CsvJsonCoverter.to_json(data_csv)
42
42
 
43
43
  print json_data
44
- #{ "file": "test18.csv",
45
- # "text": "CMkABfAGXvmSFV",
46
- # "number": 9892576,
47
- # "hex": "jz40cbafbec8d6f92e93d22ea6ef5b"
48
- #}
49
- #```
44
+
50
45
  ```
51
46
 
52
47
  **The output looks like below**
53
- ```{ "file": "test18.csv",
48
+ ```
49
+ { "file": "test18.csv",
54
50
  "text": "CMkABfAGXvmSFV",
55
51
  "number": 9892576,
56
52
  "hex": "jz40cbafbec8d6f92e93d22ea6ef5b"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CsvJsonConverter
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.3"
5
5
  end
@@ -6,16 +6,44 @@ require_relative "csv_json_converter/version"
6
6
  module CsvJsonConverter
7
7
  class Error < StandardError; end
8
8
  # Your code goes here...
9
- class Csv
10
- def self.to_json(input, separator=',')
11
- for_csv = CSV.parse(input,:col_sep => separator,headers: true, header_converters: :symbol)
12
- # treat first row as headers if the caller didn't provide them
13
- arr_to_json = for_csv.map(&:to_h).to_json
14
-
15
- arr_to_json
9
+ def self.to_json(input, separator=',')
10
+ for_csv = CSV.parse(input,:col_sep => separator,headers: true, header_converters: :symbol)
11
+ # treat first row as headers if the caller didn't provide them
12
+ arr_to_json = for_csv.map(&:to_h).to_json
13
+
14
+ arr_to_json
15
+
16
16
 
17
-
17
+ end
18
+
19
+ def self.to_csv(input)
20
+
21
+ for_json=JSON.parse(input)
22
+ column_names = for_json.first.keys
23
+ csv_string=CSV.generate do |csv|
24
+ csv << column_names
25
+ for_json.each do |x|
26
+ csv << x.values
27
+ end
18
28
  end
19
-
29
+
30
+ csv_string
31
+
20
32
  end
21
33
  end
34
+
35
+
36
+ p arr_json=CsvJsonConverter.to_json('file,text,number,hex
37
+ test18.csv,CMkABfAGXvmSFV,9892576,jz40cbafbec8d6f92e93d22ea6ef5b
38
+ test18.csv,NCVTagVs,815097234,jz0ee6f01d178831228b957376f15c
39
+ test18.csv,RCZgwkLosc,3,jz22ff45479c30a890925d5832c319
40
+ test18.csv,HZgYgaBNZFXyVdzuRuddpg,31,jz737707427673939e4c198598cefc
41
+ test18.csv,tfp,6,jzedd110611f58b53215dd0b27f7ba
42
+ ')
43
+
44
+ arr_csv=CsvJsonConverter.to_csv(
45
+ arr_json
46
+ )
47
+
48
+ p arr_csv
49
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_json_converter
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
  - alexoid1
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-24 00:00:00.000000000 Z
11
+ date: 2023-08-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem transform a csv string in a json string.
14
14
  email: