class WhoIsQuery
query = ARGV[0]
puts "Querying whois with #{query} ..."
results = IO.popen("whois -Q #{query}")
message = "No more domain for you!!"
results.readlines.each do |line|
if line.include?("No match for ")
message = "Domain waiting for you..."
break
end
end
puts message
end
Refactorings
No refactoring yet !
Jeremy Weiskotten
May 14, 2008, May 14, 2008 19:31, permalink
You can use Enumerable#any? instead of #each with a break...
class WhoIsQuery
query = ARGV[0]
puts "Querying whois with #{query} ..."
results = IO.popen("whois -Q #{query}")
message = if results.readlines.any? { |line| line.include?("No match for") }
"Domain waiting for you..."
else
"No more domain for you!!"
end
puts message
end
coderrr
May 23, 2008, May 23, 2008 17:34, permalink
no point in having the class if you aren't using it for anything
query = ARGV[0]
puts "Querying whois with #{query} ..."
message = if `whois -Q #{query}` =~ /no match for/i
then "Domain waiting for you..."
else "No more domain for you!!"
end
puts message
hugh Bien
May 23, 2008, May 23, 2008 17:55, permalink
make it one line for fun =]
puts "Querying whois with #{ARGV[0]} ...\n" + (`whois -Q #{ARGV[0]}` =~ /no match for/i ? "Domain waiting for you..." : "No more domain for you!!")
Peter Harkins
May 23, 2008, May 23, 2008 18:19, permalink
There are three Ruby whois libraries, you don't have to reinvent the wheel. And whois doesn't necessarily tell you that a domain is available for registration, you should be doing a DNS lookup.
Brad
August 29, 2009, August 29, 2009 11:49, permalink
This is kind of messy, but I'd use something like this myself. Sorry in advance, but I come from a C background which is reflected in my code. The code below does work in Ruby though, I've checked it.
if (ARGV[0] == nil)
printf("Syntax: ruby whois.rb <domain>\n");
Process.exit;
end
domain = ARGV[0];
query = `whois #{domain}`;
printf("Querying whois with %s\n",domain);
if (query.include?("No match for") == false)
printf("Sorry, but %s is taken\n",domain);
else
printf("Looks like %s is available for registration!\n",domain);
end
I'm new to ruby and I make this quick program to check if a domain is available, can we improve it?