Class: OpticalDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/optical_diff.rb,
lib/optical_diff/version.rb

Defined Under Namespace

Classes: DiffResult

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.diff(html1, html2, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/optical_diff.rb', line 7

def diff(html1, html2, options = {})
  options = {
    :replace => [],
    :ignore => [],
    :diff_option => Diffy::Diff.default_options[:diff]
  }.merge(options)

  replaced_htmls = [html1, html2].map {|html| replace(html, options[:replace]) }
  parsed_htmls = replaced_htmls.map {|html| Nokogiri::HTML.parse(html) }
  stripped_htmls = parsed_htmls.map {|html| strip_ignore_elements(html, options[:ignore]) }
  DiffResult.new(Diffy::Diff.new(stripped_htmls[0].to_html, stripped_htmls[1].to_html, :diff => options[:diff_option]))
end

.replace(page, patterns = []) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/optical_diff.rb', line 20

def replace(page, patterns = [])
  page = page.dup
  patterns.each do |pattern, replace|
    page.gsub!(pattern, replace)
  end
  page
end

.strip_ignore_elements(page, ignore = []) ⇒ Object



28
29
30
31
32
33
# File 'lib/optical_diff.rb', line 28

def strip_ignore_elements(page, ignore = [])
  ignore.each do |selector|
    page.search(selector).remove
  end
  page
end