7a1f5bc20a17bba473d54617d9893466

I'm new to ruby and I make this quick program to check if a domain is available, can we improve it?

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 !

5170ca260dbd2cdfd5a887a4dba7636f

Jeremy Weiskotten

May 14, 2008, May 14, 2008 19:31, permalink

No rating. Login to rate!

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
F2c8d2096467c682e01ce70dc5525da4

Andrea Ortiz

May 17, 2008, May 17, 2008 17:17, permalink

No rating. Login to rate!

YES

Af4ce9309f8c4f7fc5cb33e7a5b08c64

coderrr

May 23, 2008, May 23, 2008 17:34, permalink

No rating. Login to rate!

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
D41d8cd98f00b204e9800998ecf8427e

hugh Bien

May 23, 2008, May 23, 2008 17:55, permalink

No rating. Login to rate!

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!!")
E7be134a6de3d308adf765be1004c450

Peter Harkins

May 23, 2008, May 23, 2008 18:19, permalink

No rating. Login to rate!

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.

F53a59cf5cd475ddfb08b865a0ae1256

Brad

August 29, 2009, August 29, 2009 11:49, permalink

No rating. Login to rate!

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

Your refactoring





Format Copy from initial code

or Cancel