Class: Magellan::ExpectedLinksTracker

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/magellan/expected_links_tracker.rb

Overview

The observer that will listen to all results and compare them to a list of rules about expected urls.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_patterns) ⇒ ExpectedLinksTracker

Create a new expected links tracker. An array of tuples of the url pattern and expected link is a required argument. Example: Magellan::ExpectedLinksTracker.new([])



12
13
14
15
16
# File 'lib/magellan/expected_links_tracker.rb', line 12

def initialize(expected_patterns)
  @errors = []
  @expected_patterns = expected_patterns
  @evaluated_expectations = {}
end

Instance Attribute Details

#errorsObject (readonly)

An array of failed expecations



6
7
8
# File 'lib/magellan/expected_links_tracker.rb', line 6

def errors
  @errors
end

Instance Method Details

#failed?Boolean

Are there expected urls that have not been found yet, or pages that have been found with missing links?

Returns:

  • (Boolean)


46
47
48
# File 'lib/magellan/expected_links_tracker.rb', line 46

def failed?
  unmet_expecations? || has_errors?
end

#failure_messageObject

A string summary of all failure messages



51
52
53
# File 'lib/magellan/expected_links_tracker.rb', line 51

def failure_message
  unmet_expecations_messages << errors.join("\n")
end

#has_errors?Boolean

:nodoc:

Returns:

  • (Boolean)


37
38
39
# File 'lib/magellan/expected_links_tracker.rb', line 37

def has_errors? # :nodoc:
  !@errors.empty?
end

#patterns_that_apply(result) ⇒ Object

:nodoc:



31
32
33
34
35
# File 'lib/magellan/expected_links_tracker.rb', line 31

def patterns_that_apply(result) # :nodoc:
  res = @expected_patterns.select{|pattern,expecation| result.url =~ pattern || result.destination_url =~ pattern}
  res.each { |expected_pattern| @evaluated_expectations[expected_pattern] = nil }
  res
end

#unmet_expecationsObject

Expecations that have never been evaluated



62
63
64
# File 'lib/magellan/expected_links_tracker.rb', line 62

def unmet_expecations
  @expected_patterns - @evaluated_expectations.keys
end

#unmet_expecations?Boolean

:nodoc:

Returns:

  • (Boolean)


41
42
43
# File 'lib/magellan/expected_links_tracker.rb', line 41

def unmet_expecations? # :nodoc:
  !unmet_expecations.empty?
end

#unmet_expecations_messagesObject

:nodoc:



55
56
57
58
59
# File 'lib/magellan/expected_links_tracker.rb', line 55

def unmet_expecations_messages # :nodoc:
  message = ""
  unmet_expecations.each {|pattern,unmet_expecation| message << "#{pattern} was never evaluted during the crawl\n"}
  message
end

#update(time, result) ⇒ Object

The updates that come in via a observable subject, the time the result came at and the Magellan::Result itself.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/magellan/expected_links_tracker.rb', line 19

def update(time,result)
  if result.html_content?
    patterns_that_apply(result).each do |pattern,expectation|
      passed = result.linked_resources.include?(expectation)
      changed
      message = "#{result.url} did not contain a link to #{expectation}"
      notify_observers(Time.now, passed, message)
      @errors << message unless passed
    end
  end
end