Rails finding the Domain Name from a domain?
I have the following:
domain = email.split("@").last
Which takes an EMAIL like (steve.jobs@apple.com)开发者_如何学C and returns apple.com
I would like to know how to get just apple just the domain name, without the extension. Ideas?
Domain names don't have "extensions" they are just domain names and subdomains. What if you came across this address:
an.apprentice@mail.somecompany.co.uk
what pieces did you want?
You can take your split a step further
domainPieces = domain.split(".")
but then you have to decide what you want to do with those pieces.
possible code:
# expand this list
closedTlds = [ 'uk', 'pro' ]
# expand this list too
fullDomains = [ 'del.icio.us' ]
whoKnowsWhatThisActuallyMeans = ''
domain = email.split("@").last
domainPieces = domain.split(".")
if fullDomains.include?(domain)
whoKnowsWhatThisActuallyMeans = domain
else
if closedTlds.include?(domainPieces.Last)
whoKnowsWhatThisActuallyMeans = domainPieces[-3]
else
whoKnowsWhatThisActuallyMeans = domainPieces[-2]
end
end
whoKnowsWhatThisActuallyMeans # should return the correct value
I'm still learning ruby, so I think the syntax is right, but I'm not sure.
精彩评论