Class: SiteDiff::Result

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

Instance Method Summary collapse

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

#afterObject

Returns the value of attribute after

Returns:

  • (Object)

    the current value of after



9
10
11
# File 'lib/sitediff/result.rb', line 9

def after
  @after
end

#after_encodingObject

Returns the value of attribute after_encoding

Returns:

  • (Object)

    the current value of after_encoding



9
10
11
# File 'lib/sitediff/result.rb', line 9

def after_encoding
  @after_encoding
end

#beforeObject

Returns the value of attribute before

Returns:

  • (Object)

    the current value of before



9
10
11
# File 'lib/sitediff/result.rb', line 9

def before
  @before
end

#before_encodingObject

Returns the value of attribute before_encoding

Returns:

  • (Object)

    the current value of before_encoding



9
10
11
# File 'lib/sitediff/result.rb', line 9

def before_encoding
  @before_encoding
end

#diffObject (readonly)

Returns the value of attribute diff.



15
16
17
# File 'lib/sitediff/result.rb', line 15

def diff
  @diff
end

#errorObject

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



9
10
11
# File 'lib/sitediff/result.rb', line 9

def error
  @error
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



9
10
11
# File 'lib/sitediff/result.rb', line 9

def path
  @path
end

#statusObject (readonly)

Returns the value of attribute status.



15
16
17
# File 'lib/sitediff/result.rb', line 15

def status
  @status
end

#verboseObject

Returns the value of attribute verbose

Returns:

  • (Object)

    the current value of 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

#filenameObject

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

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_textObject

Textual representation of the status



36
37
38
# File 'lib/sitediff/result.rb', line 36

def status_text
  STATUS_TEXT[status]
end

#success?Boolean

Returns:

  • (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.read_tags.include?(tag) ? "/cache/#{tag}" : prefix
  base.to_s + path
end