Class: Magellan::BrokenLinkTracker

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

Overview

The class that will track all broken links, urls that return 4** or 5** http status codes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBrokenLinkTracker

Create a new broken link tracker



10
11
12
13
# File 'lib/magellan/broken_link_tracker.rb', line 10

def initialize
  @broken_links = []
  @first_linked_from = {}
end

Instance Attribute Details

All results containing 4** or 5** http status codes



7
8
9
# File 'lib/magellan/broken_link_tracker.rb', line 7

def broken_links
  @broken_links
end

Instance Method Details

Generate the failure message for a Magellan::Result



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

def broken_link_message(result)
  "#{result.url} first linked from: #{@first_linked_from[result.url]} returned: #{result.status_code}"
end

#failed?Boolean

Are there any broken links?

Returns:

  • (Boolean)


27
28
29
# File 'lib/magellan/broken_link_tracker.rb', line 27

def failed? 
  !@broken_links.empty?
end

#failure_messageObject

A text message of all failures



32
33
34
# File 'lib/magellan/broken_link_tracker.rb', line 32

def failure_message
  @broken_links.map{|broken_link| broken_link_message(broken_link)}.join("\n")
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.



16
17
18
19
20
21
22
23
24
# File 'lib/magellan/broken_link_tracker.rb', line 16

def update(time,result)
  failed = result.status_code.starts_with?("5") || result.status_code.starts_with?("4")
  @broken_links << result if failed
  changed
  notify_observers(Time.now, !failed, broken_link_message(result))
  result.absolute_linked_resources.each do |linked_resource|
    @first_linked_from[linked_resource] = result.url if !@first_linked_from.has_key?(linked_resource)
  end
end