Class: Yawast::Shared::Uri

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

Class Method Summary collapse

Class Method Details

.extract_uri(url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/shared/uri.rb', line 8

def self.extract_uri(url)
  # this might be buggy - actually, I know it is...
  url = 'http://' + url unless url.include?('http://') || url.include?('https://')

  # make sure the path is at least a slash
  uri = URI.parse(url)
  uri.path = '/' if uri.path == ''

  # this is buggy, but we don't handle files anyhow...
  # if the path doesn't end in a slash, add one.
  uri.path.concat '/' if uri.path[-1, 1] != '/'

  # see if we can resolve the host
  # we don't really need it, it just serves as validation
  begin
    dns = Resolv::DNS.new
    dns.getaddress(uri.host)
  rescue => e # rubocop:disable Style/RescueStandardError
    if uri.host == 'localhost'
      # do nothing, in this case, we just don't care.
    elsif IPAddress.valid? uri.host
      # in this case the host name is actually a IP, let it go through.
    else
      # we've passed all the exceptions, if we are here, it's a problem
      raise ArgumentError, "Invalid URL (#{e.message})"
    end
  end

  uri
end