Class: Uniword::Validation::Checkers::ExternalLinkChecker

Inherits:
LinkChecker
  • Object
show all
Defined in:
lib/uniword/validation/checkers/external_link_checker.rb

Overview

Validates external HTTP/HTTPS links.

Responsibility: Validate external URLs are accessible. Single Responsibility: External link validation only.

Performs HTTP requests to verify:

  • URL is accessible

  • Response status is acceptable

  • Follows redirects if configured

Configuration options:

  • timeout: Request timeout in seconds

  • retry_count: Number of retries on failure

  • retry_delay: Delay between retries in seconds

  • allowed_status_codes: HTTP status codes considered valid

  • follow_redirects: Whether to follow redirects

  • max_redirects: Maximum number of redirects to follow

  • check_ssl: Whether to verify SSL certificates

  • user_agent: User agent string for requests

Examples:

Create and use checker

checker = ExternalLinkChecker.new(config: {
  timeout: 10,
  retry_count: 3
})
result = checker.check(hyperlink)

Constant Summary collapse

DEFAULTS =

Default configuration values

{
  timeout: 10,
  retry_count: 3,
  retry_delay: 1,
  allowed_status_codes: [200, 201, 301, 302, 303, 307, 308],
  follow_redirects: true,
  max_redirects: 5,
  check_ssl: true,
  user_agent: "Uniword Link Validator/1.0",
}.freeze

Instance Attribute Summary

Attributes inherited from LinkChecker

#config

Instance Method Summary collapse

Methods inherited from LinkChecker

#initialize

Constructor Details

This class inherits a constructor from Uniword::Validation::LinkChecker

Instance Method Details

#can_check?(link) ⇒ Boolean

Check if this checker can validate the given link.

Examples:

checker.can_check?(hyperlink) # => true

Parameters:

  • link (Object)

    The link to check

Returns:

  • (Boolean)

    true if link has an HTTP/HTTPS URL



56
57
58
59
60
61
62
# File 'lib/uniword/validation/checkers/external_link_checker.rb', line 56

def can_check?(link)
  return false unless enabled?
  return false unless link.respond_to?(:url)

  url = link.url
  url&.to_s&.match?(%r{^https?://})
end

#check(link, _document = nil) ⇒ ValidationResult

Validate the external link.

Examples:

result = checker.check(hyperlink)

Parameters:

  • link (Object)

    The link to validate

  • document (Object)

    The document (unused for external links)

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/uniword/validation/checkers/external_link_checker.rb', line 72

def check(link, _document = nil)
  unless enabled?
    return ValidationResult.unknown(link,
                                    "Checker disabled")
  end

  url = link.url
  retry_count = config_value(:retry_count, DEFAULTS[:retry_count])

  # Attempt with retries
  last_error = nil
  (retry_count + 1).times do |attempt|
    return perform_check(url, link)
  rescue StandardError => e
    last_error = e
    if attempt < retry_count
      sleep(config_value(:retry_delay,
                         DEFAULTS[:retry_delay]))
    end
  end

  # All retries failed
  ValidationResult.failure(
    link,
    "Failed to connect after #{retry_count + 1} attempts: #{last_error.message}",
    metadata: { error: last_error.class.name },
  )
end