Module: UriResolver

Includes:
HTTParty
Defined in:
lib/uri_resolver.rb,
lib/uri_resolver/version.rb

Defined Under Namespace

Modules: Status

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.resolve_status(uri) ⇒ Object



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
48
49
50
51
52
53
54
# File 'lib/uri_resolver.rb', line 16

def self.resolve_status(uri)
  # TODO: I used to use code like this, when the below was inside a class:
  #@@responses[uri] ||= try_get_uri(uri) # Might raise error, if DNS error/timeout
  # Is it safe/sensible to memoize the reponses here, for performance?

  response = try_get_uri(uri) # Might raise error, if DNS error/timeout
  case
    # TODO: .dev domains behave differently on Ubuntu vs Windows!!
  when response.code != 200 || response.headers['server'] == "Apache/2.4.7 (Ubuntu)"
    Status::DoesNotResolve
  else
    # TODO: Is it posible to analyse anything further?
    # E.g. does the web page redirect? Does it contain certain strings? Is it at least a minimum size? ....
    Status::Resolves
  end

rescue SocketError # from TCPSocket#new
  Status::DoesNotResolve
rescue Net::OpenTimeout # from HTTParty#get
  Status::DoesNotResolve
rescue Net::ReadTimeout # from HTTParty#get
  Status::DoesNotResolve
rescue Errno::ECONNREFUSED # From HTTParty#get
  Status::DoesNotResolve
rescue Errno::ECONNRESET # From HTTParty#get
  Status::DoesNotResolve
rescue ConnectionFailed # From TCPSocket#new or HTTParty#get -- something funny happened, check manually!
  Status::MaybeResolves
rescue OpenSSL::SSL::SSLError # Resolves, but SSL certificate is not set up correctly
  Status::Resolves
rescue URI::InvalidURIError # Resolves, but to weird URL (contains escape sequences??)
  Status::Resolves
rescue HTTParty::RedirectionTooDeep # Resolves, but with lots of redirection!
  Status::Resolves
rescue StandardError => e # Something else happened??!!
  warn "URI #{uri} did not resolve, for unknown reason:"
  warn e.message
  Status::MaybeResolves
end