jpp 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/bentonporter/jpp.png?branch=master)](https://travis-ci.org/bentonporter/jpp)
2
+
1
3
  jpp
2
4
  ===
3
5
 
@@ -16,8 +18,9 @@ Or pretty-print JSON from a file:
16
18
 
17
19
  More usage:
18
20
 
19
- >> jpp -h benton.porter@bhollis-mbpro
21
+ >> jpp -h
20
22
  Usage: jpp [options] file
23
+ -i, --indent N Number of spaces to indent (default: 2)
21
24
  -s, --sort Sort json alphabetically by key
22
25
  -v, --version Show version
23
26
 
@@ -35,4 +38,4 @@ To build and run locally:
35
38
 
36
39
  git clone git@github.com:bentonporter/jpp.git
37
40
  cd jpp
38
- gem build jpp.gemspec && gem install jpp-1.0.0.gem
41
+ gem build jpp.gemspec && gem install jpp-1.1.0.gem
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/bin/jpp CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'jpp'
4
- Jpp.new.main
4
+ Jpp::Jpp.new.main
@@ -1,8 +1,10 @@
1
1
  class Hash
2
2
  def sort_by_key(&block)
3
- self.keys.sort(&block).reduce({}) do |seed, key|
4
- seed[key] = self[key]
5
- seed
3
+ sorted = {}
4
+ self.keys.sort(&block).each do |key|
5
+ value = self[key]
6
+ sorted[key] = value.is_a?(Hash) ? value.sort_by_key(&block) : value
6
7
  end
8
+ sorted
7
9
  end
8
10
  end
data/lib/jpp.rb CHANGED
@@ -1,44 +1,21 @@
1
1
  require 'json'
2
- require 'optparse'
3
- require 'ostruct'
4
2
  require 'hash'
5
-
6
- class Jpp
7
- VERSION = '1.0.0'
8
-
9
- def main()
10
- # Set default values
11
- options = OpenStruct.new
12
- options.indent = 2
13
- options.sort = false
14
-
15
- parser = OptionParser.new do |opts|
16
- opts.banner = 'Usage: jpp [options] file'
17
-
18
- opts.on('-i', '--indent N', Integer, "Number of spaces to indent (default: #{options.indent}) ") do |i|
19
- options.indent = i
20
- end
21
-
22
- opts.on('-s', '--sort', 'Sort json alphabetically by key') do |s|
23
- options.sort = s
24
- end
25
-
26
- opts.on('-v', '--version', 'Show version') do |v|
3
+ require 'jpp/command_line'
4
+
5
+ module Jpp
6
+ class Jpp
7
+ def main()
8
+ options = CommandLine.parse(ARGV)
9
+
10
+ if (options.version)
27
11
  puts VERSION
28
12
  exit
29
13
  end
30
- end
31
14
 
32
- begin parser.parse!
33
- rescue OptionParser::ParseError => message
34
- puts message
35
- puts parser
36
- exit 1
15
+ input = JSON.load(ARGF.read)
16
+ input = input.sort_by_key() if options.sort and input.is_a?(Hash) # TODO support sorting hashes within an array
17
+ output = JSON.pretty_generate(input, :indent => ' ' * options.indent)
18
+ puts output
37
19
  end
38
-
39
- input = JSON.load(ARGF.read)
40
- input = input.sort_by_key() if options.sort
41
- output = JSON.pretty_generate(input, :indent => ' ' * options.indent)
42
- puts output
43
20
  end
44
21
  end
@@ -0,0 +1,39 @@
1
+ require 'optparse'
2
+ require 'ostruct'
3
+ require 'jpp/version'
4
+
5
+ module Jpp
6
+ class CommandLine
7
+ def self.parse(args)
8
+ options = OpenStruct.new
9
+ options.indent = 2
10
+ options.sort = false
11
+ options.version = false
12
+
13
+ parser = OptionParser.new do |opts|
14
+ opts.banner = 'Usage: jpp [options] file'
15
+
16
+ opts.on('-i', '--indent N', Integer, "Number of spaces to indent (default: #{options.indent}) ") do |i|
17
+ options.indent = i
18
+ end
19
+
20
+ opts.on('-s', '--sort', 'Sort json alphabetically by key') do |s|
21
+ options.sort = s
22
+ end
23
+
24
+ opts.on('-v', '--version', 'Show version') do |v|
25
+ options.version = v
26
+ end
27
+ end
28
+
29
+ begin parser.parse!(args)
30
+ rescue OptionParser::ParseError => message
31
+ puts message
32
+ puts parser
33
+ exit 1 # TODO throw/propagate and let Jpp::Jpp exit
34
+ end
35
+
36
+ options
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Jpp
2
+ VERSION = '1.1.0'
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'test/unit'
2
+ require 'jpp/command_line'
3
+
4
+ class TestCommandLine < Test::Unit::TestCase
5
+
6
+ def test_default_sort
7
+ options = Jpp::CommandLine.parse([])
8
+ assert !options.sort
9
+ end
10
+
11
+ def test_default_indent
12
+ options = Jpp::CommandLine.parse([])
13
+ assert_equal 2, options.indent
14
+ end
15
+
16
+ def test_default_version
17
+ options = Jpp::CommandLine.parse([])
18
+ assert !options.version
19
+ end
20
+
21
+ def test_sort
22
+ options = Jpp::CommandLine.parse(['-s'])
23
+ assert options.sort
24
+ end
25
+
26
+ def test_indent
27
+ options = Jpp::CommandLine.parse(['-i', '3'])
28
+ assert_equal 3, options.indent
29
+ end
30
+
31
+ def test_version
32
+ options = Jpp::CommandLine.parse(['-v'])
33
+ assert options.version
34
+ end
35
+
36
+ end
@@ -0,0 +1,69 @@
1
+ $:.unshift File.expand_path("../../ext", __FILE__)
2
+ require 'test/unit'
3
+ require 'hash'
4
+
5
+ class TestHash < Test::Unit::TestCase
6
+
7
+ def test_that_it_sorts_strings
8
+ orig = {
9
+ 'zoo' => 1,
10
+ 'apple' => 2,
11
+ 'pizza' => 3
12
+ }
13
+
14
+ sorted = orig.sort_by_key
15
+ assert_equal ['apple', 'pizza', 'zoo'], sorted.keys
16
+ end
17
+
18
+ def test_that_it_sorts_integers
19
+ orig = {
20
+ 4 => 1,
21
+ 1 => 2,
22
+ 3 => 3
23
+ }
24
+
25
+ sorted = orig.sort_by_key
26
+ assert_equal [1, 3, 4], sorted.keys
27
+ end
28
+
29
+ def test_that_it_sorts_special_chars
30
+ orig = {
31
+ 'aaa' => 1,
32
+ '~ccc' => 2,
33
+ 'zzz' => 3
34
+ }
35
+
36
+ sorted = orig.sort_by_key
37
+ assert_equal ['aaa', 'zzz', '~ccc'], sorted.keys
38
+ end
39
+
40
+ def test_that_it_sorts_nested_hashes
41
+ orig = {
42
+ 'bob' => 5,
43
+ 'anne' => {
44
+ 'fred' => 1,
45
+ 'catherine' => 9
46
+ }
47
+ }
48
+
49
+ sorted = orig.sort_by_key
50
+ assert_equal ['anne', 'bob'], sorted.keys
51
+ assert_equal ['catherine', 'fred'], sorted['anne'].keys
52
+ end
53
+
54
+ # This is not currently supported, so might as well verify the expected behavior
55
+ def test_that_hashes_inside_an_array_are_not_sorted
56
+ orig = {
57
+ 'horse' => 5,
58
+ 'frog' => [{
59
+ 'zebra' => 1,
60
+ 'lion' => 9
61
+ }]
62
+ }
63
+
64
+ sorted = orig.sort_by_key
65
+ assert_equal ['frog', 'horse'], sorted.keys
66
+ assert_equal ['zebra', 'lion'], sorted['frog'].first.keys
67
+ end
68
+
69
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-08 00:00:00.000000000 Z
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -34,11 +34,16 @@ executables:
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
+ - Rakefile
37
38
  - bin/jpp
38
39
  - ext/hash.rb
40
+ - lib/jpp/command_line.rb
41
+ - lib/jpp/version.rb
39
42
  - lib/jpp.rb
40
- - LICENSE.md
43
+ - test/test_command_line.rb
44
+ - test/test_hash.rb
41
45
  - README.md
46
+ - LICENSE.md
42
47
  homepage: https://github.com/bentonporter/jpp
43
48
  licenses: []
44
49
  post_install_message: