Class: ChromeDiff::SiteChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_diff/site_checker.rb

Instance Method Summary collapse

Constructor Details

#initialize(domains:, sizes:, paths:, options: {}) ⇒ SiteChecker

Returns a new instance of SiteChecker.



5
6
7
8
9
10
# File 'lib/chrome_diff/site_checker.rb', line 5

def initialize(domains:, sizes:, paths:, options: {})
  @domains = domains
  @sizes = sizes
  @paths = paths
  @options = options
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chrome_diff/site_checker.rb', line 12

def run
  options = @options.dup
  @sizes.each do |size|
    shots_dir = File.join("shots", size.map(&:to_s).join("x"))
    FileUtils.mkdir_p(shots_dir)

    options.merge()
    timeout = options.delete(:timeout)
    debug = options.delete(:debug) || false
    session = ChromeDiff::Session.new(width: size[0], height: size[1], timeout: timeout, debug: debug)

    html = ""
    images = []
    @paths.each do |name, path|
      from = "#{@domains[0]}#{path}"
      to = "#{@domains[1]}#{path}"
      filename = "#{name}.png"
      output = File.join(shots_dir, filename)
      result = session.compare(from_url: from, to_url: to, output: output)
      puts "#{output} (%.2f%%)" % result.diff_percent

      title = "#{filename} (%.2f%%)" % result.diff_percent
      images << [name, title]
      html += <<~HTML
        <h2 id="#{ name }">#{ title }</h2>
        <ul>
          <li><a href="#{from}">#{from}</a></li>
          <li><a href="#{to}">#{to}</a></li>
          <li>
            <a href="#{ filename }">
              <img src="#{ filename }">
            </a>
          </li>
        </ul>
      HTML
    end

    html = "<ul>\n" + images.map{|name, title| "<li><a href='##{ name }'>#{ title }</a></li>"}.join("\n") + "</ul>\n\n" + html

    File.write(File.join(shots_dir, "index.html"), html)
  end
end