class SimplecovUrl::HtmlProcessor
Constants
- CSS_FILE
- HTML_FILE
- JAVASCRIPT_FILE
Public Class Methods
call()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 11 def self.call new.html end
Public Instance Methods
html()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 15 def html return if AccessManager.deny? tmp = read_file(HTML_FILE) return no_report_message unless tmp tmp = remove_link_and_script_tags(tmp) tmp = img_href_tags_to_base64(tmp) return frontend_error(CSS_FILE) unless css return frontend_error(JAVASCRIPT_FILE) unless javascript tmp = insert_css_and_javascript_as_inline(tmp) tmp.html_safe rescue StandardError => e general_error(e) end
Private Instance Methods
background_url_attributes_to_base64(content)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 71 def background_url_attributes_to_base64(content) imgs_to_base64( content, attribute_regex: /url\(.*?\)/, paths_regex: /(url\W*)|(\W*$)/, replacement: lambda do |encoded_img, mime_type| "url(data:#{mime_type};base64,#{encoded_img})" end ) end
css()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 59 def css @css ||= begin tmp = read_file(CSS_FILE) return nil unless tmp tmp = background_url_attributes_to_base64(tmp) "<style>#{tmp}</style>" end end
files()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 39 def files @files ||= Dir.glob(File.join(Rails.root, 'coverage', '**', '*')) end
frontend_error(filename)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 132 def frontend_error(filename) "Error: '#{filename}' file is missing." end
general_error(error)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 136 def general_error(error) puts(error.message) puts(error.backtrace) 'Error: Unable to load test coverage report.' end
imgs_to_base64(content, attribute_regex:, paths_regex:, replacement:)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 93 def imgs_to_base64(content, attribute_regex:, paths_regex:, replacement:) attributes = content.scan(attribute_regex).flatten img_paths = attributes.map do |attribute| [attribute, attribute.gsub(paths_regex, '')] end base64s = img_paths.map do |attribute, img_path| file = files.find { |f| f.include?(img_path) } mime_type = "image/#{File.extname(file).delete('.')}" encoded_img = Base64.strict_encode64(File.read(file)) [attribute, replacement.call(encoded_img, mime_type)] end.to_h base64s.each do |attribute, base64| content = content.gsub(attribute, base64) end content end
insert_css_and_javascript_as_inline(tmp)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 117 def insert_css_and_javascript_as_inline(tmp) position = (tmp =~ /<\/head>/) tmp.insert(position, css) tmp.insert(position, javascript) end
javascript()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 49 def javascript @javascript ||= begin tmp = read_file(JAVASCRIPT_FILE) return nil unless tmp "<script>#{tmp}</script>" end end
no_report_message()
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 128 def no_report_message 'No test coverage report available.' end
read_file(name)
click to toggle source
# File lib/simplecov_url/html_processor.rb, line 43 def read_file(name) file = files.find { |f| f.include?(name) } file ? File.read(file) : nil end