Class: SiteDiff::Result
- Inherits:
-
Struct
- Object
- Struct
- SiteDiff::Result
- 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
-
#after ⇒ Object
Returns the value of attribute after.
-
#after_encoding ⇒ Object
Returns the value of attribute after_encoding.
-
#before ⇒ Object
Returns the value of attribute before.
-
#before_encoding ⇒ Object
Returns the value of attribute before_encoding.
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
-
#error ⇒ Object
Returns the value of attribute error.
-
#path ⇒ Object
Returns the value of attribute path.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#diff_url(relative: false) ⇒ Object
Returns a URL to the result diff.
-
#dump(dir, relative: false) ⇒ Object
Dump the result to a file.
-
#error? ⇒ Boolean
Whether the result has an error.
-
#filename ⇒ Object
Filename to store diff.
-
#initialize(*args) ⇒ Result
constructor
Creates a Result.
-
#log(verbose: true) ⇒ Object
Log the result to the terminal.
-
#status_text ⇒ Object
Textual representation of the status.
-
#success? ⇒ Boolean
Whether the result has no diff.
-
#url(tag, prefix, cache) ⇒ Object
Printable URL.
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
#after ⇒ Object
Returns the value of attribute after
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def after @after end |
#after_encoding ⇒ Object
Returns the value of attribute after_encoding
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def after_encoding @after_encoding end |
#before ⇒ Object
Returns the value of attribute before
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def before @before end |
#before_encoding ⇒ Object
Returns the value of attribute before_encoding
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def before_encoding @before_encoding end |
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
25 26 27 |
# File 'lib/sitediff/result.rb', line 25 def diff @diff end |
#error ⇒ Object
Returns the value of attribute error
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def error @error end |
#path ⇒ Object
Returns the value of attribute path
11 12 13 |
# File 'lib/sitediff/result.rb', line 11 def path @path end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
25 26 27 |
# File 'lib/sitediff/result.rb', line 25 def status @status end |
#verbose ⇒ Object
Returns the value of attribute 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.
60 61 62 |
# File 'lib/sitediff/result.rb', line 60 def error? status == STATUS_ERROR end |
#filename ⇒ Object
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_text ⇒ Object
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.
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..include?(tag) ? "/cache/#{tag}" : prefix base.to_s + path end |