Class: SiteDiff::Report

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, cache, results) ⇒ Report

Creates a Reporter object.

Parameters:



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

#cacheObject (readonly)

Returns the value of attribute cache.



14
15
16
# File 'lib/sitediff/report.rb', line 14

def cache
  @cache
end

#resultsObject (readonly)

Returns the value of attribute results.



14
15
16
# File 'lib/sitediff/report.rb', line 14

def results
  @results
end

Class Method Details

.cssObject

Returns CSS for HTML report.



224
225
226
227
228
229
# File 'lib/sitediff/report.rb', line 224

def self.css
  output = ''
  output += File.read(File.join(SiteDiff::FILES_DIR, 'normalize.css'))
  output += File.read(File.join(SiteDiff::FILES_DIR, 'sitediff.css'))
  output
end

.jsObject

Returns JS for HTML report.



233
234
235
236
237
238
# File 'lib/sitediff/report.rb', line 233

def self.js
  output = ''
  output += File.read(File.join(SiteDiff::FILES_DIR, 'jquery.min.js'))
  output += File.read(File.join(SiteDiff::FILES_DIR, 'sitediff.js'))
  output
end

Instance Method Details

#generate_html(dir, report_before = nil, report_after = nil) ⇒ Object

Generates an HTML report.

Parameters:

  • dir (String)

    The directory in which the report is to be generated.



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 = get_timestamp(:before)
  @config.after_time = get_timestamp(: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.expand_path}"
  end
end

#generate_json(dir) ⇒ Object

Generates a JSON report.

Parameters:

  • dir

    The directory in which the report is to be generated.



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.expand_path}"
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.

Parameters:

  • dir (Pathname)

    The directory in which a “diffs” directory is to be generated.



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.expand_path}" unless @config.export
end

#write_failures(dir) ⇒ Object

Writes paths with diffs into a file.

Parameters:

  • dir (Pathname)

    The directory in which the report is to be generated.



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.expand_path}"
  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.

Parameters:

  • dir (Pathname)

    The directory in which the report is to be generated.



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