Class: Yawast::Commands::Utils

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

Class Method Summary collapse

Class Method Details

.extract_uri(args) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
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
# File 'lib/commands/utils.rb', line 4

def self.extract_uri(args)
  raise ArgumentError.new('You must specify a URL.') if args.empty?

  #this might be a bad assumption
  url = args[0]

  #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.
  if uri.path[-1, 1] != '/'
    uri.path.concat '/'
  end

  #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
    raise ArgumentError.new("Invalid URL (#{e.message})") unless uri.host == 'localhost'
  end

  return uri
end