Module: SiteDiff::Diff

Defined in:
lib/sitediff/diff.rb

Overview

SiteDiff Diff Object.

Class Method Summary collapse

Class Method Details

.binary_diffy(before, after, before_encoding, after_encoding) ⇒ Object

Computes diff of binary files using MD5 hashes.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sitediff/diff.rb', line 34

def binary_diffy(before, after, before_encoding, after_encoding)
  if before_encoding || after_encoding
    Diffy::Diff.new(encoding_blurb(before_encoding),
                    encoding_blurb(after_encoding)).to_s(:html)
  elsif before == after
    nil
  else
    md5_before = Digest::MD5.hexdigest(before)
    md5_after = Digest::MD5.hexdigest(after)
    Diffy::Diff.new("Binary content returned md5: #{md5_before}",
                    "Binary content returned md5: #{md5_after}").to_s(:html)
  end
end

.diff_config(config) ⇒ Object

Set configuration for Diffy.



78
79
80
81
82
83
84
# File 'lib/sitediff/diff.rb', line 78

def diff_config(config)
  diff_options = Diffy::Diff.default_options[:diff]
  diff_options = [diff_options] unless diff_options.is_a?(Array)
  # ignore_whitespace option
  diff_options.push('-w').uniq if config.ignore_whitespace
  Diffy::Diff.default_options[:diff] = diff_options
end

.encoding_blurb(encoding) ⇒ Object

Generates a description about encoding.



24
25
26
27
28
29
30
# File 'lib/sitediff/diff.rb', line 24

def encoding_blurb(encoding)
  if encoding
    "Text content returned - charset #{encoding}"
  else
    'Binary content returned'
  end
end

.generate_diff_output(result, relative: false) ⇒ Object

Generates diff output for a single result.



71
72
73
74
# File 'lib/sitediff/diff.rb', line 71

def generate_diff_output(result, relative: false)
  erb_path = File.join(SiteDiff::FILES_DIR, 'diff.html.erb')
  ERB.new(File.read(erb_path)).result(binding)
end

.generate_html(results, before, after, cache, config) ⇒ Object

Generates an HTML report. TODO: Generate the report in SiteDif::Report instead.



60
61
62
63
64
65
66
67
# File 'lib/sitediff/diff.rb', line 60

def generate_html(results, before, after, cache, config)
  relative = config.export
  report = config.report || {}
  before_url_report = report['before_url_report']
  after_url_report = report['after_url_report']
  erb_path = File.join(SiteDiff::FILES_DIR, 'report.html.erb')
  ERB.new(File.read(erb_path)).result(binding)
end

.html_diffy(before_html, after_html) ⇒ Object

Generates HTML diff.



16
17
18
19
20
# File 'lib/sitediff/diff.rb', line 16

def html_diffy(before_html, after_html)
  diff = Diffy::Diff.new(before_html, after_html)
  # If the diff is non-empty, convert it to string.
  diff.first ? diff.to_s(:html) : nil
end

.terminal_diffy(before_html, after_html) ⇒ Object

Generates diff for CLI output.



50
51
52
53
54
55
# File 'lib/sitediff/diff.rb', line 50

def terminal_diffy(before_html, after_html)
  args = []
  args << :color if Rainbow.enabled
  Diffy::Diff.new(before_html, after_html, context: 3)
             .to_s(*args)
end