json-colorizer 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/json-colorizer.gemspec +17 -0
- data/lib/json-colorizer.rb +56 -0
- metadata +65 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.authors = ['Taras Kunch']
|
3
|
+
gem.email = ['tkunch@rebbix.com']
|
4
|
+
|
5
|
+
description = 'Tool for pretty printing json as log'
|
6
|
+
gem.description = description
|
7
|
+
gem.summary = description
|
8
|
+
|
9
|
+
gem.files = `git ls-files`.split($\)
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
12
|
+
gem.require_paths = ['lib']
|
13
|
+
gem.name = 'json-colorizer'
|
14
|
+
gem.version = '0.0.1'
|
15
|
+
|
16
|
+
gem.add_dependency('colorize', '0.5.8')
|
17
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
class JsonColorizer
|
4
|
+
def initialize(schema)
|
5
|
+
@schema = schema
|
6
|
+
@schema_keys = @schema.keys
|
7
|
+
@other = schema.delete(:_other)
|
8
|
+
end
|
9
|
+
|
10
|
+
def format(data)
|
11
|
+
parts = []
|
12
|
+
@schema.each do |k, formatters|
|
13
|
+
key = k.to_s
|
14
|
+
formatters = [formatters] unless formatters.is_a?(Array)
|
15
|
+
parts << apply_formatters(data[key], formatters)
|
16
|
+
end
|
17
|
+
|
18
|
+
unless @other.nil?
|
19
|
+
parts << data.select{ |k, v| !@schema_keys.include?(k) }.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
parts.join(' ')
|
23
|
+
end
|
24
|
+
|
25
|
+
def apply_formatters(data, formatters)
|
26
|
+
return '' if data.nil?
|
27
|
+
|
28
|
+
formatters.reduce(data) do |output, formatter|
|
29
|
+
case formatter
|
30
|
+
when Proc
|
31
|
+
formatter.call(output)
|
32
|
+
when Symbol
|
33
|
+
self.send(formatter, output)
|
34
|
+
else
|
35
|
+
output
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
String::COLORS.each_key do | key |
|
41
|
+
next if key == :default
|
42
|
+
|
43
|
+
define_method key do |s|
|
44
|
+
self.colorize(s, :color => key)
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method "on_#{key}" do |s|
|
48
|
+
self.colorize(s, :background => key)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def colorize(s, color_options)
|
53
|
+
s.to_s.colorize(color_options)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-colorizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Taras Kunch
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: colorize
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.5.8
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.5.8
|
30
|
+
description: Tool for pretty printing json as log
|
31
|
+
email:
|
32
|
+
- tkunch@rebbix.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- json-colorizer.gemspec
|
40
|
+
- lib/json-colorizer.rb
|
41
|
+
homepage:
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.23
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Tool for pretty printing json as log
|
65
|
+
test_files: []
|