Sha256: 82c2fee4b452185f1582077b9f139ef08632ab44f73e6309f610406c189cbfa3

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

module Yawast
  module Commands
    class Utils
      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yawast-0.2.2 lib/commands/utils.rb
yawast-0.2.1 lib/commands/utils.rb
yawast-0.2.0.beta3 lib/commands/utils.rb
yawast-0.2.0.beta2 lib/commands/utils.rb
yawast-0.2.0.beta1 lib/commands/utils.rb