ki_format 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ki_format CHANGED
@@ -12,18 +12,22 @@ module KiFormat
12
12
  main do |arg|
13
13
  input = arg.nil? ? $stdin : File.open(arg, 'r')
14
14
  command = Command.new(input)
15
- if options[:pretty]
16
- command.print_pretty
17
- elsif options[:list]
18
- command.print_list
19
- elsif options.has_key?(:table)
20
- keys = (options[:table] || "").split(",")
21
- command.print_table(*keys)
22
- elsif options.has_key?(:select)
23
- keys = (options[:select] || "").split(",")
24
- command.print_selected(*keys)
15
+ if options[:keys]
16
+ command.print_keys
25
17
  else
26
- help_now! "No option given"
18
+ if options[:pretty]
19
+ command.print_pretty
20
+ elsif options[:list]
21
+ command.print_list
22
+ elsif options.has_key?(:table)
23
+ keys = (options[:table] || "").split(",")
24
+ command.print_table(*keys)
25
+ elsif options.has_key?(:select)
26
+ keys = (options[:select] || "").split(",")
27
+ command.print_selected(*keys)
28
+ else
29
+ help_now! "No option given"
30
+ end
27
31
  end
28
32
  end
29
33
 
@@ -35,6 +39,7 @@ module KiFormat
35
39
  on("--list", "Print each JSON object on one line")
36
40
  on("--table [key1,key2]", "Print JSON as table. Optionally include keys")
37
41
  on("--select [key1,key2]", "Select specific keys")
42
+ on("--keys", "Show a list of keys")
38
43
  go!
39
44
  end
40
45
 
@@ -16,6 +16,14 @@ module KiFormat
16
16
  end
17
17
  end
18
18
 
19
+ def print_keys
20
+ json_in do |data|
21
+ data = wrap(data)
22
+ keys = data.inject({}) { |r,e| r.merge(e) }.keys
23
+ output.puts keys.join("\n")
24
+ end
25
+ end
26
+
19
27
  def print_list
20
28
  json_in do |data|
21
29
  data = wrap(data)
@@ -55,10 +63,25 @@ module KiFormat
55
63
  data = MultiJson.load(json)
56
64
  yield data
57
65
  rescue MultiJson::DecodeError
58
- error.puts "Invalid JSON"
66
+ begin
67
+ data = try_as_array(json)
68
+ yield data
69
+ rescue MultiJson::DecodeError
70
+ error.puts "Invalid JSON"
71
+ end
59
72
  end
60
73
  end
61
74
 
75
+ def try_as_array(json)
76
+ array = []
77
+ parts = json.split("\n")
78
+ parts.each do |part|
79
+ array << MultiJson.load(part)
80
+ end
81
+ array
82
+ end
83
+
62
84
  end
63
85
 
86
+
64
87
  end
@@ -1,3 +1,3 @@
1
1
  module KiFormat
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -53,6 +53,27 @@ EOS
53
53
  out.should == expected_json_out
54
54
  end
55
55
 
56
+ it "accepts array-like json" do
57
+ input =<<-JSON
58
+ { "foo" : "bar" }
59
+ { "foo" : "baz" }
60
+ JSON
61
+ expected_json_out =<<-EOS
62
+ [
63
+ {
64
+ "foo": "bar"
65
+ },
66
+ {
67
+ "foo": "baz"
68
+ }
69
+ ]
70
+ EOS
71
+ out, err = with_command(input) do |command|
72
+ command.print_pretty
73
+ end
74
+ out.should == expected_json_out
75
+ end
76
+
56
77
  it "can print objects in array on different lines" do
57
78
  input =<<-JSON
58
79
  [ { "foo" : "bar" }, { "baz" : "baz" } ]
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.1
4
+ version: 0.0.5
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: 2012-08-16 00:00:00.000000000Z
13
+ date: 2012-08-17 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
17
- requirement: &70206829817800 !ruby/object:Gem::Requirement
17
+ requirement: &70155507580720 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.3.6
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70206829817800
25
+ version_requirements: *70155507580720
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: methadone
28
- requirement: &70206829817300 !ruby/object:Gem::Requirement
28
+ requirement: &70155507580220 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.2.1
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70206829817300
36
+ version_requirements: *70155507580220
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: map
39
- requirement: &70206829816840 !ruby/object:Gem::Requirement
39
+ requirement: &70155507579760 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 4.6.1
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70206829816840
47
+ version_requirements: *70155507579760
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: terminal-table
50
- requirement: &70206829816380 !ruby/object:Gem::Requirement
50
+ requirement: &70155507579300 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.4.5
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *70206829816380
58
+ version_requirements: *70155507579300
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: debugger
61
- requirement: &70206856634200 !ruby/object:Gem::Requirement
61
+ requirement: &70155507578840 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.2.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70206856634200
69
+ version_requirements: *70155507578840
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rake
72
- requirement: &70206856633740 !ruby/object:Gem::Requirement
72
+ requirement: &70155507578380 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 0.9.2
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70206856633740
80
+ version_requirements: *70155507578380
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rspec
83
- requirement: &70206856633280 !ruby/object:Gem::Requirement
83
+ requirement: &70155507577920 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ~>
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 2.11.0
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *70206856633280
91
+ version_requirements: *70155507577920
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: simplecov
94
- requirement: &70206856632820 !ruby/object:Gem::Requirement
94
+ requirement: &70155507577460 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ~>
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: 0.6.4
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *70206856632820
102
+ version_requirements: *70155507577460
103
103
  description: Formats JSON
104
104
  email:
105
105
  - ben@freeagent.com