How do I get just the sitename from url in ruby?

parsing, ruby, url

Solution

Using a gem for this might be overkill, but anyway: There's a handy gem called domainatrix that can extract the sitename for your while dealing with things like two element top-level domains and more.

url = Domainatrix.parse("http://www.pauldix.net")
url.url       # => "http://www.pauldix.net" (the original url)
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"

Problem

I have a url such as: ``` http://www.relevantmagazine.com/life/relationship/blog/23317-pursuing-singleness ``` And would like to extract just relevantmagazine from it. Currently I have: ``` @urlroot = URI.parse(@link.url).host ``` But it returns www.relevantmagazine.com can anyone help me?

Original source

Related problems