ki_format 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ki_format.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ben Brinckerhoff
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # KiFormat
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ki_format'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ki_format
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb"
11
+ end
12
+
13
+ desc "Generate code coverage"
14
+ RSpec::Core::RakeTask.new("spec:coverage") do |t|
15
+ ENV['COVERAGE'] = "true"
16
+ end
data/bin/ki_format ADDED
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'methadone'
4
+ require 'ki_format'
5
+ require 'fileutils'
6
+
7
+ module KiFormat
8
+ module CLI
9
+ include Methadone::Main
10
+ include Methadone::CLILogging
11
+
12
+ main do |arg|
13
+ input = arg.nil? ? $stdin : File.open(arg, 'r')
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)
25
+ else
26
+ help_now! "No option given"
27
+ end
28
+ end
29
+
30
+ version KiFormat::VERSION
31
+ description 'Formats JSON'
32
+
33
+ arg(:file, :optional)
34
+ on("--pretty", "Pretty print the JSON")
35
+ on("--list", "Print each JSON object on one line")
36
+ on("--table [key1,key2]", "Print JSON as table. Optionally include keys")
37
+ on("--select [key1,key2]", "Select specific keys")
38
+ go!
39
+ end
40
+
41
+ end
data/ki_format.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ki_format/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ben Brinckerhoff", "Aaron Cronin"]
6
+ gem.email = ["ben@freeagent.com", "aaron@freeagent.com"]
7
+ gem.description = %q{Formats JSON}
8
+ gem.summary = %q{Formats JSON}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ki_format"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = KiFormat::VERSION
17
+
18
+ gem.add_dependency('multi_json', '~> 1.3.6')
19
+ gem.add_dependency('methadone', '~> 1.2.1')
20
+ gem.add_dependency('map', '~> 4.6.1')
21
+ gem.add_dependency('terminal-table', '~> 1.4.5')
22
+
23
+ gem.add_development_dependency('debugger', '~> 1.2.0')
24
+ gem.add_development_dependency('rake', '~> 0.9.2')
25
+ gem.add_development_dependency('rspec', '~> 2.11.0')
26
+ gem.add_development_dependency('simplecov', '~> 0.6.4')
27
+ end
@@ -0,0 +1,64 @@
1
+ module KiFormat
2
+
3
+ class Command
4
+
5
+ attr_reader :input, :output, :error
6
+
7
+ def initialize(input=$stdin, output=$stdout, error=$stderr)
8
+ @input = input
9
+ @output = output
10
+ @error = error
11
+ end
12
+
13
+ def print_pretty
14
+ json_in do |data|
15
+ output.puts MultiJson.dump(data, :pretty => true)
16
+ end
17
+ end
18
+
19
+ def print_list
20
+ json_in do |data|
21
+ data = wrap(data)
22
+ data.each do |obj|
23
+ output.puts MultiJson.dump(obj, :pretty => false)
24
+ end
25
+ end
26
+ end
27
+
28
+ def print_table(*keys)
29
+ json_in do |data|
30
+ data = wrap(data)
31
+ output.puts(TableMaker.new(data, :only => keys).print)
32
+ end
33
+ end
34
+
35
+ def print_selected(*keys)
36
+ json_in do |data|
37
+ data = wrap(data)
38
+ output.puts(TableMaker.new(data, :only => keys).print(:plain => true))
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def wrap(data)
45
+ if data.is_a?(Array)
46
+ data
47
+ else
48
+ [data]
49
+ end
50
+ end
51
+
52
+ def json_in
53
+ json = input.read
54
+ begin
55
+ data = MultiJson.load(json)
56
+ yield data
57
+ rescue MultiJson::DecodeError
58
+ error.puts "Invalid JSON"
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,61 @@
1
+ require 'set'
2
+
3
+ module KiFormat
4
+
5
+ class TableMaker
6
+
7
+ def initialize(data, opts = {})
8
+ keys = opts.fetch(:only, nil)
9
+ @data = data
10
+ @headings = build_headings(keys)
11
+ @rows = build_rows
12
+ end
13
+
14
+ def print(opts = {})
15
+ plain = opts.fetch(:plain, false)
16
+ table = Terminal::Table.new do |t|
17
+ t.rows = @rows
18
+ if plain
19
+ t.style = {:border_x => "", :border_i => "", :border_y => "", :padding_left => 0, :padding_right => 2}
20
+ else
21
+ t.headings = @headings
22
+ end
23
+ end
24
+ str = table.to_s
25
+ if plain
26
+ # hack to remove the first (blank) line
27
+ str.split("\n")[1..-1].join("\n")
28
+ else
29
+ str
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def build_headings(keys)
36
+ all_keys = Set.new
37
+ @data.each do |obj|
38
+ all_keys.merge(obj.keys)
39
+ end
40
+ if keys && !keys.empty?
41
+ all_keys.intersection(keys)
42
+ else
43
+ all_keys
44
+ end
45
+ end
46
+
47
+ def build_rows
48
+ rows = []
49
+ @data.each do |obj|
50
+ row = []
51
+ @headings.each do |key|
52
+ row << obj[key]
53
+ end
54
+ rows << row
55
+ end
56
+ rows
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,3 @@
1
+ module KiFormat
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ki_format.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "ki_format/version"
2
+ require 'multi_json'
3
+ require 'map'
4
+ require 'terminal-table'
5
+
6
+ module KiFormat
7
+
8
+ autoload :Command, 'ki_format/command'
9
+ autoload :TableMaker, 'ki_format/table_maker'
10
+
11
+ end
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe Command do
4
+
5
+ def with_command(input_string)
6
+ input = StringIO.new(input_string)
7
+ error = StringIO.new
8
+ output = StringIO.new
9
+ command = Command.new(input, output, error)
10
+ yield command
11
+ output.rewind
12
+ error.rewind
13
+ [output.read, error.read]
14
+ end
15
+
16
+ it "gives sensible error on bad input" do
17
+ input = "adfasd"
18
+ _, err = with_command(input) do |command|
19
+ command.print_pretty
20
+ end
21
+ err.should match /Invalid JSON/
22
+ end
23
+
24
+ it "can pretty print JSON" do
25
+ input =<<-JSON
26
+ [ { "foo" : "bar" } ]
27
+ JSON
28
+ expected_json_out =<<-EOS
29
+ [
30
+ {
31
+ "foo": "bar"
32
+ }
33
+ ]
34
+ EOS
35
+ out, _ = with_command(input) do |command|
36
+ command.print_pretty
37
+ end
38
+ out.should == expected_json_out
39
+ end
40
+
41
+ it "pretty prints out single object" do
42
+ input =<<-JSON
43
+ { "foo" : "bar" }
44
+ JSON
45
+ expected_json_out =<<-EOS
46
+ {
47
+ "foo": "bar"
48
+ }
49
+ EOS
50
+ out, err = with_command(input) do |command|
51
+ command.print_pretty
52
+ end
53
+ out.should == expected_json_out
54
+ end
55
+
56
+ it "can print objects in array on different lines" do
57
+ input =<<-JSON
58
+ [ { "foo" : "bar" }, { "baz" : "baz" } ]
59
+ JSON
60
+ expected_json_out =<<-EOS
61
+ {"foo":"bar"}
62
+ {"baz":"baz"}
63
+ EOS
64
+ out, err = with_command(input) do |command|
65
+ command.print_list
66
+ end
67
+ out.should == expected_json_out
68
+ end
69
+
70
+ it "prints out single object" do
71
+ input =<<-JSON
72
+ { "foo" : "bar" }
73
+ JSON
74
+ expected_json_out =<<-EOS
75
+ {"foo":"bar"}
76
+ EOS
77
+ out, err = with_command(input) do |command|
78
+ command.print_list
79
+ end
80
+ out.should == expected_json_out
81
+ end
82
+
83
+ it "can print ASCII table with all keys" do
84
+ input =<<-JSON
85
+ [ { "name" : "bob", "count" : 1 }, { "name" : "jane", "count" : 2 } ]
86
+ JSON
87
+ expected_json_out =<<-EOS
88
+ +------+-------+
89
+ | name | count |
90
+ +------+-------+
91
+ | bob | 1 |
92
+ | jane | 2 |
93
+ +------+-------+
94
+ EOS
95
+ out, err = with_command(input) do |command|
96
+ command.print_table
97
+ end
98
+ out.should == expected_json_out
99
+ end
100
+
101
+ it "can print ASCII table with select keys" do
102
+ input =<<-JSON
103
+ [ { "name" : "bob", "count" : 1 }, { "name" : "jane", "count" : 2 } ]
104
+ JSON
105
+ expected_json_out =<<-EOS
106
+ +------+
107
+ | name |
108
+ +------+
109
+ | bob |
110
+ | jane |
111
+ +------+
112
+ EOS
113
+ out, err = with_command(input) do |command|
114
+ command.print_table("name")
115
+ end
116
+ out.should == expected_json_out
117
+ end
118
+
119
+ it "can select keys" do
120
+ input =<<-JSON
121
+ [ { "name" : "bob", "count" : 1 }, { "name" : "jane", "count" : 2 } ]
122
+ JSON
123
+ expected_json_out =<<-EOS
124
+ bob 1
125
+ jane 2
126
+ EOS
127
+ out, err = with_command(input) do |command|
128
+ command.print_selected
129
+ end
130
+ out.should == expected_json_out
131
+ end
132
+
133
+ end
@@ -0,0 +1,27 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+
7
+ require 'ki_format'
8
+
9
+ include KiFormat
10
+
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
23
+
24
+ if ENV['COVERAGE']
25
+ require 'simplecov'
26
+ SimpleCov.start
27
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe TableMaker do
4
+
5
+ it "prints table with keys" do
6
+ data = [{ :name => "Bob", :age => 35 },
7
+ { :name => "Sam", :age => 40 }
8
+ ]
9
+ expected = <<-EOS
10
+ +------+-----+
11
+ | name | age |
12
+ +------+-----+
13
+ | Bob | 35 |
14
+ | Sam | 40 |
15
+ +------+-----+
16
+ EOS
17
+
18
+ maker = TableMaker.new(data)
19
+ maker.print.should == expected.chomp
20
+ end
21
+
22
+ it "can select keys" do
23
+ data = [{ :name => "Bob", :age => 35 },
24
+ { :name => "Sam", :age => 40 }
25
+ ]
26
+ expected = <<-EOS
27
+ +------+
28
+ | name |
29
+ +------+
30
+ | Bob |
31
+ | Sam |
32
+ +------+
33
+ EOS
34
+
35
+ maker = TableMaker.new(data, :only => [:name])
36
+ maker.print.should == expected.chomp
37
+ end
38
+
39
+ it "combines all keys for all rows" do
40
+ data = [
41
+ { :name => "Bob", :age => 35 },
42
+ { :name => "Jane", :eyes => "blue" }
43
+ ]
44
+ expected = <<-EOS
45
+ +------+-----+------+
46
+ | name | age | eyes |
47
+ +------+-----+------+
48
+ | Bob | 35 | |
49
+ | Jane | | blue |
50
+ +------+-----+------+
51
+ EOS
52
+ maker = TableMaker.new(data)
53
+ maker.print.should == expected.chomp
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ki_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Brinckerhoff
9
+ - Aaron Cronin
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-08-16 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: multi_json
17
+ requirement: &70206829817800 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70206829817800
26
+ - !ruby/object:Gem::Dependency
27
+ name: methadone
28
+ requirement: &70206829817300 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70206829817300
37
+ - !ruby/object:Gem::Dependency
38
+ name: map
39
+ requirement: &70206829816840 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 4.6.1
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70206829816840
48
+ - !ruby/object:Gem::Dependency
49
+ name: terminal-table
50
+ requirement: &70206829816380 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.4.5
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70206829816380
59
+ - !ruby/object:Gem::Dependency
60
+ name: debugger
61
+ requirement: &70206856634200 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.2.0
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70206856634200
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: &70206856633740 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70206856633740
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &70206856633280 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 2.11.0
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70206856633280
92
+ - !ruby/object:Gem::Dependency
93
+ name: simplecov
94
+ requirement: &70206856632820 !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ~>
98
+ - !ruby/object:Gem::Version
99
+ version: 0.6.4
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *70206856632820
103
+ description: Formats JSON
104
+ email:
105
+ - ben@freeagent.com
106
+ - aaron@freeagent.com
107
+ executables:
108
+ - ki_format
109
+ extensions: []
110
+ extra_rdoc_files: []
111
+ files:
112
+ - .gitignore
113
+ - .rspec
114
+ - Gemfile
115
+ - LICENSE
116
+ - README.md
117
+ - Rakefile
118
+ - bin/ki_format
119
+ - ki_format.gemspec
120
+ - lib/ki_format.rb
121
+ - lib/ki_format/command.rb
122
+ - lib/ki_format/table_maker.rb
123
+ - lib/ki_format/version.rb
124
+ - spec/integration/commands_spec.rb
125
+ - spec/spec_helper.rb
126
+ - spec/unit/table_maker_spec.rb
127
+ homepage: ''
128
+ licenses: []
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 1.8.10
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Formats JSON
151
+ test_files:
152
+ - spec/integration/commands_spec.rb
153
+ - spec/spec_helper.rb
154
+ - spec/unit/table_maker_spec.rb