Class: Perfectline::ValidatesUrl::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_url.rb

Class Method Summary collapse

Class Method Details

.valid_url?(value, schemes) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/validates_url.rb', line 7

def self.valid_url?(value, schemes)
  schemes = [*schemes].map(&:to_s)
  begin
    uri = ::URI.parse(value)

    unless schemes.include?(uri.scheme)
      raise ::URI::InvalidURIError
    end

    unless uri.send(:scheme).present?
      raise ::URI::InvalidURIError
    end

    unless uri.send(:host).present?
      raise ::URI::InvalidURIError
    end

  rescue ::URI::InvalidURIError
    return false
  end

  true
end