compat_tables.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. module CompatTables
  2. require 'json'
  3. require 'net/http'
  4. BROWSERS = {
  5. 'chrome' => 'Chrome',
  6. 'edge' => 'Edge',
  7. 'firefox' => 'Firefox',
  8. 'ie' => 'Internet Explorer',
  9. 'opera' => 'Opera',
  10. 'safari' => 'Safari',
  11. 'webview_android' => 'WebView Android',
  12. 'chrome_android' => 'Chrome Android',
  13. 'firefox_android' => 'Firefox for Android',
  14. 'opera_android' => 'Opera Android',
  15. 'safari_ios' => 'Safari on IOS',
  16. 'samsunginternet_android' => 'Samsung Internet'
  17. }
  18. def generate_compatibility_table()
  19. json_files_uri = request_bcd_uris()
  20. compat_tables = []
  21. json_files_uri.each do |uri|
  22. compat_tables.push(generate_compatibility_table_wrapper(uri))
  23. end
  24. return compat_tables
  25. end
  26. def request_bcd_uris
  27. index_json = JSON.load(Net::HTTP.get(URI(current_url.to_s + '/index.json')))
  28. uris = []
  29. index_json['doc']['body'].each do |element|
  30. uris.push(element['value']['dataURL']) if element['type'] == 'browser_compatibility' and element['value']['dataURL']
  31. end
  32. uris.map! do |uri|
  33. tmp_uri = URI.parse(base_url.to_s)
  34. tmp_uri.path = uri
  35. uri = tmp_uri.to_s
  36. end
  37. return uris
  38. end
  39. def generate_compatibility_table_wrapper(uri)
  40. @json_data = JSON.load(Net::HTTP.get(URI(uri)))['data']
  41. html_table = generate_basic_html_table()
  42. @json_data.keys.each do |key|
  43. if key == '__compat' or @json_data[key]['__compat']
  44. add_entry_to_table(html_table, key)
  45. else
  46. end
  47. end
  48. return html_table
  49. end
  50. def generate_basic_html_table
  51. table = Nokogiri::XML::Node.new('table', doc)
  52. table.add_child('<thead><tr id=bct-browser-type><tr id=bct-browsers><tbody>')
  53. table.css('#bct-browser-type').each do |node|
  54. node.add_child('<th>')
  55. %w(Desktop Mobile).each do |browser_type|
  56. node.add_child("<th colspan=6>#{browser_type}")
  57. end
  58. end
  59. table.css('#bct-browsers').each do |node|
  60. node.add_child('<th>')
  61. BROWSERS.values.each do |browser|
  62. node.add_child("<th>#{browser}")
  63. end
  64. end
  65. return table
  66. end
  67. def add_entry_to_table(html_table, key)
  68. json = @json_data[key]
  69. html_table.at_css('tbody').add_child('<tr>')
  70. last_table_entry = html_table.at_css('tbody').last_element_child
  71. if key == '__compat'
  72. tmp = slug.split('/')
  73. last_table_entry.add_child("<th><code>#{tmp.last}")
  74. else
  75. last_table_entry.add_child("<th><code>#{key}")
  76. end
  77. BROWSERS.keys.each do |browser_key|
  78. if key == '__compat'
  79. add_data_to_entry(json['support'][browser_key], last_table_entry)
  80. else
  81. add_data_to_entry(json['__compat']['support'][browser_key], last_table_entry)
  82. end
  83. end
  84. end
  85. def add_data_to_entry(json, entry)
  86. version_added = []
  87. version_removed = []
  88. notes = []
  89. if json
  90. if json.is_a?(Array)
  91. json.each do |element|
  92. if element['version_added']
  93. version_added.push(element['version_added'])
  94. else
  95. version_added.push(false)
  96. end
  97. if element['version_removed']
  98. version_removed.push(element['version_removed'])
  99. else
  100. version_removed.push(false)
  101. end
  102. if element['notes']
  103. notes.push(element['notes'])
  104. else
  105. notes.push(false)
  106. end
  107. end
  108. else
  109. version_added.push(json['version_added'])
  110. version_removed.push(json['version_removed'])
  111. notes.push(json['notes'])
  112. end
  113. version_added.map! do |version|
  114. if version == true
  115. version = 'Yes'
  116. elsif version == false
  117. version = 'No'
  118. elsif version.is_a?(Numeric)
  119. else
  120. version = '?'
  121. end
  122. version
  123. end
  124. if version_removed[0]
  125. format_string = "<td class=bc-supports-no>"
  126. else
  127. if version_added[0] == 'No'
  128. format_string = "<td class=bc-supports-no>"
  129. else
  130. format_string = "<td class=bc-supports-yes>"
  131. end
  132. end
  133. for value in (0..version_added.length-1) do
  134. if version_removed[value]
  135. format_string += "<div>#{version_added[value]}-#{version_removed[value]}</div>"
  136. else
  137. if version_added[value] == 'No'
  138. format_string += "<div>#{version_added[value]}</div>"
  139. else
  140. format_string += "<div>#{version_added[value]}</div>"
  141. end
  142. end
  143. if notes[value]
  144. format_string += "<div>#{notes[value]}</div>"
  145. end
  146. end
  147. else
  148. format_string = "<td class=bc-supports-no><div>?</div></td>"
  149. end
  150. entry.add_child(format_string)
  151. end
  152. end