Module: SchemaDotOrg::UrlValidator

Defined in:
lib/schema_dot_org/url_validator.rb

Overview

Utility module for URL validation

Class Method Summary collapse

Class Method Details

.valid_web_url?(url) ⇒ Boolean

Validates that a string is a proper web URL (HTTP/HTTPS with a host)

Doc Tests:

>> SchemaDotOrg::UrlValidator.valid_web_url?('https://example.com')     #=> true
>> SchemaDotOrg::UrlValidator.valid_web_url?('http://example.com/path') #=> true
>> SchemaDotOrg::UrlValidator.valid_web_url?('ftp://example.com')       #=> false
>> SchemaDotOrg::UrlValidator.valid_web_url?('not-a-url')               #=> false
>> SchemaDotOrg::UrlValidator.valid_web_url?('')                        #=> false
>> SchemaDotOrg::UrlValidator.valid_web_url?(nil)                       #=> false

Parameters:

  • url (String)

    The URL string to validate

Returns:

  • (Boolean)

    true if the URL is valid, false otherwise



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/schema_dot_org/url_validator.rb', line 21

def self.valid_web_url?(url)
  return false if url.nil? || url.to_s.strip.empty?

  uri = URI.parse(url.to_s)

  # Check for valid HTTP/HTTPS scheme
  return false unless %w[http https].include?(uri.scheme)

  # Check for a host
  return false if uri.host.nil? || uri.host.strip.empty?

  true
rescue URI::InvalidURIError
  false
end