ki_format 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ki_format.rb +3 -1
- data/lib/ki_format/command.rb +16 -35
- data/lib/ki_format/json.rb +29 -0
- data/lib/ki_format/object_set.rb +15 -0
- data/lib/ki_format/table_maker.rb +10 -3
- data/lib/ki_format/version.rb +1 -1
- data/spec/unit/json_spec.rb +23 -0
- data/spec/unit/object_set_spec.rb +23 -0
- data/spec/unit/table_maker_spec.rb +5 -0
- metadata +71 -19
data/lib/ki_format.rb
CHANGED
@@ -5,7 +5,9 @@ require 'terminal-table'
|
|
5
5
|
|
6
6
|
module KiFormat
|
7
7
|
|
8
|
-
autoload :Command,
|
8
|
+
autoload :Command, 'ki_format/command'
|
9
|
+
autoload :Json, 'ki_format/json'
|
10
|
+
autoload :ObjectSet, 'ki_format/object_set'
|
9
11
|
autoload :TableMaker, 'ki_format/table_maker'
|
10
12
|
|
11
13
|
end
|
data/lib/ki_format/command.rb
CHANGED
@@ -11,74 +11,55 @@ module KiFormat
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def print_pretty
|
14
|
-
json_in do |data|
|
15
|
-
|
14
|
+
json_in(false) do |data|
|
15
|
+
MultiJson.dump(data, :pretty => true)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
def print_keys
|
20
20
|
json_in do |data|
|
21
|
-
|
22
|
-
keys = data.inject({}) { |r,e| r.merge(e) }.keys
|
23
|
-
output.puts keys.join("\n")
|
21
|
+
ObjectSet.merged_keys(data).join("\n")
|
24
22
|
end
|
25
23
|
end
|
26
24
|
|
27
25
|
def print_list
|
28
26
|
json_in do |data|
|
29
|
-
data
|
30
|
-
|
31
|
-
|
32
|
-
end
|
27
|
+
data.map do |obj|
|
28
|
+
MultiJson.dump(obj, :pretty => false)
|
29
|
+
end.join("\n")
|
33
30
|
end
|
34
31
|
end
|
35
32
|
|
36
33
|
def print_table(*keys)
|
37
34
|
json_in do |data|
|
38
|
-
data
|
39
|
-
output.puts(TableMaker.new(data, :only => keys).print)
|
35
|
+
TableMaker.new(data, :only => keys).print
|
40
36
|
end
|
41
37
|
end
|
42
38
|
|
43
39
|
def print_selected(*keys)
|
44
40
|
json_in do |data|
|
45
|
-
data
|
46
|
-
output.puts(TableMaker.new(data, :only => keys).print(:plain => true))
|
41
|
+
TableMaker.new(data, :only => keys).print(:plain => true)
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
50
45
|
private
|
51
46
|
|
52
47
|
def wrap(data)
|
53
|
-
|
48
|
+
case data
|
49
|
+
when Array
|
54
50
|
data
|
55
51
|
else
|
56
52
|
[data]
|
57
53
|
end
|
58
54
|
end
|
59
55
|
|
60
|
-
def json_in
|
61
|
-
|
62
|
-
|
63
|
-
data = MultiJson.load(json)
|
64
|
-
yield data
|
65
|
-
rescue MultiJson::DecodeError
|
66
|
-
begin
|
67
|
-
data = try_as_array(json)
|
68
|
-
yield data
|
69
|
-
rescue MultiJson::DecodeError
|
70
|
-
error.puts "Invalid JSON"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def try_as_array(json)
|
76
|
-
array = []
|
77
|
-
parts = json.split("\n")
|
78
|
-
parts.each do |part|
|
79
|
-
array << MultiJson.load(part)
|
56
|
+
def json_in(should_wrap = true)
|
57
|
+
result = Json.load_json(input.read) do |data|
|
58
|
+
yield should_wrap ? wrap(data) : data
|
80
59
|
end
|
81
|
-
|
60
|
+
output.puts result
|
61
|
+
rescue MultiJson::DecodeError
|
62
|
+
error.puts "Invalid JSON"
|
82
63
|
end
|
83
64
|
|
84
65
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module KiFormat
|
2
|
+
|
3
|
+
module Json
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def load_json(json)
|
8
|
+
data = MultiJson.load(json)
|
9
|
+
yield data
|
10
|
+
rescue MultiJson::DecodeError
|
11
|
+
begin
|
12
|
+
data = try_as_array(json)
|
13
|
+
yield data
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def try_as_array(json)
|
18
|
+
array = []
|
19
|
+
parts = json.split("\n")
|
20
|
+
parts.each do |part|
|
21
|
+
array << MultiJson.load(part)
|
22
|
+
end
|
23
|
+
array
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -23,8 +23,7 @@ module KiFormat
|
|
23
23
|
end
|
24
24
|
str = table.to_s
|
25
25
|
if plain
|
26
|
-
|
27
|
-
str.split("\n")[1..-1].join("\n")
|
26
|
+
remove_first_blank_line(str)
|
28
27
|
else
|
29
28
|
str
|
30
29
|
end
|
@@ -32,13 +31,21 @@ module KiFormat
|
|
32
31
|
|
33
32
|
private
|
34
33
|
|
34
|
+
def remove_first_blank_line(str)
|
35
|
+
if str=~/.+\n/
|
36
|
+
str.split("\n")[1..-1].join("\n")
|
37
|
+
else
|
38
|
+
str
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
35
42
|
def build_headings(keys)
|
36
43
|
all_keys = Set.new
|
37
44
|
@data.each do |obj|
|
38
45
|
all_keys.merge(obj.keys)
|
39
46
|
end
|
40
47
|
if keys && !keys.empty?
|
41
|
-
all_keys.intersection(keys)
|
48
|
+
all_keys.intersection(keys)
|
42
49
|
else
|
43
50
|
all_keys
|
44
51
|
end
|
data/lib/ki_format/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Json.load_json" do
|
4
|
+
|
5
|
+
it 'yields json' do
|
6
|
+
data = nil
|
7
|
+
Json.load_json('{ "a" : 1 }') { |d| data = d}
|
8
|
+
data.should == {'a' => 1}
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'yields array if JSON objects are on different lines' do
|
12
|
+
data = nil
|
13
|
+
Json.load_json(%Q{{"a":1}\n{"b":2}}) { |d| data = d}
|
14
|
+
data.should == [{'a'=>1}, {'b' => 2}]
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'raises DecodeError if JSON is completely invalid' do
|
18
|
+
lambda do
|
19
|
+
Json.load_json("2")
|
20
|
+
end.should raise_error(MultiJson::DecodeError)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ObjectSet do
|
4
|
+
|
5
|
+
describe ".merged_keys" do
|
6
|
+
|
7
|
+
it 'returns empty array for empty set of objects' do
|
8
|
+
ObjectSet.merged_keys([]).should == []
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns all keys of object if there is just one object' do
|
12
|
+
ObjectSet.merged_keys([{'a'=>1}]).should == ['a']
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'returns union of keys if there are more than one object' do
|
16
|
+
objects = [ {:a => 1, :b => 2},
|
17
|
+
{:b => 3, :c => 4} ]
|
18
|
+
ObjectSet.merged_keys(objects).should == [:a, :b, :c]
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -2,6 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe TableMaker do
|
4
4
|
|
5
|
+
it "returns empty string if data is empty array" do
|
6
|
+
maker = TableMaker.new([])
|
7
|
+
maker.print(:plain => true).should == "\n"
|
8
|
+
end
|
9
|
+
|
5
10
|
it "prints table with keys" do
|
6
11
|
data = [{ :name => "Bob", :age => 35 },
|
7
12
|
{ :name => "Sam", :age => 40 }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ki_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,15 @@ dependencies:
|
|
22
22
|
version: 1.3.6
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements:
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.3.6
|
26
31
|
- !ruby/object:Gem::Dependency
|
27
32
|
name: methadone
|
28
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
29
34
|
none: false
|
30
35
|
requirements:
|
31
36
|
- - ~>
|
@@ -33,10 +38,15 @@ dependencies:
|
|
33
38
|
version: 1.2.1
|
34
39
|
type: :runtime
|
35
40
|
prerelease: false
|
36
|
-
version_requirements:
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.2.1
|
37
47
|
- !ruby/object:Gem::Dependency
|
38
48
|
name: map
|
39
|
-
requirement:
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
51
|
requirements:
|
42
52
|
- - ~>
|
@@ -44,10 +54,15 @@ dependencies:
|
|
44
54
|
version: 4.6.1
|
45
55
|
type: :runtime
|
46
56
|
prerelease: false
|
47
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 4.6.1
|
48
63
|
- !ruby/object:Gem::Dependency
|
49
64
|
name: terminal-table
|
50
|
-
requirement:
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
51
66
|
none: false
|
52
67
|
requirements:
|
53
68
|
- - ~>
|
@@ -55,10 +70,15 @@ dependencies:
|
|
55
70
|
version: 1.4.5
|
56
71
|
type: :runtime
|
57
72
|
prerelease: false
|
58
|
-
version_requirements:
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.4.5
|
59
79
|
- !ruby/object:Gem::Dependency
|
60
80
|
name: debugger
|
61
|
-
requirement:
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
62
82
|
none: false
|
63
83
|
requirements:
|
64
84
|
- - ~>
|
@@ -66,10 +86,15 @@ dependencies:
|
|
66
86
|
version: 1.2.0
|
67
87
|
type: :development
|
68
88
|
prerelease: false
|
69
|
-
version_requirements:
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ~>
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.2.0
|
70
95
|
- !ruby/object:Gem::Dependency
|
71
96
|
name: rake
|
72
|
-
requirement:
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
73
98
|
none: false
|
74
99
|
requirements:
|
75
100
|
- - ~>
|
@@ -77,10 +102,15 @@ dependencies:
|
|
77
102
|
version: 0.9.2
|
78
103
|
type: :development
|
79
104
|
prerelease: false
|
80
|
-
version_requirements:
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ~>
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.9.2
|
81
111
|
- !ruby/object:Gem::Dependency
|
82
112
|
name: rspec
|
83
|
-
requirement:
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
84
114
|
none: false
|
85
115
|
requirements:
|
86
116
|
- - ~>
|
@@ -88,10 +118,15 @@ dependencies:
|
|
88
118
|
version: 2.11.0
|
89
119
|
type: :development
|
90
120
|
prerelease: false
|
91
|
-
version_requirements:
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ~>
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 2.11.0
|
92
127
|
- !ruby/object:Gem::Dependency
|
93
128
|
name: simplecov
|
94
|
-
requirement:
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
95
130
|
none: false
|
96
131
|
requirements:
|
97
132
|
- - ~>
|
@@ -99,7 +134,12 @@ dependencies:
|
|
99
134
|
version: 0.6.4
|
100
135
|
type: :development
|
101
136
|
prerelease: false
|
102
|
-
version_requirements:
|
137
|
+
version_requirements: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ~>
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 0.6.4
|
103
143
|
description: Formats JSON
|
104
144
|
email:
|
105
145
|
- ben@freeagent.com
|
@@ -119,10 +159,14 @@ files:
|
|
119
159
|
- ki_format.gemspec
|
120
160
|
- lib/ki_format.rb
|
121
161
|
- lib/ki_format/command.rb
|
162
|
+
- lib/ki_format/json.rb
|
163
|
+
- lib/ki_format/object_set.rb
|
122
164
|
- lib/ki_format/table_maker.rb
|
123
165
|
- lib/ki_format/version.rb
|
124
166
|
- spec/integration/commands_spec.rb
|
125
167
|
- spec/spec_helper.rb
|
168
|
+
- spec/unit/json_spec.rb
|
169
|
+
- spec/unit/object_set_spec.rb
|
126
170
|
- spec/unit/table_maker_spec.rb
|
127
171
|
homepage: ''
|
128
172
|
licenses: []
|
@@ -136,19 +180,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
136
180
|
- - ! '>='
|
137
181
|
- !ruby/object:Gem::Version
|
138
182
|
version: '0'
|
183
|
+
segments:
|
184
|
+
- 0
|
185
|
+
hash: 3016356244557293053
|
139
186
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
187
|
none: false
|
141
188
|
requirements:
|
142
189
|
- - ! '>='
|
143
190
|
- !ruby/object:Gem::Version
|
144
191
|
version: '0'
|
192
|
+
segments:
|
193
|
+
- 0
|
194
|
+
hash: 3016356244557293053
|
145
195
|
requirements: []
|
146
196
|
rubyforge_project:
|
147
|
-
rubygems_version: 1.8.
|
197
|
+
rubygems_version: 1.8.24
|
148
198
|
signing_key:
|
149
199
|
specification_version: 3
|
150
200
|
summary: Formats JSON
|
151
201
|
test_files:
|
152
202
|
- spec/integration/commands_spec.rb
|
153
203
|
- spec/spec_helper.rb
|
204
|
+
- spec/unit/json_spec.rb
|
205
|
+
- spec/unit/object_set_spec.rb
|
154
206
|
- spec/unit/table_maker_spec.rb
|