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.3"

Class Method Summary collapse

Class Method Details

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



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

def diff(html1, html2, options = {})
  options = {
    :replace => [],
    :ignore => []
  }.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.map(&:to_html)))
end

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



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

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



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

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