Class: BadLinkFinder::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/bad_link_finder/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_url, link) ⇒ Link

Returns a new instance of Link.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bad_link_finder/link.rb', line 7

def initialize(page_url, link)
  @page_url = page_url
  @link = link
  @url = get_url_from_link(link)

  verify_url(@url)
  validate_with_request

rescue URI::InvalidURIError => exception
  record_error("This link is in a bad format", exception)
rescue Mechanize::ResponseCodeError => exception
  if [405, 500].include?(exception.response_code.to_i)  && !@head_unsupported
    @head_unsupported = true
    retry
  else
    record_error("This request returned a #{exception.response_code}", exception)
  end
rescue Mechanize::UnauthorizedError => exception
  record_error("This link requires authorisation", exception)
rescue Mechanize::UnsupportedSchemeError => exception
  record_error("This link has a scheme we can't load (should be http or https)", exception)
rescue Mechanize::RedirectLimitReachedError => exception
  record_error("This link might be in a redirect loop", exception)
rescue Mechanize::RobotsDisallowedError => exception
  record_error("This link is blocked by robots.txt or nofollow attributes", exception)
rescue Mechanize::Error, Net::HTTP::Persistent::Error, Timeout::Error, Errno::EINVAL,
       Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
       Net::ProtocolError, OpenSSL::SSL::SSLError, SocketError => exception # Thanks Net::HTTP
  record_error("The server failed to serve this page properly", exception)
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



5
6
7
# File 'lib/bad_link_finder/link.rb', line 5

def error_message
  @error_message
end

#exceptionObject (readonly)

Returns the value of attribute exception.



5
6
7
# File 'lib/bad_link_finder/link.rb', line 5

def exception
  @exception
end

Returns the value of attribute link.



5
6
7
# File 'lib/bad_link_finder/link.rb', line 5

def link
  @link
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/bad_link_finder/link.rb', line 5

def url
  @url
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bad_link_finder/link.rb', line 38

def valid?
  @error_message.nil?
end