Module: Json2table

Defined in:
lib/json2table.rb

Class Method Summary collapse

Class Method Details

.close_table_tagObject



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, options)
  html = start_table_tag(options)
  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, options)
    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, options)
        else
          # non similar keys, create horizontal table
          value.each do |h|
            html += create_table(h, options)
          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, options)
  html = start_table_tag(options)
  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], options)}</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, options)
          else
            # non similar keys, create horizontal table
            hash[key].each do |h|
              html += create_table(h, options)
            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, options = {})
  html = ""
  begin
    hash = JSON.parse(json_str)
  rescue Exception => e
    puts "JSON2TABLE:: Input not a valid JSON, provide valid JSON object"
    puts e.message
    throw e
  end
  html = self.create_table(hash, options)
  return html
end

.similar_keys?(arr) ⇒ Boolean

This method checks if all the individual array items are hashes with similar keys.

Returns:

  • (Boolean)


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(options)
  "<table class='#{options[:table_class]}' 
          style='#{options[:table_style]}'
          #{options[: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