Sha256: 3cfc7cdcf40748aab5421e74cdb3b6911c3e07b219ef8870d907cc297b9b0dd9

Contents?: true

Size: 1.18 KB

Versions: 23

Compression:

Stored size: 1.18 KB

Contents

require 'ipaddress'

module Yawast
  module Shared
    class Uri
      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.
        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
          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.new("Invalid URL (#{e.message})")
          end
        end

        return uri
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
yawast-0.7.0.beta1 lib/shared/uri.rb
yawast-0.6.0 lib/shared/uri.rb
yawast-0.6.0.beta6 lib/shared/uri.rb
yawast-0.6.0.beta5 lib/shared/uri.rb
yawast-0.6.0.beta4 lib/shared/uri.rb
yawast-0.6.0.beta3 lib/shared/uri.rb
yawast-0.6.0.beta2 lib/shared/uri.rb
yawast-0.6.0.beta1 lib/shared/uri.rb
yawast-0.5.2 lib/shared/uri.rb
yawast-0.5.1 lib/shared/uri.rb
yawast-0.5.1.beta1 lib/shared/uri.rb
yawast-0.5.0 lib/shared/uri.rb
yawast-0.5.0.beta8 lib/shared/uri.rb
yawast-0.5.0.beta7 lib/shared/uri.rb
yawast-0.5.0.beta6 lib/shared/uri.rb
yawast-0.5.0.beta5 lib/shared/uri.rb
yawast-0.5.0.beta4 lib/shared/uri.rb
yawast-0.5.0.beta3 lib/shared/uri.rb
yawast-0.5.0.beta2 lib/shared/uri.rb
yawast-0.5.0.beta1 lib/shared/uri.rb