Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The client can provide additional settings to the underlying WHOIS ad…

…apter.
  • Loading branch information...
commit e6e7167dba1a60938c952a8e37d5441de75c3436 1 parent 961bfc3
@weppos authored
View
1  lib/whois/client.rb
@@ -83,6 +83,7 @@ def query(object)
string = object.to_s
Timeout::timeout(timeout) do
@server = Server.guess(string)
+ @server.configure(settings)
@server.query(string)
end
end
View
22 lib/whois/server.rb
@@ -175,7 +175,7 @@ def self.factory(type, allocation, host, options = {})
end
- # Parses <tt>qstring</tt> and tries to guess the right server.
+ # Parses <tt>string</tt> and tries to guess the right server.
#
# It successfully detects the following query types:
# * ipv6
@@ -194,14 +194,14 @@ def self.factory(type, allocation, host, options = {})
# @param [String] string
# @return [Whois::Server::Adapters::Base]
# The adapter that can be used to perform
- # WHOIS queries for <tt>qstring</tt>.
+ # WHOIS queries for <tt>string</tt>.
#
# @raise [Whois::ServerNotFound]
# When unable to find an appropriate WHOIS adapter
- # for <tt>qstring</tt>. Most of the cases, the <tt>qstring</tt>
+ # for <tt>string</tt>. Most of the cases, the <tt>string</tt>
# haven't been recognised as one of the supported query types.
# @raise [Whois::ServerNotSupported]
- # When the <tt>qstring</tt> type is detected,
+ # When the <tt>string</tt> type is detected,
# but the object type doesn't have any supported WHOIS adapter associated.
#
def self.guess(string)
@@ -226,7 +226,7 @@ def self.guess(string)
end
# Gave Over
- raise ServerNotFound, "Unable to find a whois server for `#{string}'"
+ raise ServerNotFound, "Unable to find a WHOIS server for `#{string}'"
end
@@ -245,24 +245,24 @@ def self.matches_email?(string)
end
- def self.find_for_ip(qstring)
- ip = IPAddr.new(qstring)
+ def self.find_for_ip(string)
+ ip = IPAddr.new(string)
type = ip.ipv4? ? :ipv4 : :ipv6
definitions(type).each do |definition|
if IPAddr.new(definition.first).include?(ip)
return factory(type, *definition)
end
end
- raise AllocationUnknown, "IP Allocation for `#{qstring}' unknown. Server definitions might be outdated."
+ raise AllocationUnknown, "IP Allocation for `#{string}' unknown. Server definitions might be outdated."
end
- def self.find_for_email(qstring)
+ def self.find_for_email(string)
raise ServerNotSupported, "No WHOIS server is known for email objects"
end
- def self.find_for_domain(qstring)
+ def self.find_for_domain(string)
definitions(:tld).each do |definition|
- return factory(:tld, *definition) if /#{Regexp.escape(definition.first)}$/ =~ qstring
+ return factory(:tld, *definition) if /#{Regexp.escape(definition.first)}$/ =~ string
end
nil
end
View
17 lib/whois/server/adapters/base.rb
@@ -79,6 +79,15 @@ def ==(other)
alias_method :eql?, :==
+ # Merges given +settings+ into current {options}.
+ #
+ # @param [Hash] settings
+ # @return [Hash] The updated options for this object.
+ def configure(settings)
+ options.merge!(settings)
+ end
+
+
# Performs a Whois query for <tt>qstring</tt>
# using the current server adapter.
#
@@ -124,8 +133,8 @@ def append_to_buffer(response, host)
@buffer << Whois::Answer::Part.new(response, host)
end
- def query_the_socket(qstring, host, port = nil)
- ask_the_socket(qstring, host, port || options[:port] || DEFAULT_WHOIS_PORT)
+ def query_the_socket(query, host, port = nil)
+ ask_the_socket(query, host, port || options[:port] || DEFAULT_WHOIS_PORT)
end
def with_buffer(&block)
@@ -135,9 +144,9 @@ def with_buffer(&block)
result
end
- def ask_the_socket(qstring, host, port)
+ def ask_the_socket(query, host, port)
client = TCPSocket.open(host, port)
- client.write("#{qstring}\r\n") # I could use put(foo) and forget the \n
+ client.write("#{query}\r\n") # I could use put(foo) and forget the \n
client.read # but write/read is more symmetric than puts/read
ensure # and I really want to use read instead of gets.
client.close if client # If != client something went wrong.
View
20 spec/whois/client_spec.rb
@@ -42,13 +42,15 @@
context "#query" do
it "coerces the argument to string" do
- server = Object.new
- # I can't use the String because Array#to_s behaves differently
+ # I can't use the String in place of instance_of(String)
+ # because Array#to_s behaves differently
# on Ruby 1.8.7 and Ruby 1.9.1
# http://redmine.ruby-lang.org/issues/show/2617
+
+ server = Whois::Server::Adapters::Base.new(:tld, ".test", "example.test")
server.expects(:query).with(instance_of(String))
Whois::Server.expects(:guess).with(instance_of(String)).returns(server)
- klass.new.query(["google", ".", "com"])
+ klass.new.query(["google", ".", "test"])
end
it "detects email" do
@@ -74,36 +76,36 @@
end
it "raises if timeout is exceeded" do
- server = Class.new do
+ adapter = Class.new(Whois::Server::Adapters::Base) do
def query(*args)
sleep(2)
end
end
- Whois::Server.expects(:guess).returns(server.new)
+ Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "example.test"))
client = klass.new(:timeout => 1)
lambda { client.query("foo.com") }.should raise_error(Timeout::Error)
end
it "doesn't raise if timeout is not exceeded" do
- server = Class.new do
+ adapter = Class.new(Whois::Server::Adapters::Base) do
def query(*args)
sleep(1)
end
end
- Whois::Server.expects(:guess).returns(server.new)
+ Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "example.test"))
client = klass.new(:timeout => 5)
lambda { client.query("foo.com") }.should_not raise_error
end
it "supports unlimited timeout" do
- server = Class.new do
+ adapter = Class.new(Whois::Server::Adapters::Base) do
def query(*args)
sleep(1)
end
end
- Whois::Server.expects(:guess).returns(server.new)
+ Whois::Server.expects(:guess).returns(adapter.new(:tld, ".test", "example.test"))
client = klass.new.tap { |c| c.timeout = nil }
lambda { client.query("foo.com") }.should_not raise_error
View
17 spec/whois/server/adapters/base_spec.rb
@@ -62,6 +62,23 @@
end
end
+
+ describe "#configure" do
+ it "merges settings with current options" do
+ a = klass.new(:tld, ".test", "whois.test", { :hello => "world" })
+ a.configure(:foo => "bar")
+ a.options.should == { :hello => "world", :foo => "bar" }
+ end
+
+ it "gives higher priority to settings argument" do
+ a = klass.new(:tld, ".test", "whois.test", { :foo => "bar" })
+ a.options.should == { :foo => "bar" }
+ a.configure(:foo => "baz")
+ a.options.should == { :foo => "baz" }
+ end
+ end
+
+
describe "#query" do
it "raises NotImplementedError" do
lambda { klass.new(*@definition).query("example.test") }.should raise_error(NotImplementedError)
Please sign in to comment.
Something went wrong with that request. Please try again.