Skip to content

Instantly share code, notes, and snippets.

@thomasleese
Created July 3, 2017 14:07
Show Gist options
  • Select an option

  • Save thomasleese/2c321fbe5279ff336a291dbe2c999901 to your computer and use it in GitHub Desktop.

Select an option

Save thomasleese/2c321fbe5279ff336a291dbe2c999901 to your computer and use it in GitHub Desktop.
module LinkChecker::UriChecker
class UriCheckerError < Error
def initialize(options = {})
super(suggested_fix: :check_correct_manually, options)
end
end
class UriCheckerWarning < Warning
def initialize(options = {})
super(suggested_fix: :check_correct_manually, options)
end
end
class MissingUriScheme < UriCheckerError
def initialize
super(summary: :invalid_url, message: :link_missing_scheme)
end
end
class InvalidUri < UriCheckerError
def initialize
super(summary: :invalid_url, message: :not_a_valid_link)
end
end
class ContactDetails < UriCheckerWarning
def initialize
super(summary: :contact_details, message: :links_to_contact_details)
end
end
class UnusualUrl < UriCheckerWarning
def initialize
super(summary: :unusual_url, message: :link_is_unsupported)
end
end
class ValidUriChecker < Checker
def call
if parsed_uri.scheme.nil?
add_problem(MissingUriScheme.new)
elsif HTTP_URI_SCHEMES.include?(parsed_uri.scheme)
report.merge(HttpChecker.new(parsed_uri, redirect_history: redirect_history).call)
elsif FILE_URI_SCHEMES.include?(parsed_uri.scheme)
report.merge(FileChecker.new(parsed_uri, redirect_history: redirect_history).call)
elsif CONTACT_SCHEMES.include?(parsed_uri.scheme)
add_problem(ContactDetails.new)
else
add_problem(UnusualUrl.new)
end
rescue URI::InvalidURIError
add_problem(InvalidUri.new)
end
private
HTTP_URI_SCHEMES = %w(http https).freeze
FILE_URI_SCHEMES = %w(file).freeze
CONTACT_SCHEMES = %w(mailto tel).freeze
def parsed_uri
@parsed_uri ||= URI.parse(uri)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment