json2table 1.0.4 → 1.0.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json2table.rb +41 -28
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8b4e2cdf26775415b6c8260e3d72e1f44561cbd
4
- data.tar.gz: 785b33c9089dbd3de23e980a135718d8acfa3e26
3
+ metadata.gz: f029ec372c3f1c7ce827f3fd6bbc1be84b878309
4
+ data.tar.gz: 7ba26efd088936d8fc55b95afb7d59b1dbb1fb45
5
5
  SHA512:
6
- metadata.gz: 331a9b1de10556c29cd762a3de29f29b9ab25dfa6820278fc59eff4b29b54be3b1d090e6ccc45ad06db785ba4ad86f145d9bc700599da1b53da20565c530e8e6
7
- data.tar.gz: c5d20144b6f5562b25496517769e2a89765ee38100187274e10edb9bd381c5810238c8b887f468112dfec34a24817665c43bf61309ddd808d8cd97b079d10fa0
6
+ metadata.gz: 03e568b01626b1f6f9f18173ab2c6bf668992521bf158a63e3f9415d37a72e15c15c044ba5cdaa6fe8e27f996a102dc715266aa3e9cf60d5b49249098c5878b7
7
+ data.tar.gz: a456b9485d58dff6f0d933b3872612b2c9806b81a5e247ef800fffa380ac7efaa9cdfd4177b8bb62c6bb076da771f84bc98ebcc0333d038ca5cd310a361827e7
@@ -20,7 +20,7 @@ module Json2table
20
20
  end
21
21
  rescue Exception => e
22
22
  puts "JSON2TABLE:: Input not a valid JSON, provide valid JSON object"
23
- puts e.message
23
+ #puts e.message
24
24
  throw e
25
25
  end
26
26
  html = self.create_table(hash, options)
@@ -29,41 +29,54 @@ module Json2table
29
29
 
30
30
  def self.create_table(hash, options)
31
31
  html = start_table_tag(options)
32
- hash.each do |key, value|
33
- # key goes in a column and value in second column of the same row
34
- html += "<tr><th>#{to_human(key)}</th>\n"
35
- html += "<td>"
36
-
37
- if value.is_a?(Hash)
38
- # create a row with key as heading and body
39
- # as another table
40
- html += create_table(value, options)
41
- elsif value.is_a?(Array)
42
- if value[0].is_a?(Hash) # Array of hashes
43
- keys = similar_keys?(value)
44
- if keys
45
- # if elements of this array are hashes with same keys,
46
- # display it as a top-down table
47
- html += create_vertical_table_from_array(value, keys, options)
48
- else
49
- # non similar keys, create horizontal table
50
- value.each do |h|
51
- html += create_table(h, options)
52
- end
53
- end
54
- else
55
- # array of a primitive data types eg. [1,2,3]
56
- # all values can be displayed in in cell
32
+ if hash.is_a?(Array)
33
+ html += "<tr><td>\n"
34
+ #puts ">>>> #{process_array(hash, options)}"
35
+ html += process_array(hash, options)
36
+ elsif hash.is_a?(Hash)
37
+ hash.each do |key, value|
38
+ # key goes in a column and value in second column of the same row
39
+ html += "<tr><th>#{to_human(key)}</th>\n"
40
+ html += "<td>"
41
+ if value.is_a?(Hash)
42
+ # create a row with key as heading and body
43
+ # as another table
44
+ html += create_table(value, options)
45
+ elsif value.is_a?(Array)
46
+ html += process_array(value, options)
47
+ else # simple primitive data type of value (non hash, non array)
57
48
  html += "#{value}</td></tr>\n"
58
49
  end
59
- else # simple primitive data type of value (non hash, non array)
60
- html += "#{value}</td></tr>\n"
61
50
  end
51
+ else # simple primitive data type of value (non hash, non array)
52
+ html += "<tr><td>#{hash}</td></tr>\n"
62
53
  end
63
54
  html += close_table_tag
64
55
  return html
65
56
  end
66
57
 
58
+ def self.process_array(arr, options)
59
+ html = ""
60
+ if arr[0].is_a?(Hash) # Array of hashes
61
+ keys = similar_keys?(arr)
62
+ if keys
63
+ # if elements of this array are hashes with same keys,
64
+ # display it as a top-down table
65
+ html += create_vertical_table_from_array(arr, keys, options)
66
+ else
67
+ # non similar keys, create horizontal table
68
+ arr.each do |h|
69
+ html += create_table(h, options)
70
+ end
71
+ end
72
+ else
73
+ # array of a primitive data types eg. [1,2,3]
74
+ # all values can be displayed in in cell
75
+ html += "#{arr}</td></tr>\n"
76
+ end
77
+ return html
78
+ end
79
+
67
80
  # This method checks if all the individual array items
68
81
  # are hashes with similar keys.
69
82
  def self.similar_keys?(arr)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json2table
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code Express
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-13 00:00:00.000000000 Z
11
+ date: 2017-06-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  This gem provides functionality to convert a JSON object into HTML