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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f677cbf0543705206cad17d0457246b8381b72d7
4
- data.tar.gz: b66600d379cf1bf072d9e04c23d8f155e35f7e84
3
+ metadata.gz: 3bf934f8b2353d7a82b324d092111fdad8a191c0
4
+ data.tar.gz: fb5481246d8032306c4c64dea83cd628f644d48a
5
5
  SHA512:
6
- metadata.gz: 36aeadb86de510f13f8842b46edb1e10e7160f1e1c76cf4b1a1a28bb8962cdb4c9cee813c389070dbc80350dcc8f915704b2b136a66b808d34a2f269883b78f5
7
- data.tar.gz: 20bc21a32ff42e21ef0842744109fe80c4582ce961acdbdc873b6c80ae602df0a4f0568101cb687d8775434238052dda57929097acb2214552d46ec43a6fd1ec
6
+ metadata.gz: d1c8b0b0586c5710fb5e16d8e84b54bf8036b2a4268db79ed7ef57736b8b3d671dd37221dda0d63fead0110177eba0aa42b1acaf429965940c9b4965a0a7b93c
7
+ data.tar.gz: 78e87409e0c446b7116b15ab02a995f05cf4488879d6501c8120cfa7d255b0430ba3c69c1b0a42a6d627bf202dba5183ded5299f52320f99ada252e04d1b24b2
@@ -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', '~> 1.7.2'
26
- gem.add_development_dependency 'pry', '~> 0.10.1'
27
- gem.add_development_dependency 'rake', '~> 10.3.2'
28
- gem.add_development_dependency 'rspec', '~> 2.4'
29
- gem.add_development_dependency 'rubocop', '~> 0.24.1'
30
- gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
31
- gem.add_development_dependency 'yard', '~> 0.8'
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
@@ -1,3 +1,4 @@
1
1
  require 'json2xxx/version'
2
2
  require 'json2xxx/core'
3
3
  require 'json2xxx/cli'
4
+ require 'json2xxx/ext/string'
@@ -1,22 +1,34 @@
1
1
  require "thor"
2
2
  require "json"
3
3
  require 'yaml'
4
- require 'hashie'
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: 'default is first data'
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
- unless @global_options['fields'].nil?
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 = []
@@ -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.first.keys
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.first.keys
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.first.keys
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
- filepath = Time.now.strftime("%Y%m%d%H%M%S") + '.xls'
54
- headers = data.first.keys
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.first.keys
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 = Hashie::Mash.new(record)
97
+ record = HashWrapper.new(record)
92
98
  fields.inject({}) { |result, field|
93
- result[field] = eval("record.#{field}")
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 == Hashie::Mash
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
@@ -0,0 +1,7 @@
1
+ class String
2
+ REGEXP_PATTERN = /\e\[([;\d]+)?m/
3
+
4
+ def uncolorize
5
+ self.gsub(REGEXP_PATTERN, '')
6
+ end
7
+ end
@@ -1,4 +1,4 @@
1
1
  module Json2xxx
2
2
  # json2xxx version
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
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.3.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: 2015-01-14 00:00:00.000000000 Z
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: 1.7.2
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: 1.7.2
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.10.1
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.10.1
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: 10.3.2
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: 10.3.2
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: '2.4'
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: '2.4'
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.24.1
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.24.1
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.2'
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.2'
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.8'
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.8'
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.4.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: