I created a String class function using the Open Class method for my purpose .
class String def to_dn return '' if self.blank? return self.split('@').last if self.match('@') link = self link = "http://#{link}" unless link.match(/^(http:\/\/|https:\/\/)/) link = URI.parse(URI.encode(link)).host.present? ? URI.parse(URI.encode(link)).host : link.strip domain_name = link.sub(/.*?www./,'') domain_name = domain_name.match(/[AZ]+.[AZ]{2,4}$/i).to_s if domain_name.split('.').length >= 2 && domain_name.match(/[AZ]+.[AZ]{2,4}$/i).present? end end
Example:
1. "https://www.facebook.com/someuser".to_dn = "facebook.com" 2. "www.facebook.com/someuser".to_dn = "facebook.com" 3. "facebook.com/someuser".to_dn = "facebook.com" 4. "http://someuser.tumblr.com".to_dn = "tumblr.com" 5. "dc.ads.linkedin.com".to_dn = "linkedin.com" 6. ' your_name@domain.com '.to_dn = "domain.com"
It also works for email addresses (which are required for my purpose). Hope this will be helpful to others. Correct me if you find something wrong :)
Note. This will not work for "www.domainname.co.in". I'm working on it:)
source share