I am trying to evaluate user-submitted urls to find out whether they contain valid hostnames (formatting) and if so, extract the hostname. Know of any libraries/methods that could help?
Example:
user_input = "www.google.com"
if user_input.has_valid_host?
hostname = user_input.get_hostname #=> "google.com"
url = "http://" + @hostname #=> "http://google.com"
else
puts "Invalid URL"
end
This example is very simple but I need the url checked against all valid domain extensions and the hostname extracted from any string (assuming that it’s present)
I don’t know ruby, but I wouldn’t think of this as a ruby question.
I would use regex to split out the hostname as you suggest.
Then I would do a system call to the nslookup routine.
On a Windows system from the command prompt it is nslookup.
From Ruby you should do an API call instead of using the command line, but both will eventually interface to the DNS service on the local machine.
See: Is there a good DNS server library in ruby?