class UrlChecker::SingleChecker

Checks a single URL and returns the result

Constants

INVALID_URI_MSG
SCHEMES

Attributes

url_string[R]

Public Class Methods

new(url:) click to toggle source
# File lib/url_checker/single_checker.rb, line 11
def initialize(url:)
  @url_string = url
end

Public Instance Methods

call() click to toggle source
# File lib/url_checker/single_checker.rb, line 15
def call
  check_url
end

Private Instance Methods

check_url() click to toggle source
# File lib/url_checker/single_checker.rb, line 21
def check_url
  return invalid_uri_response unless valid_url?
  response = Net::HTTP.get_response uri
  response.uri ||= uri # sometimes the uri is not set
  response
rescue Errno::ECONNREFUSED => e
  rescued_response('Connection refused', e.message)
rescue Errno::EINVAL => e
  rescued_response('Invalid argument', e.message)
rescue => e
  rescued_response(e.class.to_s, e.message)
end
invalid_uri_response() click to toggle source
# File lib/url_checker/single_checker.rb, line 34
def invalid_uri_response
  UrlChecker::RecuedResponse.new(
    code: 'Invalid URL',
    message: INVALID_URI_MSG,
    uri: url_string
  )
end
rescued_response(code, message) click to toggle source
# File lib/url_checker/single_checker.rb, line 42
def rescued_response(code, message)
  UrlChecker::RecuedResponse.new(
    code: code,
    message: message,
    uri: uri
  )
end
uri() click to toggle source
# File lib/url_checker/single_checker.rb, line 56
def uri
  @uri ||= Addressable::URI.parse url_string
end
valid_url?() click to toggle source
# File lib/url_checker/single_checker.rb, line 50
def valid_url?
  SCHEMES.include? uri.scheme
rescue Addressable::URI::InvalidURIError
  false
end