Class: LinkChecker

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

Class Method Summary collapse

Class Method Details

.check_uri(uri, redirected = false) ⇒ Object



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
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/link_checker_override.rb', line 7

def self.check_uri(uri, redirected=false)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == "https"
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl?

  http.start do
    path = (uri.path.empty?) ? '/' : uri.path
    http.request_get(path) do |response|
      #print "\n#{response.class}(#{response.code}) "
      case response

      when Net::HTTPSuccess
        if redirected
          return Redirect.new(:final_destination_uri_string => uri.to_s)
        else
          return Good.new(:uri_string => uri.to_s)
        end

      when Net::HTTPForbidden
        if redirected
          return Redirect.new(:final_destination_uri_string => uri.to_s)
        else
          return Good.new(:uri_string => uri.to_s)
        end

      when Net::HTTPUnauthorized 
        if redirected
          return Redirect.new(:final_destination_uri_string => uri.to_s)
        else
          return Good.new(:uri_string => uri.to_s)
        end


      when Net::HTTPRedirection then
        return self.check_uri(URI(response['location']), true)
      else
        return Error.new(:uri_string => uri.to_s, :error => response)
      end
    end
  end
end