Module: Json2table

Defined in:
lib/json2table.rb

Class Method Summary collapse

Class Method Details

.close_table_tagObject



157
158
159
# File 'lib/json2table.rb', line 157

def self.close_table_tag
  "</table>\n"
end

.create_table(hash, options) ⇒ Object



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
# File 'lib/json2table.rb', line 30

def self.create_table(hash, options)
  html = start_table_tag(options)
  if hash.is_a?(Array)
    html += "<tr><td>\n"
    #puts ">>>> #{process_array(hash, options)}"
    html += process_array(hash, options)
  elsif hash.is_a?(Hash)
    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)
        html += process_array(value, options)
      else      # simple primitive data type of value (non hash, non array)
        html += "#{value}</td></tr>\n"
      end
    end
  else      # simple primitive data type of value (non hash, non array)
    html += "<tr><td>#{hash}</td></tr>\n"
  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  |
 ---------------------


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/json2table.rb', line 110

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)
        html += "<td>\n"
        html += process_array(hash[key], options)
        html += "</td>\n"
        # 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 += "<td>\n"
        #     html += create_vertical_table_from_array(hash[key], k, options)
        #     html += "</td>\n"
        #   else
        #     # non similar keys, create horizontal table
        #     hash[key].each do |h|
        #       html += create_table(h, options)
        #     end
        #   end
        # else
        #   html += "<td>#{hash[key]}</td>\n"
        # end
      else
        html += "<td>#{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
25
26
27
28
# File 'lib/json2table.rb', line 13

def self.get_html_table(json_str, options = {})
  html = ""
  begin
    if json_str.is_a?(Hash)
      hash = json_str
    else
      hash = JSON.parse(json_str)
    end
  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

.process_array(arr, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/json2table.rb', line 58

def self.process_array(arr, options)
  html = ""
  if arr[0].is_a?(Hash) # Array of hashes
    keys = similar_keys?(arr)
    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(arr, keys, options)
    else
      # non similar keys, create horizontal table
      arr.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 a single column table
    arr.each do |element|
      html += "#{element}<br/>\n"        
    end
  end
  return html
end

.similar_keys?(arr) ⇒ Boolean

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

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/json2table.rb', line 84

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



151
152
153
154
155
# File 'lib/json2table.rb', line 151

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”



164
165
166
167
168
169
170
# File 'lib/json2table.rb', line 164

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