Class: SiteDiff::Result

Inherits:
Struct
  • Object
show all
Defined in:
lib/sitediff/result.rb

Overview

SiteDiff Result Object.

Constant Summary collapse

STATUS_SUCCESS =

Identical before and after

0
STATUS_FAILURE =

Different before and after

1
STATUS_ERROR =

Couldn’t fetch page

2
STATUS_TEXT =
%w[unchanged changed error].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Result

Creates a Result.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sitediff/result.rb', line 29

def initialize(*args)
  super
  if error
    @status = STATUS_ERROR
  else
    if !before_encoding || !after_encoding
      @diff = Diff.binary_diffy(
        before,
        after,
        before_encoding,
        after_encoding
      )
    else
      @diff = Diff.html_diffy(before, after)
    end
    @status = @diff ? STATUS_FAILURE : STATUS_SUCCESS
  end
end

Instance Attribute Details

#afterObject

Returns the value of attribute after

Returns:

  • (Object)

    the current value of after



11
12
13
# File 'lib/sitediff/result.rb', line 11

def after
  @after
end

#after_encodingObject

Returns the value of attribute after_encoding

Returns:

  • (Object)

    the current value of after_encoding



11
12
13
# File 'lib/sitediff/result.rb', line 11

def after_encoding
  @after_encoding
end

#beforeObject

Returns the value of attribute before

Returns:

  • (Object)

    the current value of before



11
12
13
# File 'lib/sitediff/result.rb', line 11

def before
  @before
end

#before_encodingObject

Returns the value of attribute before_encoding

Returns:

  • (Object)

    the current value of before_encoding



11
12
13
# File 'lib/sitediff/result.rb', line 11

def before_encoding
  @before_encoding
end

#diffObject (readonly)

Returns the value of attribute diff.



25
26
27
# File 'lib/sitediff/result.rb', line 25

def diff
  @diff
end

#errorObject

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



11
12
13
# File 'lib/sitediff/result.rb', line 11

def error
  @error
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



11
12
13
# File 'lib/sitediff/result.rb', line 11

def path
  @path
end

#statusObject (readonly)

Returns the value of attribute status.



25
26
27
# File 'lib/sitediff/result.rb', line 25

def status
  @status
end

#verboseObject

Returns the value of attribute verbose

Returns:

  • (Object)

    the current value of verbose



11
12
13
# File 'lib/sitediff/result.rb', line 11

def verbose
  @verbose
end

Instance Method Details

#diff_url(relative: false) ⇒ Object

Returns a URL to the result diff.

Returns nil if the result has no diffs.



85
86
87
88
# File 'lib/sitediff/result.rb', line 85

def diff_url(relative: false)
  prefix = relative ? 'files/' : '/files/'
  return prefix + filename if status == STATUS_FAILURE
end

#dump(dir, relative: false) ⇒ Object

Dump the result to a file



104
105
106
107
108
109
110
111
# File 'lib/sitediff/result.rb', line 104

def dump(dir, relative: false)
  dump_path = File.join(dir, filename)
  base = File.dirname(dump_path)
  FileUtils.mkdir_p(base) unless File.exist?(base)
  File.open(dump_path, 'w') do |f|
    f.write(Diff.generate_diff_output(self, relative:))
  end
end

#error?Boolean

Whether the result has an error.

Returns:

  • (Boolean)


60
61
62
# File 'lib/sitediff/result.rb', line 60

def error?
  status == STATUS_ERROR
end

#filenameObject

Filename to store diff



78
79
80
# File 'lib/sitediff/result.rb', line 78

def filename
  File.join(Report::DIFFS_DIR, "#{Digest::SHA1.hexdigest(path)}.html")
end

#log(verbose: true) ⇒ Object

Log the result to the terminal



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sitediff/result.rb', line 91

def log(verbose: true)
  case status
  when STATUS_SUCCESS
    SiteDiff.log path, :success, 'UNCHANGED'
  when STATUS_ERROR
    SiteDiff.log path + " (#{error})", :warning, 'ERROR'
  when STATUS_FAILURE
    SiteDiff.log path, :error, 'CHANGED'
    puts Diff.terminal_diffy(before, after) if verbose
  end
end

#status_textObject

Textual representation of the status



65
66
67
# File 'lib/sitediff/result.rb', line 65

def status_text
  STATUS_TEXT[status]
end

#success?Boolean

Whether the result has no diff.

If there is a diff, it is not a success.

TODO: Change “Success” to unchanged.

Returns:

  • (Boolean)


54
55
56
# File 'lib/sitediff/result.rb', line 54

def success?
  status == STATUS_SUCCESS
end

#url(tag, prefix, cache) ⇒ Object

Printable URL



70
71
72
73
74
75
# File 'lib/sitediff/result.rb', line 70

def url(tag, prefix, cache)
  return unless prefix

  base = cache.read_tags.include?(tag) ? "/cache/#{tag}" : prefix
  base.to_s + path
end