Class: WebChecker::Http

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

Instance Method Summary collapse

Constructor Details

#initialize(uri_str, limit = 10) ⇒ Http

Returns a new instance of Http.

Raises:



10
11
12
13
14
15
16
# File 'lib/web_checker/http.rb', line 10

def initialize(uri_str, limit = 10)
  @uri_str, @limit = uri_str, limit
  @uri_str = "http://#{@uri_str}" unless @uri_str.include?('://')
  @uri = URI(@uri_str)
  raise NotHttpURIError, "Not HTTP URI: #{@uri_str}" unless @uri.scheme.include?('http')
  raise InvalidHostError, "Domain or host does not exists: #{@uri.host}" unless domain_exists?(@uri.host)
end

Instance Method Details

#accessible?Boolean

Returns:

  • (Boolean)


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
# File 'lib/web_checker/http.rb', line 18

def accessible?
  return false if @limit == 0
  begin
    Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
      request = Net::HTTP::Get.new(@uri.request_uri)
      response = http.request(request)
      case response
        when Net::HTTPSuccess      then true
        when Net::HTTPClientError  then false
        when Net::HTTPServerError  then false
        when Net::HTTPRedirection
          location = response['location']
          unless location.include?('://')
            host_with_protocol = @uri_str[/^[^\:]+\:\/\/[^\/]+/, 0]
            location = host_with_protocol + location
          end
          self.class.new(location, @limit - 1).accessible?
        else
          false
      end
    end
  rescue SocketError
    false
  end
end