Friday, April 19, 2013

Extract Domain name from URL and validate it

require 'rubygems'
require 'public_suffix'
require 'uri'

urls = [
        'http://google.com/sdsd/sdasd.html',
        'https://www.google.com/dsf/dsfds',
        'http://sda.google.com/dfsdfs',
        'http://sdfsf.google.co.in/dfsd/dfds',
        'http://google.dhjdf/sdas/sdada',
        'http://sd.dfsd.google.co.uk/dsfs/dsfs'
        ]
 
urls.each do |url|

        domainname = URI.parse(url).host
        if(PublicSuffix.valid?(domainname))
        domain = PublicSuffix.parse(domainname)
        puts domain.domain
        else
        puts "invalid domain : #{domainname}"
        end

end

Output :
google.com
google.com
google.com
google.co.in
invalid domain : google.dhjdf
google.co.uk