json2csv 0.0.2 → 0.0.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
  SHA1:
3
- metadata.gz: bde3bd18b5a618adf8726f451417568341559b05
4
- data.tar.gz: 6c5a12445f165ec69288e854c2a4a14dd74c90f1
3
+ metadata.gz: 249febdf6137898ce7a43bd9f12ba1d7fbffd5d5
4
+ data.tar.gz: 8d606d373e79e91632d69236ba314481cf53347c
5
5
  SHA512:
6
- metadata.gz: 46d4a50652f7052216e1898bce79a9783cd08c4a13ab2a9a260422e22002e7a7ec3ad21a962d3814612d2f8eb43b11b130a29c916edaa34ef521e4de1efa52e0
7
- data.tar.gz: 5456480f5408f730e17ce3b08ad8dc6d3735ba24e5173f789b1fc1c765c6967113b90ae3dd8d2fd3f4e26c043d1a5b6362d4c60656d1a31f24bdb5a6f31c4cc5
6
+ metadata.gz: d67e8524e08b0182186b8b351fbe56ae6cdb83f4cfe73db0f19f576f82b658e9ada51d351e7c52e1e82c69cb4add5aa42cbb0063976c06aaed17ff954c8c44ca
7
+ data.tar.gz: 79499ae598cc5e21cca31a761171e13850f22faaf1203c510698f6847bb5f778258b63cd1f9d6fd5338889f428a29420a18b3609e82f9df2c980f861bd565423
data/.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
 
32
32
  s.add_development_dependency 'coveralls', '~> 0.7', '>= 0.7.0r'
33
33
  s.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
34
- s.add_development_dependency 'ruby-debug-ide' if RUBY_VERSION >= '2.1'
34
+ # s.add_development_dependency 'ruby-debug-ide' if RUBY_VERSION >= '2.1'
35
35
  s.add_development_dependency 'rubocop', '~> 0.24', '>= 0.24.0'
36
36
  s.add_development_dependency 'simplecov', '~> 0.8', '>= 0.8.2'
37
37
  end
data/README.md CHANGED
@@ -15,14 +15,51 @@ gem install json2csv
15
15
 
16
16
  ## Usage
17
17
 
18
+ **Simple conversion**
19
+
18
20
  ```
19
21
  json2csv convert data/sample.json
22
+
23
+ ```
24
+
25
+ **Global help**
26
+
27
+ ```
28
+ json2csv help
29
+ NAME
30
+ json2csv - json2csv 0.0.3
31
+
32
+ SYNOPSIS
33
+ json2csv [global options] command [command options] [arguments...]
34
+
35
+ GLOBAL OPTIONS
36
+ --help - Show this message
37
+
38
+ COMMANDS
39
+ convert - Convert json file
40
+ help - Shows a list of commands or help for one command
41
+ version - Print version info
42
+ ```
43
+
44
+ **Convert help**
45
+
46
+ ```
47
+ json2csv help convert
48
+ NAME
49
+ convert - Convert json file
50
+
51
+ SYNOPSIS
52
+ json2csv [global options] convert [command options]
53
+
54
+ COMMAND OPTIONS
55
+ -d, --delimiter=arg - (default: ,)
56
+ -r, --root=arg - (default: none)
20
57
  ```
21
58
 
22
59
  ## Example
23
60
 
24
61
 
25
- *Input json*
62
+ **Input json**
26
63
 
27
64
  ```
28
65
  cat data/sample.json
@@ -47,14 +84,14 @@ cat data/sample.json
47
84
  "Street": "#111 Sutter St, 94104",
48
85
  "City": "San Francisco",
49
86
  "Details": {
50
- "note": "Korean Close close main entrance"
87
+ "note": "Korean Deli near to main entrance"
51
88
  }
52
89
  }
53
90
  }
54
91
  }
55
92
  ```
56
93
 
57
- *Convert json*
94
+ **Convert json**
58
95
 
59
96
  ```
60
97
  ./bin/json2csv convert data/sample.json
@@ -62,13 +99,13 @@ Converting data/sample.json
62
99
 
63
100
  ```
64
101
 
65
- *Output json*
102
+ **Output json**
66
103
 
67
104
  ```
68
105
  cat data/sample.json.csv
69
106
 
70
107
  id,Firstname,Lastname,Address.Street,Address.City,Address.Details.note
71
108
  12345,Joe,Doe,"#2140 Taylor Street, 94133",San Francisco,Pool available
72
- 45678,Jack,Plumber,"#111 Sutter St, 94104",San Francisco,Korean Close close main entrance
109
+ 45678,Jack,Plumber,"#111 Sutter St, 94104",San Francisco,Korean Deli near to main entrance
73
110
 
74
- ```
111
+ ```
@@ -18,7 +18,7 @@
18
18
  "Street": "#111 Sutter St, 94104",
19
19
  "City": "San Francisco",
20
20
  "Details": {
21
- "note": "Korean Close close main entrance"
21
+ "note": "Korean Deli near to main entrance"
22
22
  }
23
23
  }
24
24
  }
@@ -9,10 +9,14 @@ require_relative '../convert/convert'
9
9
 
10
10
  desc 'Convert json file'
11
11
  command :convert do |c|
12
- c.action do |_global_options, _options, args|
12
+ c.flag [:r, :root], :type => String
13
+ c.flag [:d, :delimiter], :type => String, :default_value => ','
14
+
15
+ c.action do |global_options, options, args|
13
16
  fail ArgumentError, 'No file to convert specified' if args.empty?
14
17
 
15
- Json2Csv::Convert.convert(args)
18
+ opts = {}.merge(global_options).merge(options)
19
+ Json2Csv::Convert.convert(args, opts)
16
20
  end
17
21
  end
18
22
 
@@ -10,15 +10,25 @@ require_relative '../../version'
10
10
  module Json2Csv
11
11
  # Apollon bootstrap module
12
12
  module Convert
13
+ DEFAULT_OPTIONS = {
14
+ :out_path => 'out.txt',
15
+ :delimiter => ','
16
+ }
17
+
13
18
  class << self
14
- def convert(paths)
19
+ def convert(paths, opts = {})
15
20
  paths = [paths] unless paths.is_a?(Array)
16
21
  paths.each do |path|
17
22
  puts "Converting #{path}"
18
23
 
19
24
  json = load_file(path)
20
25
 
21
- process(json, "#{path}.csv")
26
+ if(opts[:root])
27
+ json = json[opts[:root]]
28
+ end
29
+
30
+ real_opts =
31
+ process(json, DEFAULT_OPTIONS.merge(opts).merge(:out_path => "#{path}.csv"))
22
32
  end
23
33
  end
24
34
 
@@ -45,6 +55,7 @@ module Json2Csv
45
55
  def get_value(obj, path)
46
56
  segments = path.split('.')
47
57
  segments.each do |segment|
58
+ return nil if obj.nil?
48
59
  obj = obj[segment]
49
60
  end
50
61
  obj
@@ -68,13 +79,18 @@ module Json2Csv
68
79
  File.open('error.txt', 'wt') { |f| f.write(e.to_s) }
69
80
  end
70
81
 
71
- def process(json, out_path = 'out.txt')
82
+ def process(json, opts = DEFAULT_OPTIONS)
72
83
  keys = json.keys
73
84
 
74
85
  header = nil
75
86
 
87
+ out_path = opts[:out_path]
88
+ csv_opts = {
89
+ col_sep: opts[:delimiter] || DEFAULT_OPTIONS[:delimiter]
90
+ }
91
+
76
92
  # Open the CSV for write
77
- CSV.open(out_path, 'wt') do |csv|
93
+ CSV.open(out_path, 'wt', csv_opts) do |csv|
78
94
  # Take each story - json['stories'][<ID_HERE>]
79
95
  keys.each do |key|
80
96
  obj = json[key]
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Apollon module
4
4
  module Json2Csv
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json2csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Korcak
@@ -138,20 +138,6 @@ dependencies:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: 3.0.0
141
- - !ruby/object:Gem::Dependency
142
- name: ruby-debug-ide
143
- requirement: !ruby/object:Gem::Requirement
144
- requirements:
145
- - - ">="
146
- - !ruby/object:Gem::Version
147
- version: '0'
148
- type: :development
149
- prerelease: false
150
- version_requirements: !ruby/object:Gem::Requirement
151
- requirements:
152
- - - ">="
153
- - !ruby/object:Gem::Version
154
- version: '0'
155
141
  - !ruby/object:Gem::Dependency
156
142
  name: rubocop
157
143
  requirement: !ruby/object:Gem::Requirement