Class: Magellan::ExpectedLinksTracker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_patterns) ⇒ ExpectedLinksTracker

Returns a new instance of ExpectedLinksTracker.



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/magellan/expected_links_tracker.rb', line 4

def errors
  @errors
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  unmet_expecations? || has_errors?
end

#failure_messageObject



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

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

#has_errors?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/magellan/expected_links_tracker.rb', line 30

def has_errors?
  !@errors.empty?
end

#patterns_that_apply(result) ⇒ Object



24
25
26
27
28
# File 'lib/magellan/expected_links_tracker.rb', line 24

def patterns_that_apply(result)
  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



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

def unmet_expecations
  @expected_patterns - @evaluated_expectations.keys
end

#unmet_expecations?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/magellan/expected_links_tracker.rb', line 34

def unmet_expecations?
  !unmet_expecations.empty?
end

#unmet_expecations_messagesObject



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

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

#update(time, result) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/magellan/expected_links_tracker.rb', line 12

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