Module: Json2table
- Defined in:
- lib/json2table.rb
Class Method Summary collapse
- .close_table_tag ⇒ Object
- .create_table(hash, options) ⇒ Object
-
.create_vertical_table_from_array(array_of_hashes, keys, options) ⇒ Object
creates a vertical table of following form for the array of hashes like this: ——————— | key1 | key2 | key3 | ——————— | val1 | val2 | val3 | | val4 | val5 | val6 | | val9 | val8 | val7 | ———————.
- .get_html_table(json_str, options = {}) ⇒ Object
-
.similar_keys?(arr) ⇒ Boolean
This method checks if all the individual array items are hashes with similar keys.
- .start_table_tag(options) ⇒ Object
-
.to_human(key) ⇒ Object
turns CamelCase and snake_case keys to human readable strings Input: this_isA_mixedCAse_line-string Output: “This Is A Mixed C Ase Line String”.
Class Method Details
.close_table_tag ⇒ Object
131 132 133 |
# File 'lib/json2table.rb', line 131 def self.close_table_tag "</table>\n" end |
.create_table(hash, options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/json2table.rb', line 26 def self.create_table(hash, ) html = start_table_tag() hash.each do |key, value| # key goes in a column and value in second column of the same row html += "<tr><th>#{to_human(key)}</th>\n" html += "<td>" if value.is_a?(Hash) # create a row with key as heading and body # as another table html += create_table(value, ) elsif value.is_a?(Array) if value[0].is_a?(Hash) # Array of hashes keys = similar_keys?(value) if keys # if elements of this array are hashes with same keys, # display it as a top-down table html += create_vertical_table_from_array(value, keys, ) else # non similar keys, create horizontal table value.each do |h| html += create_table(h, ) end end else # array of a primitive data types eg. [1,2,3] # all values can be displayed in in cell html += "#{value}</td></tr>\n" end else # simple primitive data type of value (non hash, non array) html += "#{value}</td></tr>\n" end end html += close_table_tag return html end |
.create_vertical_table_from_array(array_of_hashes, keys, options) ⇒ Object
creates a vertical table of following form for the array of hashes like this:
---------------------
| key1 | key2 | key3 |
---------------------
| val1 | val2 | val3 |
| val4 | val5 | val6 |
| val9 | val8 | val7 |
---------------------
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/json2table.rb', line 91 def self.create_vertical_table_from_array(array_of_hashes, keys, ) html = start_table_tag() html += "<tr>\n" keys.each do |key| html += "<th>#{to_human(key)}</th>\n" end html += "</tr>\n" array_of_hashes.each do |hash| html += "<tr>\n" keys.each do |key| if hash[key].is_a?(Hash) # another hash, create table out of it html += "<td>#{create_table(hash[key], )}</td>\n" elsif hash[key].is_a?(Array) if hash[key][0].is_a?(Hash) # Array of hashes k = similar_keys?(hash[key]) if k # if elements of this array are hashes with same keys, # display it as a top-down table html += create_vertical_table_from_array(value, k, ) else # non similar keys, create horizontal table hash[key].each do |h| html += create_table(h, ) end end end else html += "<td>#{to_human(hash[key])}</td>\n" end end html += "</tr>\n" end html += self.close_table_tag end |
.get_html_table(json_str, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/json2table.rb', line 13 def self.get_html_table(json_str, = {}) html = "" begin hash = JSON.parse(json_str) rescue Exception => e puts "JSON2TABLE:: Input not a valid JSON, provide valid JSON object" puts e. throw e end html = self.create_table(hash, ) return html end |
.similar_keys?(arr) ⇒ Boolean
This method checks if all the individual array items are hashes with similar keys.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/json2table.rb', line 65 def self.similar_keys?(arr) previous_keys = Set.new current_keys = Set.new arr.each do |hash| # every item of the array should be a hash, if not return false return nil if not hash.is_a?(Hash) current_keys = hash.keys.to_set if previous_keys.empty? previous_keys = current_keys # happens on first iteration else # if different keys in two distinct array elements(hashes), return false return nil if not (previous_keys^current_keys).empty? previous_keys = current_keys end end return arr[0].keys # all array elements were hash with same keys end |
.start_table_tag(options) ⇒ Object
125 126 127 128 129 |
# File 'lib/json2table.rb', line 125 def self.start_table_tag() "<table class='#{[:table_class]}' style='#{[:table_style]}' #{[:table_attributes]} >\n" end |
.to_human(key) ⇒ Object
turns CamelCase and snake_case keys to human readable strings Input: this_isA_mixedCAse_line-string Output: “This Is A Mixed C Ase Line String”
138 139 140 141 142 143 144 |
# File 'lib/json2table.rb', line 138 def self.to_human(key) key.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2'). gsub(/([a-z\d])([A-Z])/,'\1 \2'). tr("-", " ").tr("_", " "). split.map {|word| word.capitalize}.join(" ") end |