Skip to content

Commit

Permalink
Validate the response when attempting to get a property and raise if …
Browse files Browse the repository at this point in the history
…the response is throttled (references #61).
  • Loading branch information
weppos committed Feb 18, 2011
1 parent f392458 commit 59e7ffb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lib/whois/answer/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ def self.property_register(property, status, &block)

class_eval(<<-RUBY, __FILE__, __LINE__ + 1) if status == :supported
def #{property}(*args)
cached_properties_fetch(:#{property}) { internal_#{property}(*args) }
cached_properties_fetch(:#{property}) do
validate!
internal_#{property}(*args)
end
end
RUBY

Expand Down Expand Up @@ -253,6 +256,11 @@ def is(symbol)
respond_to?(symbol) && send(symbol)
end

# @api internal
def validate!
raise ResponseIsThrottled if is(:throttled?)
end


# @group Properties

Expand Down Expand Up @@ -339,9 +347,6 @@ def unchanged?(other)
def throttled?
end

# Let it to be documented
undef :throttled?

# Checks whether this is an incomplete response.
# The default implementation always returns +nil+.
#
Expand All @@ -356,8 +361,9 @@ def throttled?
def incomplete?
end

# Let it to be documented
# Let them be documented
undef :incomplete?
undef :throttled?

# @endgroup

Expand Down
15 changes: 15 additions & 0 deletions lib/whois/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,19 @@ class PropertyNotAvailable < ParserError

# @endgroup


# @group Response

# Generic class for response errors.
class ResponseError < Error
end

# Raised when attempting to access a property when the response is throttled.
#
# @see Whois::Answer::Parser::Base#throttled?
class ResponseIsThrottled < ResponseError
end

# @endgroup

end
59 changes: 56 additions & 3 deletions spec/whois/answer/parser/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@
end
end

describe "#is" do
it "calls the method if the object respond to the method" do
i = Class.new(klass) { def throttled?; true; end }.new(Whois::Answer::Part.new)
i.is(:throttled?)
end
it "does not call the method if the object does not respond to the method" do
i = Class.new(klass).new(Whois::Answer::Part.new)
i.is(:throttled?).should be_false
end
end

describe "#validate!" do
it "raises Whois::ResponseIsThrottled when the response is throttled" do
i = Class.new(klass) { def throttled?; true; end }.new(Whois::Answer::Part.new)
lambda { i.validate! }.should raise_error(Whois::ResponseIsThrottled)

i = Class.new(klass) { def throttled?; false; end }.new(Whois::Answer::Part.new)
lambda { i.validate! }.should_not raise_error
end
end


describe "#changed?" do
it "raises if the argument is not an instance of the same class" do
Expand Down Expand Up @@ -160,13 +181,12 @@
c1 = Whois::Answer::Contact.new(:id => "1st", :name => "foo")
c2 = Whois::Answer::Contact.new(:id => "2nd", :name => "foo")
c3 = Whois::Answer::Contact.new(:id => "3rd", :name => "foo")
k = Class.new(klass) do
i = Class.new(klass) do
property_register(:registrant_contact, :supported) { [c1, c2] }
property_register(:admin_contact, :supported) { nil }
property_register(:technical_contact, :supported) { c3 }
end
end.new(@part)

i = k.new(@part)
i.contacts.should == [c1, c2, c3]
end

Expand Down Expand Up @@ -210,3 +230,36 @@
end

end

describe Whois::Answer::Parser::Base, "Parser Behavior" do

Klass = Class.new(Whois::Answer::Parser::Base) do
property_supported(:domain) { "example.com" }
property_not_supported(:domain_id)

def throttled?
part.host == "throttled.whois.test"
end
end

context "property supported" do
it "raises Whois::ResponseIsThrottled when the response is throttled" do
i = Klass.new(Whois::Answer::Part.new("", "throttled.whois.test"))
lambda { i.domain }.should raise_error(Whois::ResponseIsThrottled)

i = Klass.new(Whois::Answer::Part.new("", "success.whois.test"))
lambda { i.domain }.should_not raise_error
end
end

context "property not supported" do
it "raises Whois::ResponseIsThrottled when the response is throttled" do
i = Klass.new(Whois::Answer::Part.new("", "throttled.whois.test"))
lambda { i.domain_id }.should raise_error(Whois::PropertyNotSupported)

i = Klass.new(Whois::Answer::Part.new("", "success.whois.test"))
lambda { i.domain_id }.should raise_error(Whois::PropertyNotSupported)
end
end

end

0 comments on commit 59e7ffb

Please sign in to comment.