json2xxx 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/json2xxx.gemspec +8 -7
- data/lib/json2xxx.rb +1 -0
- data/lib/json2xxx/cli.rb +23 -4
- data/lib/json2xxx/core.rb +29 -11
- data/lib/json2xxx/ext/string.rb +7 -0
- data/lib/json2xxx/version.rb +1 -1
- metadata +46 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bf934f8b2353d7a82b324d092111fdad8a191c0
|
4
|
+
data.tar.gz: fb5481246d8032306c4c64dea83cd628f644d48a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1c8b0b0586c5710fb5e16d8e84b54bf8036b2a4268db79ed7ef57736b8b3d671dd37221dda0d63fead0110177eba0aa42b1acaf429965940c9b4965a0a7b93c
|
7
|
+
data.tar.gz: 78e87409e0c446b7116b15ab02a995f05cf4488879d6501c8120cfa7d255b0430ba3c69c1b0a42a6d627bf202dba5183ded5299f52320f99ada252e04d1b24b2
|
data/json2xxx.gemspec
CHANGED
@@ -21,12 +21,13 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.add_dependency 'spreadsheet'
|
22
22
|
gem.add_dependency 'tbpgr_utils'
|
23
23
|
gem.add_dependency 'hashie'
|
24
|
+
gem.add_dependency 'awesome_print'
|
24
25
|
|
25
|
-
gem.add_development_dependency 'bundler'
|
26
|
-
gem.add_development_dependency 'pry'
|
27
|
-
gem.add_development_dependency 'rake'
|
28
|
-
gem.add_development_dependency 'rspec'
|
29
|
-
gem.add_development_dependency 'rubocop'
|
30
|
-
gem.add_development_dependency 'rubygems-tasks'
|
31
|
-
gem.add_development_dependency 'yard'
|
26
|
+
gem.add_development_dependency 'bundler'
|
27
|
+
gem.add_development_dependency 'pry'
|
28
|
+
gem.add_development_dependency 'rake'
|
29
|
+
gem.add_development_dependency 'rspec'
|
30
|
+
gem.add_development_dependency 'rubocop'
|
31
|
+
gem.add_development_dependency 'rubygems-tasks'
|
32
|
+
gem.add_development_dependency 'yard'
|
32
33
|
end
|
data/lib/json2xxx.rb
CHANGED
data/lib/json2xxx/cli.rb
CHANGED
@@ -1,22 +1,34 @@
|
|
1
1
|
require "thor"
|
2
2
|
require "json"
|
3
3
|
require 'yaml'
|
4
|
-
require '
|
4
|
+
require 'pp'
|
5
|
+
require 'awesome_print'
|
5
6
|
|
6
7
|
module Json2xxx
|
7
8
|
class CLI < Thor
|
8
9
|
|
9
10
|
include Thor::Actions
|
10
|
-
class_option :fields, aliases: '-f', type: :array, desc: '
|
11
|
+
class_option :fields, aliases: '-f', type: :array, desc: 'extract fields'
|
12
|
+
class_option :sort, aliases: '-s', type: :string, desc: 'sort'
|
11
13
|
def initialize(args = [], options = {}, config = {})
|
12
14
|
super(args, options, config)
|
13
15
|
@global_options = config[:shell].base.options
|
14
16
|
return unless File.pipe?(STDIN)
|
15
17
|
@data = parse_json(STDIN.read)
|
16
18
|
@core = Core.new
|
17
|
-
|
19
|
+
if @global_options['fields']
|
18
20
|
@data = @core.extract(@data, @global_options['fields'])
|
19
21
|
end
|
22
|
+
if @global_options['sort']
|
23
|
+
@data = @core.sort(@data, @global_options['sort'])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'delimiter', 'delimiter'
|
28
|
+
class_option :force_quotes, type: :boolean, default: true, desc: 'write quote'
|
29
|
+
class_option :write_header, type: :boolean, default: true, desc: 'write header'
|
30
|
+
def delimiter(delim)
|
31
|
+
puts @core.convert_csv(@data, delim, options['force_quotes'], options['write_header'])
|
20
32
|
end
|
21
33
|
|
22
34
|
desc 'tsv', 'tsv'
|
@@ -53,15 +65,22 @@ module Json2xxx
|
|
53
65
|
puts @core.convert_html(@data)
|
54
66
|
end
|
55
67
|
|
68
|
+
desc 'hash', 'hash'
|
69
|
+
def hash
|
70
|
+
ap @data, indent: 2, index: false
|
71
|
+
end
|
72
|
+
|
56
73
|
desc 'excel', 'excel'
|
74
|
+
option :output, aliases: '-o', type: :string, default: Time.now.strftime("%Y%m%d%H%M%S") + '.xls', desc: 'output file path.'
|
57
75
|
def excel
|
58
|
-
@core.convert_excel(@data)
|
76
|
+
@core.convert_excel(@data, options['output'])
|
59
77
|
end
|
60
78
|
|
61
79
|
private
|
62
80
|
|
63
81
|
def parse_json(buffer)
|
64
82
|
begin
|
83
|
+
buffer = buffer.uncolorize
|
65
84
|
data = JSON.parse(buffer)
|
66
85
|
rescue => e
|
67
86
|
data = []
|
data/lib/json2xxx/core.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
require 'spreadsheet'
|
2
1
|
require 'csv'
|
3
2
|
require 'tbpgr_utils'
|
3
|
+
require 'set'
|
4
|
+
require 'hashie'
|
5
|
+
|
6
|
+
class HashWrapper < ::Hashie::Mash
|
7
|
+
disable_warnings if respond_to?(:disable_warnings)
|
8
|
+
end
|
4
9
|
|
5
10
|
module Json2xxx
|
6
11
|
class Core
|
@@ -9,7 +14,7 @@ module Json2xxx
|
|
9
14
|
end
|
10
15
|
|
11
16
|
def convert_csv(data, delimiter, force_quotes, write_header)
|
12
|
-
headers = data
|
17
|
+
headers = get_keys(data)
|
13
18
|
csv = []
|
14
19
|
CSV.generate(col_sep: delimiter, force_quotes: force_quotes) do |csv|
|
15
20
|
csv << headers if write_header
|
@@ -22,7 +27,7 @@ module Json2xxx
|
|
22
27
|
end
|
23
28
|
|
24
29
|
def convert_markdown(data)
|
25
|
-
headers = data
|
30
|
+
headers = get_keys(data)
|
26
31
|
result = []
|
27
32
|
header = '|' + headers.join('|') + '|' + "\n" +
|
28
33
|
'|' + headers.map { |_header| ':--' }.join('|') + '|'
|
@@ -36,7 +41,7 @@ module Json2xxx
|
|
36
41
|
end
|
37
42
|
|
38
43
|
def convert_backlog_wiki(data)
|
39
|
-
headers = data
|
44
|
+
headers = get_keys(data)
|
40
45
|
result = []
|
41
46
|
result << '|~' + headers.join('|') + '|h'
|
42
47
|
|
@@ -49,9 +54,10 @@ module Json2xxx
|
|
49
54
|
result.join("\n")
|
50
55
|
end
|
51
56
|
|
52
|
-
def convert_excel(data)
|
53
|
-
|
54
|
-
headers = data
|
57
|
+
def convert_excel(data, filepath = Time.now.strftime("%Y%m%d%H%M%S") + '.xls')
|
58
|
+
require 'spreadsheet'
|
59
|
+
headers = get_keys(data)
|
60
|
+
|
55
61
|
basename = File.basename(filepath, '.*')
|
56
62
|
Spreadsheet.client_encoding = 'UTF-8'
|
57
63
|
workbook = Spreadsheet::Workbook.new
|
@@ -74,7 +80,7 @@ module Json2xxx
|
|
74
80
|
end
|
75
81
|
|
76
82
|
def convert_html(data)
|
77
|
-
headers = data
|
83
|
+
headers = get_keys(data)
|
78
84
|
result = []
|
79
85
|
result << headers
|
80
86
|
data.each do |hash|
|
@@ -88,17 +94,29 @@ module Json2xxx
|
|
88
94
|
|
89
95
|
def extract(data, fields)
|
90
96
|
data.map { |record|
|
91
|
-
record =
|
97
|
+
record = HashWrapper.new(record)
|
92
98
|
fields.inject({}) { |result, field|
|
93
|
-
|
99
|
+
eval_string = field[0] == "[" ? "record#{field}" : "record.#{field}"
|
100
|
+
result[field] = eval(eval_string)
|
94
101
|
result
|
95
102
|
}
|
96
103
|
}
|
97
104
|
end
|
98
105
|
|
106
|
+
def sort(data, sort_column)
|
107
|
+
data.sort_by{ |record|
|
108
|
+
record[sort_column]
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def get_keys(data)
|
113
|
+
keys = data.inject(Set.new) {|set, record| set.merge(record.keys) }
|
114
|
+
keys.to_a
|
115
|
+
end
|
116
|
+
|
99
117
|
def get_json_value(value)
|
100
118
|
return '' if value.nil?
|
101
|
-
return value.to_json if value.class == Array || value.class == Hash || value.class ==
|
119
|
+
return value.to_json if value.class == Array || value.class == Hash || value.class == HashWrapper
|
102
120
|
return value.to_s
|
103
121
|
end
|
104
122
|
end
|
data/lib/json2xxx/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json2xxx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hiroshi Toyama
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -66,104 +66,118 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: awesome_print
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: bundler
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- - "
|
87
|
+
- - ">="
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
89
|
+
version: '0'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- - "
|
94
|
+
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
96
|
+
version: '0'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - "
|
101
|
+
- - ">="
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0
|
103
|
+
version: '0'
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - "
|
108
|
+
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0
|
110
|
+
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
112
|
name: rake
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
|
-
- - "
|
115
|
+
- - ">="
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
117
|
+
version: '0'
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
|
-
- - "
|
122
|
+
- - ">="
|
109
123
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
124
|
+
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: rspec
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
114
128
|
requirements:
|
115
|
-
- - "
|
129
|
+
- - ">="
|
116
130
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
131
|
+
version: '0'
|
118
132
|
type: :development
|
119
133
|
prerelease: false
|
120
134
|
version_requirements: !ruby/object:Gem::Requirement
|
121
135
|
requirements:
|
122
|
-
- - "
|
136
|
+
- - ">="
|
123
137
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
138
|
+
version: '0'
|
125
139
|
- !ruby/object:Gem::Dependency
|
126
140
|
name: rubocop
|
127
141
|
requirement: !ruby/object:Gem::Requirement
|
128
142
|
requirements:
|
129
|
-
- - "
|
143
|
+
- - ">="
|
130
144
|
- !ruby/object:Gem::Version
|
131
|
-
version: 0
|
145
|
+
version: '0'
|
132
146
|
type: :development
|
133
147
|
prerelease: false
|
134
148
|
version_requirements: !ruby/object:Gem::Requirement
|
135
149
|
requirements:
|
136
|
-
- - "
|
150
|
+
- - ">="
|
137
151
|
- !ruby/object:Gem::Version
|
138
|
-
version: 0
|
152
|
+
version: '0'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: rubygems-tasks
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
142
156
|
requirements:
|
143
|
-
- - "
|
157
|
+
- - ">="
|
144
158
|
- !ruby/object:Gem::Version
|
145
|
-
version: '0
|
159
|
+
version: '0'
|
146
160
|
type: :development
|
147
161
|
prerelease: false
|
148
162
|
version_requirements: !ruby/object:Gem::Requirement
|
149
163
|
requirements:
|
150
|
-
- - "
|
164
|
+
- - ">="
|
151
165
|
- !ruby/object:Gem::Version
|
152
|
-
version: '0
|
166
|
+
version: '0'
|
153
167
|
- !ruby/object:Gem::Dependency
|
154
168
|
name: yard
|
155
169
|
requirement: !ruby/object:Gem::Requirement
|
156
170
|
requirements:
|
157
|
-
- - "
|
171
|
+
- - ">="
|
158
172
|
- !ruby/object:Gem::Version
|
159
|
-
version: '0
|
173
|
+
version: '0'
|
160
174
|
type: :development
|
161
175
|
prerelease: false
|
162
176
|
version_requirements: !ruby/object:Gem::Requirement
|
163
177
|
requirements:
|
164
|
-
- - "
|
178
|
+
- - ">="
|
165
179
|
- !ruby/object:Gem::Version
|
166
|
-
version: '0
|
180
|
+
version: '0'
|
167
181
|
description: json to variety of formats
|
168
182
|
email: toyama0919@gmail.com
|
169
183
|
executables:
|
@@ -186,6 +200,7 @@ files:
|
|
186
200
|
- lib/json2xxx.rb
|
187
201
|
- lib/json2xxx/cli.rb
|
188
202
|
- lib/json2xxx/core.rb
|
203
|
+
- lib/json2xxx/ext/string.rb
|
189
204
|
- lib/json2xxx/version.rb
|
190
205
|
- spec/cli_spec.rb
|
191
206
|
- spec/core_spec.rb
|
@@ -211,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
211
226
|
version: '0'
|
212
227
|
requirements: []
|
213
228
|
rubyforge_project:
|
214
|
-
rubygems_version: 2.
|
229
|
+
rubygems_version: 2.5.1
|
215
230
|
signing_key:
|
216
231
|
specification_version: 4
|
217
232
|
summary: json to variety of formats
|
@@ -220,4 +235,3 @@ test_files:
|
|
220
235
|
- spec/core_spec.rb
|
221
236
|
- spec/json2xxx_spec.rb
|
222
237
|
- spec/spec_helper.rb
|
223
|
-
has_rdoc:
|