Remove subdomain from string in ruby

前端 未结 8 1828
再見小時候
再見小時候 2020-12-17 00:32

I\'m looping over a series of URLs and want to clean them up. I have the following code:

# Parse url to remove http, path and check format
o_url = URI.parse(         


        
8条回答
  •  生来不讨喜
    2020-12-17 00:47

    I just wrote a library to do this called Domainatrix. You can find it here: http://github.com/pauldix/domainatrix

    require 'rubygems'
    require 'domainatrix'
    
    url = Domainatrix.parse("http://www.pauldix.net")
    url.public_suffix       # => "net"
    url.domain    # => "pauldix"
    url.canonical # => "net.pauldix"
    
    url = Domainatrix.parse("http://foo.bar.pauldix.co.uk/asdf.html?q=arg")
    url.public_suffix       # => "co.uk"
    url.domain    # => "pauldix"
    url.subdomain # => "foo.bar"
    url.path      # => "/asdf.html?q=arg"
    url.canonical # => "uk.co.pauldix.bar.foo/asdf.html?q=arg"
    

提交回复
热议问题