Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you parse a url in Ruby to get the main domain?

I want to be able to parse any URL with Ruby to get the main part of the domain without the www (just the example.com)

like image 255
Justin Meltzer Avatar asked Jul 13 '11 04:07

Justin Meltzer


People also ask

What is parse domain?

Splits a hostname into subdomains, domain and (effective) top-level domains. Since domain name registrars organize their namespaces in different ways, it's not straight-forward to split a hostname into subdomains, the domain and top-level domains.


1 Answers

Please note there is no algorithmic method of finding the highest level at which a domain may be registered for a particular top-level domain (the policies differ with each registry), the only method is to create a list of all top-level domains and the level at which domains can be registered.

This is the reason why the Public Suffix List exists.

I'm the author of PublicSuffix, a Ruby library that decomposes a domain into the different parts.

Here's an example

require 'uri/http'  uri = URI.parse("http://toolbar.google.com") domain = PublicSuffix.parse(uri.host) # => "toolbar.google.com" domain.domain # => "google.com"  uri = URI.parse("http://www.google.co.uk") domain = PublicSuffix.parse(uri.host) # => "www.google.co.uk" domain.domain # => "google.co.uk" 
like image 140
Simone Carletti Avatar answered Sep 20 '22 16:09

Simone Carletti