Class: SiteDiff::Report
- Inherits:
-
Object
- Object
- SiteDiff::Report
- Defined in:
- lib/sitediff/report.rb
Overview
SiteDiff Report Helper.
Constant Summary collapse
- DIFFS_DIR =
Directory where diffs will be generated.
'diffs'
- FAILURES_FILE =
Name of file containing a list of pages with diffs.
'failures.txt'
- REPORT_FILE_HTML =
Name of file containing HTML report of diffs.
'report.html'
- REPORT_FILE_JSON =
Name of file containing JSON report of diffs.
'report.json'
- REPORT_FILE_TAR =
Name of file containing exported file archive.
'report.tgz'
- REPORT_BUILD_DIR =
Name of directory in which to build the portable report.
'_tmp_report'
- REPORT_DIR =
Name of the portable report directory.
'report'
- SETTINGS_FILE =
Path to settings used for report.
'settings.yaml'
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Class Method Summary collapse
-
.css ⇒ Object
Returns CSS for HTML report.
-
.js ⇒ Object
Returns JS for HTML report.
Instance Method Summary collapse
-
#generate_html(dir, report_before = nil, report_after = nil) ⇒ Object
Generates an HTML report.
-
#generate_json(dir) ⇒ Object
Generates a JSON report.
-
#initialize(config, cache, results) ⇒ Report
constructor
Creates a Reporter object.
-
#package_report(dir) ⇒ Object
Package report for export.
-
#write_diffs(dir) ⇒ Object
Creates diff files in a directory named “diffs”.
-
#write_failures(dir) ⇒ Object
Writes paths with diffs into a file.
-
#write_settings(dir, report_before = nil, report_after = nil) ⇒ Object
Creates report settings.yaml file.
Constructor Details
#initialize(config, cache, results) ⇒ Report
Creates a Reporter object.
54 55 56 57 58 |
# File 'lib/sitediff/report.rb', line 54 def initialize(config, cache, results) @config = config @cache = cache @results = results end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
14 15 16 |
# File 'lib/sitediff/report.rb', line 14 def cache @cache end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
14 15 16 |
# File 'lib/sitediff/report.rb', line 14 def results @results end |
Class Method Details
Instance Method Details
#generate_html(dir, report_before = nil, report_after = nil) ⇒ Object
Generates an HTML report.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sitediff/report.rb', line 65 def generate_html( dir, report_before = nil, report_after = nil ) report_before ||= @config.before_url report_after ||= @config.after_url @config.before_time = (:before) @config.after_time = (:after) dir = SiteDiff.ensure_dir dir write_diffs dir write_failures dir # Prepare report. report = Diff.generate_html( @results, report_before, report_after, @cache, @config ) # Write report. report_file = dir + REPORT_FILE_HTML report_file.unlink if report_file.file? report_file.open('w') { |f| f.write(report) } write_settings dir, report_before, report_after if @config.export package_report(dir) else SiteDiff.log "Report generated to #{report_file.}" end end |
#generate_json(dir) ⇒ Object
Generates a JSON report.
108 109 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 |
# File 'lib/sitediff/report.rb', line 108 def generate_json(dir) dir = SiteDiff.ensure_dir dir write_diffs dir write_failures dir # Prepare report. report = { paths_compared: @results.length, paths_diffs: 0, paths: {} } @results.each do |item| report[:paths_diffs] += 1 unless item.success? item_report = { path: item.path, status: item.status, message: item.error } report[:paths][item.path] = item_report end report = JSON report # Write report. report_file = dir + REPORT_FILE_JSON report_file.unlink if report_file.file? report_file.open('w') { |f| f.write(report) } write_settings dir SiteDiff.log "Report generated to #{report_file.}" end |
#package_report(dir) ⇒ Object
Package report for export.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/sitediff/report.rb', line 143 def package_report(dir) # Create temporaryreport directories. temp_path = dir + REPORT_BUILD_DIR temp_path.rmtree if temp_path.directory? temp_path.mkpath report_path = temp_path + REPORT_DIR report_path.mkpath files_path = "#{report_path}/files" FileUtils.mkpath(files_path) diffs_path = dir + DIFFS_DIR # Move files to place. FileUtils.move(dir + REPORT_FILE_HTML, report_path) FileUtils.move(diffs_path, files_path) if diffs_path.directory? # Make tar file. Dir.chdir(temp_path) do Minitar.pack( REPORT_DIR, Zlib::GzipWriter.new(File.open(REPORT_FILE_TAR, 'wb')) ) end FileUtils.move(temp_path + REPORT_FILE_TAR, dir) temp_path.rmtree SiteDiff.log "Archived report generated to #{dir.join(REPORT_FILE_TAR)}" end |
#write_diffs(dir) ⇒ Object
Creates diff files in a directory named “diffs”.
If “dir” is /foo/bar, then diffs will be placed in /foo/bar/diffs.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sitediff/report.rb', line 177 def write_diffs(dir) raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname # Delete existing "diffs" dir, if exists. diff_dir = dir + DIFFS_DIR diff_dir.rmtree if diff_dir.exist? # Write diffs to the diff directory. @results.each { |r| r.dump(dir, relative: @config.export) if r.status == Result::STATUS_FAILURE } SiteDiff.log "All diff files written to #{diff_dir.}" unless @config.export end |
#write_failures(dir) ⇒ Object
Writes paths with diffs into a file.
194 195 196 197 198 199 200 201 202 |
# File 'lib/sitediff/report.rb', line 194 def write_failures(dir) raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname failures = dir + FAILURES_FILE SiteDiff.log "All failures written to #{failures.}" failures.open('w') do |f| @results.each { |r| f.puts r.path unless r.success? } end end |
#write_settings(dir, report_before = nil, report_after = nil) ⇒ Object
Creates report settings.yaml file.
TODO: Find a way to avoid having to create this file.
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/sitediff/report.rb', line 211 def write_settings(dir, report_before = nil, report_after = nil) raise Exception 'dir must be a Pathname' unless dir.is_a? Pathname settings = { 'before' => report_before, 'after' => report_after, 'cached' => %w[before after] } dir.+(SETTINGS_FILE).open('w') { |f| YAML.dump(settings, f) } end |