json2table 1.0.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.
- checksums.yaml +7 -0
- data/lib/json2table.rb +145 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b464bb1c19b0ea7af318814d3da874d0842f44b7
|
4
|
+
data.tar.gz: a0c98c51aee2a3f6ae14964ec157831755215ab2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b95ef24549e6644f59b4348afe5a7c83ac03233c2cfb0f2b90710af88c9d3696087261a980bdcf369a208ea740d866d4517d9165ecc59f30f04faf50d0dc7c1b
|
7
|
+
data.tar.gz: 5eec3646905acafbe2464eb216e1843397aecbe827227737cc577bf9b7aa0d93f5217f9ea1af6ae052d52d7feccd403bd97adb6147ec332da55b79ebb6177083
|
data/lib/json2table.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# This module contains the entire source code needed to
|
2
|
+
# convert a JSON blob into a HTML Table.
|
3
|
+
#
|
4
|
+
# The gem is at http://rubygems.org/gems/json2table
|
5
|
+
# To install the gem, run command `gem install json2table`
|
6
|
+
# @author CodeExpress
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
require 'set'
|
10
|
+
|
11
|
+
module Json2table
|
12
|
+
|
13
|
+
def self.get_html_table(json_str, options = {})
|
14
|
+
html = ""
|
15
|
+
begin
|
16
|
+
hash = JSON.parse(json_str)
|
17
|
+
rescue Exception => e
|
18
|
+
puts "JSON2TABLE:: Input not a valid JSON, provide valid JSON object"
|
19
|
+
puts e.message
|
20
|
+
throw e
|
21
|
+
end
|
22
|
+
html = self.create_table(hash, options)
|
23
|
+
return html
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.create_table(hash, options)
|
27
|
+
html = start_table_tag(options)
|
28
|
+
hash.each do |key, value|
|
29
|
+
# key goes in a column and value in second column of the same row
|
30
|
+
html += "<tr><th>#{to_human(key)}</th>\n"
|
31
|
+
html += "<td>"
|
32
|
+
|
33
|
+
if value.is_a?(Hash)
|
34
|
+
# create a row with key as heading and body
|
35
|
+
# as another table
|
36
|
+
html += create_table(value, options)
|
37
|
+
elsif value.is_a?(Array)
|
38
|
+
if value[0].is_a?(Hash) # Array of hashes
|
39
|
+
keys = similar_keys?(value)
|
40
|
+
if keys
|
41
|
+
# if elements of this array are hashes with same keys,
|
42
|
+
# display it as a top-down table
|
43
|
+
html += create_vertical_table_from_array(value, keys, options)
|
44
|
+
else
|
45
|
+
# non similar keys, create horizontal table
|
46
|
+
value.each do |h|
|
47
|
+
html += create_table(h, options)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
else
|
51
|
+
# array of a primitive data types eg. [1,2,3]
|
52
|
+
# all values can be displayed in in cell
|
53
|
+
html += "#{value}</td></tr>\n"
|
54
|
+
end
|
55
|
+
else # simple primitive data type of value (non hash, non array)
|
56
|
+
html += "#{value}</td></tr>\n"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
html += close_table_tag
|
60
|
+
return html
|
61
|
+
end
|
62
|
+
|
63
|
+
# This method checks if all the individual array items
|
64
|
+
# are hashes with similar keys.
|
65
|
+
def self.similar_keys?(arr)
|
66
|
+
previous_keys = Set.new
|
67
|
+
current_keys = Set.new
|
68
|
+
arr.each do |hash|
|
69
|
+
# every item of the array should be a hash, if not return false
|
70
|
+
return nil if not hash.is_a?(Hash)
|
71
|
+
current_keys = hash.keys.to_set
|
72
|
+
if previous_keys.empty?
|
73
|
+
previous_keys = current_keys # happens on first iteration
|
74
|
+
else
|
75
|
+
# if different keys in two distinct array elements(hashes), return false
|
76
|
+
return nil if not (previous_keys^current_keys).empty?
|
77
|
+
previous_keys = current_keys
|
78
|
+
end
|
79
|
+
end
|
80
|
+
return arr[0].keys # all array elements were hash with same keys
|
81
|
+
end
|
82
|
+
|
83
|
+
# creates a vertical table of following form for the array of hashes like this:
|
84
|
+
# ---------------------
|
85
|
+
# | key1 | key2 | key3 |
|
86
|
+
# ---------------------
|
87
|
+
# | val1 | val2 | val3 |
|
88
|
+
# | val4 | val5 | val6 |
|
89
|
+
# | val9 | val8 | val7 |
|
90
|
+
# ---------------------
|
91
|
+
def self.create_vertical_table_from_array(array_of_hashes, keys, options)
|
92
|
+
html = start_table_tag(options)
|
93
|
+
html += "<tr>\n"
|
94
|
+
keys.each do |key|
|
95
|
+
html += "<th>#{to_human(key)}</th>\n"
|
96
|
+
end
|
97
|
+
html += "</tr>\n"
|
98
|
+
array_of_hashes.each do |hash|
|
99
|
+
html += "<tr>\n"
|
100
|
+
keys.each do |key|
|
101
|
+
if hash[key].is_a?(Hash) # another hash, create table out of it
|
102
|
+
html += "<td>#{create_table(hash[key], options)}</td>\n"
|
103
|
+
elsif hash[key].is_a?(Array)
|
104
|
+
if hash[key][0].is_a?(Hash) # Array of hashes
|
105
|
+
k = similar_keys?(hash[key])
|
106
|
+
if k
|
107
|
+
# if elements of this array are hashes with same keys,
|
108
|
+
# display it as a top-down table
|
109
|
+
html += create_vertical_table_from_array(value, k, options)
|
110
|
+
else
|
111
|
+
# non similar keys, create horizontal table
|
112
|
+
hash[key].each do |h|
|
113
|
+
html += create_table(h, options)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
else
|
118
|
+
html += "<td>#{to_human(hash[key])}</td>\n"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
html += "</tr>\n"
|
122
|
+
end
|
123
|
+
html += self.close_table_tag
|
124
|
+
end
|
125
|
+
def self.start_table_tag(options)
|
126
|
+
"<table class='#{options[:table_class]}'
|
127
|
+
style='#{options[:table_style]}'
|
128
|
+
#{options[:table_attributes]} >\n"
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.close_table_tag
|
132
|
+
"</table>\n"
|
133
|
+
end
|
134
|
+
|
135
|
+
# turns CamelCase and snake_case keys to human readable strings
|
136
|
+
# Input: this_isA_mixedCAse_line-string
|
137
|
+
# Output: "This Is A Mixed C Ase Line String"
|
138
|
+
def self.to_human(key)
|
139
|
+
key.gsub(/::/, '/').
|
140
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2').
|
141
|
+
gsub(/([a-z\d])([A-Z])/,'\1 \2').
|
142
|
+
tr("-", " ").tr("_", " ").
|
143
|
+
split.map {|word| word.capitalize}.join(" ")
|
144
|
+
end
|
145
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json2table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Code Express
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |-
|
14
|
+
This gem provides functionality to convert a JSON object into HTML
|
15
|
+
table. It can handle nested JSONs. Table class, styles and
|
16
|
+
attributes can be provided.
|
17
|
+
email: code.expres@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/json2table.rb
|
23
|
+
homepage: https://github.com/codeexpress/json2table
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.6
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Converts JSON to HTML tables
|
47
|
+
test_files: []
|