json_cat 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b73da8c08a289821edb8b450b72eabdb224adcaf
4
- data.tar.gz: 37ba4d1809d1200c220303b6a8f50e7ef27e2e3d
3
+ metadata.gz: c85bbe2eaf7e7864c651518dd06808c7154e04fe
4
+ data.tar.gz: 283627cc99f79c29e915a9725eef4df9356176bd
5
5
  SHA512:
6
- metadata.gz: c792d7c1e68af9bb0af9dbddb1c4c43cfc57cc735834598739006bcdf60b0d212e6ac176047a7ed0611be1fe26bddbd0083ff50584c8c0dbc93d0d445e3e8d23
7
- data.tar.gz: ffdf6313d8f24af0ac1a40dfbfa352f66ba1ef449511a3fda60731f7b72740dcdda34bcaff272eb7fdbdcdd6d54dac70bf9a241cb3cffee71ae06cde07216627
6
+ metadata.gz: e92ec93685d139c8924414e7b8ae94f58fbd3c6266bc18dc2837904331daa7b5ec6cec855fe34db3e587012a3dec61956b4381bea9332604cb4d0372e586f562
7
+ data.tar.gz: 76652b97d4c3b3160ea0474619dca218c8d98e25ccb44868ef806faa659056536c35cca4c8348fe16a9b17710a9827ba8fa0b3b3a6c2cf8e1aabfc52ae7c5f9e
data/bin/console CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
3
- require "bundler/setup"
4
- require "json_cat"
4
+ require 'bundler/setup'
5
+ require 'json_cat'
5
6
 
6
- require "irb"
7
+ require 'irb'
7
8
  IRB.start
data/bin/json_cat CHANGED
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
+ # encoding: utf-8
2
3
 
4
+ require 'english'
3
5
  require 'optparse'
4
6
 
5
7
  require 'json_cat'
6
8
 
7
-
8
9
  options = {
9
10
  grep: nil,
10
11
  omit: [],
@@ -14,52 +15,48 @@ options = {
14
15
  }
15
16
 
16
17
  OptionParser.new do |opts|
17
- opts.banner = "Usage: json_cat [options]"
18
+ opts.banner = 'Usage: json_cat [options]'
18
19
 
19
- opts.on("-c", "--[no-]color", "Colorize output (default: --color)") do |c|
20
+ opts.on('-c', '--[no-]color', 'Colorize output (default: --color)') do |c|
20
21
  Rainbow.enabled = c
21
22
  end
22
23
 
23
- opts.on("--grep key", "Filter out JSON without key") do |key|
24
+ opts.on('--grep key', 'Filter out JSON without key') do |key|
24
25
  options[:grep] = key
25
26
  end
26
27
 
27
- opts.on("-h", "--help", "Prints this help") do
28
+ opts.on('-h', '--help', 'Prints this help') do
28
29
  puts opts
29
30
  exit
30
31
  end
31
32
 
32
- opts.on("-o", "--omit key[,key]", Array, "Omit keys from JSON") do |keys|
33
+ opts.on('-o', '--omit key[,key]', Array, 'Omit keys from JSON') do |keys|
33
34
  options[:omit] = keys
34
35
  end
35
36
 
36
- opts.on("-p", "--parse key[,key]", Array, "Parse Key-value as JSON") do |keys|
37
+ opts.on('-p', '--parse key[,key]', Array, 'Parse Key-value as JSON') do |keys|
37
38
  options[:parse] = keys
38
39
  end
39
40
 
40
- opts.on("-s", "--[no-]sort", "Sort keys") do |s|
41
+ opts.on('-s', '--[no-]sort', 'Sort keys') do |s|
41
42
  options[:sort] = s
42
43
  end
43
44
 
44
- opts.on("-t", "--[no-]timestamp", "Timestamp lines") do |t|
45
+ opts.on('-t', '--[no-]timestamp', 'Timestamp lines') do |t|
45
46
  options[:timestamp] = t
46
47
  end
47
48
  end.parse!
48
49
 
49
-
50
50
  jp = JsonCat::JsonPrettyPrinter.new(options)
51
-
52
51
  begin
53
- if ARGV.length > 0
52
+ if !ARGV.empty?
54
53
  ARGV.each do |filename|
55
54
  File.open(filename) do |f|
56
55
  f.each_line { |line| jp.print line }
57
56
  end
58
57
  end
59
58
  else
60
- while gets()
61
- jp.print $_
62
- end
59
+ jp.print $LAST_READ_LINE while gets
63
60
  end
64
61
  rescue Interrupt
65
62
  end
data/json_cat.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = %q{Pretty print JSON files and standard input}
14
14
  spec.description = %q{Format JSON to be easier to read; colorize, sort keys, etc. }
15
- spec.homepage = "https://github.com/sanscore/./"
15
+ spec.homepage = "https://github.com/sanscore/json_cat/"
16
16
 
17
17
  if spec.respond_to?(:metadata)
18
18
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
@@ -1,21 +1,33 @@
1
+ # encoding: utf-8
1
2
 
2
3
  class Array
3
4
  def rainbow_print(level=0)
4
5
  indent = ' ' * (level * 2)
5
- if self.length == 0
6
- return "#{indent}#{Rainbow("[]").color(JsonCat::COLORS[level])}"
6
+ if length == 0
7
+ return "#{indent}#{Rainbow('[]').color(JsonCat::COLORS[level])}"
7
8
  end
8
9
 
9
- open_br = "#{indent}#{Rainbow("[").color(JsonCat::COLORS[level])}"
10
- close_br = "#{indent}#{Rainbow("]").color(JsonCat::COLORS[level])}"
10
+ rainbow_print_build(level)
11
+ end
12
+
13
+ private
14
+
15
+ def rainbow_print_open_bracket(level)
16
+ Rainbow('[').color(JsonCat::COLORS[level])
17
+ end
18
+
19
+ def rainbow_print_close_bracket(level)
20
+ Rainbow(']').color(JsonCat::COLORS[level])
21
+ end
11
22
 
12
- ret = [open_br]
13
- self.each do |k|
23
+ def rainbow_print_build(level)
24
+ ret = [rainbow_print_open_bracket(level)]
25
+ each do |k|
14
26
  indent = ' ' * ((level + 1) * 2)
15
- rkey = k.rainbow_print(level+1).lstrip
27
+ rkey = k.rainbow_print(level + 1).lstrip
16
28
  ret << "#{indent}#{rkey},"
17
29
  end
18
- ret << close_br
19
- return ret.join("\n")
30
+ ret << rainbow_print_close_bracket(level)
31
+ ret.join("\n")
20
32
  end
21
33
  end
@@ -1,22 +1,34 @@
1
+ # encoding: utf-8
1
2
 
2
3
  class Hash
3
4
  def rainbow_print(level=0)
4
5
  indent = ' ' * (level * 2)
5
- if self.length == 0
6
- return "#{indent}#{Rainbow("{}").color(JsonCat::COLORS[level])}"
6
+ if length == 0
7
+ return "#{indent}#{Rainbow('{}').color(JsonCat::COLORS[level])}"
7
8
  end
8
9
 
9
- open_br = "#{indent}#{Rainbow("{").color(JsonCat::COLORS[level])}"
10
- close_br = "#{indent}#{Rainbow("}").color(JsonCat::COLORS[level])}"
10
+ rainbow_print_build(level)
11
+ end
12
+
13
+ private
14
+
15
+ def rainbow_print_open_bracket(level)
16
+ Rainbow('{').color(JsonCat::COLORS[level])
17
+ end
18
+
19
+ def rainbow_print_close_bracket(level)
20
+ Rainbow('}').color(JsonCat::COLORS[level])
21
+ end
11
22
 
12
- ret = [open_br]
13
- self.each do |k, v|
23
+ def rainbow_print_build(level)
24
+ ret = [rainbow_print_open_bracket(level)]
25
+ each do |k, v|
14
26
  indent = ' ' * ((level + 1) * 2)
15
- rkey = k.rainbow_print(level+1)
16
- rval = v.rainbow_print(level+2).lstrip
27
+ rkey = k.rainbow_print(level + 1)
28
+ rval = v.rainbow_print(level + 2).lstrip
17
29
  ret << "#{indent}#{rkey}: #{rval},"
18
30
  end
19
- ret << close_br
20
- return ret.join("\n")
31
+ ret << rainbow_print_close_bracket(level)
32
+ ret.join("\n")
21
33
  end
22
34
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
 
2
3
  module Kernel
3
4
  def bow_print(obj)
@@ -1,6 +1,7 @@
1
+ # encoding: utf-8
1
2
 
2
3
  class Object
3
- def rainbow_print(level=0)
4
- return "#{Rainbow(self.to_json).color(JsonCat::COLORS[level])}"
4
+ def rainbow_print(level = 0)
5
+ Rainbow(to_json).color(JsonCat::COLORS[level])
5
6
  end
6
7
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module JsonCat
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'.freeze
3
5
  end
data/lib/json_cat.rb CHANGED
@@ -1,22 +1,24 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'json'
2
4
  require 'rainbow'
3
5
 
4
- require "json_cat/version"
5
- require "json_cat/ext/array"
6
- require "json_cat/ext/hash"
7
- require "json_cat/ext/kernel"
8
- require "json_cat/ext/object"
6
+ require 'json_cat/version'
7
+ require 'json_cat/ext/array'
8
+ require 'json_cat/ext/hash'
9
+ require 'json_cat/ext/kernel'
10
+ require 'json_cat/ext/object'
9
11
 
10
12
  module JsonCat
11
13
  COLORS = %i(blue chartreuse cornflower red green darkviolet
12
- orangered magenta yellow cyan coral limegreen dodgerblue
13
- gold indigo)
14
+ orangered magenta yellow cyan coral limegreen dodgerblue
15
+ gold indigo).freeze
14
16
 
15
17
  class JsonPrettyPrinter
16
18
  attr_accessor :count, :grep, :omit, :parse, :sort, :timestamp
17
19
  def initialize(**opts)
18
20
  self.count = 0
19
- opts.each { |k,v| self.instance_variable_set("@#{k}", v) }
21
+ opts.each { |k, v| instance_variable_set("@#{k}", v) }
20
22
  end
21
23
 
22
24
  def print(line)
@@ -29,17 +31,16 @@ module JsonCat
29
31
  end
30
32
 
31
33
  private
34
+
32
35
  def print_hash(json)
33
- return if grep and not json[grep]
36
+ return if grep && !json[grep]
34
37
  self.count += 1
35
38
 
36
- x = json.delete_if { |k,v| omit.include?(k) }
37
- x.each {|k,v| x[k] = parse.include?(k) ? json_parse(v) : v }
39
+ x = json.delete_if { |k, _v| omit.include?(k) }
40
+ x.each { |k, v| x[k] = parse.include?(k) ? json_parse(v) : v }
38
41
  x = x.sort.to_h if sort
39
42
 
40
- if timestamp
41
- x = {"timestamp": Time.now.httpdate, "line-#{count}": x}
42
- end
43
+ x['__json_cat_ts'] = Time.now.httpdate if timestamp
43
44
  bow_print x
44
45
  end
45
46
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_cat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Welch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-02 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -105,7 +105,7 @@ files:
105
105
  - lib/json_cat/ext/object.rb
106
106
  - lib/json_cat/version.rb
107
107
  - tags
108
- homepage: https://github.com/sanscore/./
108
+ homepage: https://github.com/sanscore/json_cat/
109
109
  licenses:
110
110
  - Apache-2.0
111
111
  metadata: