Skip to content

Commit

Permalink
Change (registrant|admin|technical)_contact properties to return an A…
Browse files Browse the repository at this point in the history
…rray of Contact.
  • Loading branch information
weppos committed Mar 8, 2011
1 parent 4375b6d commit 0af79a9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
49 changes: 46 additions & 3 deletions lib/whois/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def initialize(server, parts)
# Checks if this class respond to given method.
#
# Overrides the default implementation to add support
# for {PROPERTIES} and {METHODS}.
# for {Parser::PROPERTIES} and {Parser::METHODS}.
#
# @returns [Boolean]
def respond_to?(symbol, include_private = false)
Expand Down Expand Up @@ -166,6 +166,51 @@ def properties

# @group Methods

# Shortcut for <tt>#registrant_contacts.first</tt>.
#
# @return [Whois::Answer::Contact]
# If the property is supported and a contact exists.
# @return [nil]
# If the property is not supported or the contact doesn't exist.
#
# @see Answer#registrant_contacts
#
def registrant_contact
if property_supported?(:registrant_contacts)
parser.registrant_contacts.first
end
end

# Shortcut for <tt>#admin_contacts.first</tt>.
#
# @return [Whois::Answer::Contact]
# If the property is supported and a contact exists.
# @return [nil]
# If the property is not supported or the contact doesn't exist.
#
# @see Answer#admin_contacts
#
def admin_contact
if property_supported?(:admin_contacts)
parser.admin_contacts.first
end
end

# Shortcut for <tt>#technical_contacts.first</tt>.
#
# @return [Whois::Answer::Contact]
# If the property is supported and a contact exists.
# @return [nil]
# If the property is not supported or the contact doesn't exist.
#
# @see Answer#technical_contacts
#
def technical_contact
if property_supported?(:technical_contacts)
parser.technical_contacts.first
end
end

# Collects and returns all the contacts.
#
# @return [Array<Whois::Answer::Contact>]
Expand Down Expand Up @@ -251,8 +296,6 @@ def self.define_property_method(method)
def #{method}(*args, &block)
if property_supported?(:#{method})
parser.#{method}(*args, &block)
else
nil
end
end
RUBY
Expand Down
2 changes: 1 addition & 1 deletion lib/whois/answer/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Parser
:status, :available?, :registered?,
:created_on, :updated_on, :expires_on,
:registrar,
:registrant_contact, :admin_contact, :technical_contact,
:registrant_contacts, :admin_contacts, :technical_contacts,
:nameservers,
]

Expand Down
4 changes: 2 additions & 2 deletions lib/whois/answer/parser/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def validate!
# @see Whois::Answer::Parser#contacts
#
def contacts
[:registrant_contact, :admin_contact, :technical_contact].inject([]) do |contacts, property|
contacts += Array.wrap(send(property)) if property_supported?(property)
[:registrant_contacts, :admin_contacts, :technical_contacts].inject([]) do |contacts, property|
contacts += send(property) if property_supported?(property)
contacts
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/whois/answer/parser/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@
c2 = Whois::Answer::Contact.new(:id => "2nd", :name => "foo")
c3 = Whois::Answer::Contact.new(:id => "3rd", :name => "foo")
i = Class.new(klass) do
property_register(:registrant_contact, :supported) { [c1, c2] }
property_register(:admin_contact, :supported) { nil }
property_register(:technical_contact, :supported) { c3 }
property_register(:registrant_contacts, :supported) { [c1, c2] }
property_register(:admin_contacts, :supported) { [] }
property_register(:technical_contacts, :supported) { [c3] }
end.new(@part)

i.contacts.should == [c1, c2, c3]
Expand Down
8 changes: 4 additions & 4 deletions spec/whois/answer/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ class Whois::Answer::Parser::Contacts1Test < Whois::Answer::Parser::Base
end

class Whois::Answer::Parser::Contacts2Test < Whois::Answer::Parser::Base
property_supported(:technical_contact) { "p2-t1" }
property_supported(:admin_contact) { "p2-a1" }
property_supported(:registrant_contact) { nil }
property_supported(:technical_contacts) { ["p2-t1"] }
property_supported(:admin_contacts) { ["p2-a1"] }
property_supported(:registrant_contacts) { [] }
end

class Whois::Answer::Parser::Contacts3Test< Whois::Answer::Parser::Base
property_supported(:technical_contact) { "p3-t1" }
property_supported(:technical_contacts) { ["p3-t1"] }
end

it "returns an empty array when 0 parts" do
Expand Down
4 changes: 2 additions & 2 deletions spec/whois/answer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,13 @@ def happy; "yes"; end
end.should raise_error(NoMethodError)
end

it "doesn't catch all methods" do
it "does not catch all methods" do
lambda do
klass.new(nil, []).i_am_not_defined
end.should raise_error(NoMethodError)
end

it "doesn't catch all question methods" do
it "does not catch all question methods" do
lambda do
klass.new(nil, []).i_am_not_defined?
end.should raise_error(NoMethodError)
Expand Down

0 comments on commit 0af79a9

Please sign in to comment.