Class: SiteDiff::Result
- Inherits:
-
Struct
- Object
- Struct
- SiteDiff::Result
- Defined in:
- lib/sitediff/result.rb
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[success failure 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
-
#dump(dir) ⇒ Object
Dump the result to a file.
-
#filename ⇒ Object
Filename to store diff.
-
#initialize(*args) ⇒ Result
constructor
A new instance of Result.
-
#link ⇒ Object
Text of the link in the HTML report.
-
#log(verbose = true) ⇒ Object
Log the result to the terminal.
-
#status_text ⇒ Object
Textual representation of the status.
- #success? ⇒ Boolean
-
#url(tag, prefix, cache) ⇒ Object
Printable URL.
Constructor Details
#initialize(*args) ⇒ Result
Returns a new instance of Result.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sitediff/result.rb', line 17 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
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def after @after end |
#after_encoding ⇒ Object
Returns the value of attribute after_encoding
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def after_encoding @after_encoding end |
#before ⇒ Object
Returns the value of attribute before
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def before @before end |
#before_encoding ⇒ Object
Returns the value of attribute before_encoding
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def before_encoding @before_encoding end |
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
15 16 17 |
# File 'lib/sitediff/result.rb', line 15 def diff @diff end |
#error ⇒ Object
Returns the value of attribute error
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def error @error end |
#path ⇒ Object
Returns the value of attribute path
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def path @path end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
15 16 17 |
# File 'lib/sitediff/result.rb', line 15 def status @status end |
#verbose ⇒ Object
Returns the value of attribute verbose
9 10 11 |
# File 'lib/sitediff/result.rb', line 9 def verbose @verbose end |
Instance Method Details
#dump(dir) ⇒ Object
Dump the result to a file
74 75 76 77 78 79 80 81 |
# File 'lib/sitediff/result.rb', line 74 def dump(dir) 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)) end end |
#filename ⇒ Object
Filename to store diff
47 48 49 |
# File 'lib/sitediff/result.rb', line 47 def filename File.join(SiteDiff::DIFFS_DIR, Digest::SHA1.hexdigest(path) + '.html') end |
#link ⇒ Object
Text of the link in the HTML report
52 53 54 55 56 57 58 |
# File 'lib/sitediff/result.rb', line 52 def link case status when STATUS_ERROR then error when STATUS_SUCCESS then status_text when STATUS_FAILURE then "<a href='#{filename}'>DIFF</a>" end end |
#log(verbose = true) ⇒ Object
Log the result to the terminal
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sitediff/result.rb', line 61 def log(verbose = true) case status when STATUS_SUCCESS then SiteDiff.log path, :diff_success, 'UNCHANGED' when STATUS_ERROR then SiteDiff.log path, :warn, "ERROR (#{error})" when STATUS_FAILURE then SiteDiff.log path, :diff_failure, 'CHANGED' puts Diff.terminal_diffy(before, after) if verbose end end |
#status_text ⇒ Object
Textual representation of the status
36 37 38 |
# File 'lib/sitediff/result.rb', line 36 def status_text STATUS_TEXT[status] end |
#success? ⇒ Boolean
31 32 33 |
# File 'lib/sitediff/result.rb', line 31 def success? status == STATUS_SUCCESS end |
#url(tag, prefix, cache) ⇒ Object
Printable URL
41 42 43 44 |
# File 'lib/sitediff/result.rb', line 41 def url(tag, prefix, cache) base = cache..include?(tag) ? "/cache/#{tag}" : prefix base.to_s + path end |