[Nitro] [NP.ORG] Nitro/Og Fora daily digest

Robert Mela rob at robmela.com
Mon May 14 21:34:06 EDT 2007


This is slow but probably correct.  I'm not sure it qualifies as 
elegant.  Anything else would probably require encoding some information 
about valid domain naming, and would fail on any unusual, 
organization-internal domain naming.

robert-melas-ibook-g4:~/robmela.com robertmela$ vim foo.cpp

def domain_base(str)
   domain = nil
   str.split(/\./).reverse.each do |part|
      begin
         domain = domain ? [ part, domain ].join('.') : part
         return Socket::gethostbyname( domain )[0]
      rescue
      end
   end
   return nil
end

puts domain_base('www.cnet.co.uk')
puts domain_base('gmosx.nitro.com')
puts domain_base('ci.lexington.ma.us')
puts domain_base('localhost')
puts domain_base('voozzlbp.szligscky.vrzovnobitz')

~                                                          

Output:

cnet.co.uk
nitro.com
ci.lexington.ma.us
localhost
nil





george.moschovitis at gmail.com wrote:
> >From gmosx.myopenid.com 
> Date: Mon May 14 06:20:25 UTC 2007
> Subject: Extract main part of domain
> Message-ID: http://www.nitroproject.org/fora/posts/view/a0hNacaEmr3kiUeJeZaaqU
>
> <Dear devs,
>
> I am wondering if anyone has an elegant solution to this problem: I want to extact the 'main' part of a domain, for example:
>
> www.nitro.com ->< nitro.com
> localhost:8000 ->< localhost:8000
> gmosx.nitro.com -> nitro.com
>
> If someone can provide an elegant snippet of code, I would like to see this.
>
> -g.
>
>
>
>
> --
> This mail is automatically generated from the http://nitroproject.org/fora
> digest robot. It presents the discussions in the fora during the last 24 hours.
> Do not reply to this email.
> _______________________________________________
> Nitro-general mailing list
> Nitro-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/nitro-general
>
>
>   

-------------- next part --------------
A non-text attachment was scrubbed...
Name: rob.vcf
Type: text/x-vcard
Size: 116 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/nitro-general/attachments/20070514/55e07080/attachment-0001.vcf 


More information about the Nitro-general mailing list