Skip to content

Commit

Permalink
fixed Contact.find_all_by_emails, and added a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
idris committed Sep 19, 2010
1 parent fa79acb commit 4104e83
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/constant_contact/contact.rb
Expand Up @@ -78,7 +78,7 @@ def self.find_by_email(email_address)
# But we need: email=<email 1>&email=<email 2>
def self.find_all_by_emails(emails)
raise ArgumentError, "Expected an array of emails, got a(n) #{emails.class.name}" unless emails.is_a? Array
return [] if email_addresses.empty?
return [] if emails.empty?

query_string = emails.map{|e| "email=#{CGI.escape(e.to_s.downcase)}"}.join('&')

Expand Down
2 changes: 1 addition & 1 deletion lib/constant_contact/version.rb
@@ -1,3 +1,3 @@
module ConstantContact
Version = '1.1.3'.freeze
Version = '1.1.4'.freeze
end
9 changes: 9 additions & 0 deletions test/constant_contact/contact_test.rb
Expand Up @@ -107,12 +107,21 @@ class ContactTest < Test::Unit::TestCase
ConstantContact::Base.api_key = "api_key"
stub_get('https://api_key%25joesflowers:password@api.constantcontact.com/ws/customers/joesflowers/contacts', 'all_contacts.xml')
stub_get('https://api_key%25joesflowers:password@api.constantcontact.com/ws/customers/joesflowers/contacts?email=jon%40example.com', 'single_contact_by_email.xml')
stub_get('https://api_key%25joesflowers:password@api.constantcontact.com/ws/customers/joesflowers/contacts?email=jon%40example.com&email=my%40example.com', 'multiple_contacts_by_emails.xml')
end

should 'find contact with email address' do
assert_equal 'smith, jon', ConstantContact::Contact.find(:first, :params => {:email => 'jon@example.com'}).Name
end

should 'find two contacts with two email addresses' do
contacts = ConstantContact::Contact.find_all_by_emails(['jon@example.com', 'my@example.com'])
assert_equal Array, contacts.class
assert_equal 2, contacts.size
assert_equal 'smith, jon', contacts[0].Name
assert_equal 'Doe, Marvin', contacts[1].Name
end

should 'return nil when asking for contact_lists' do
assert_equal nil, ConstantContact::Contact.find(:first, :params => {:email => 'jon@example.com'}).contact_lists
end
Expand Down
55 changes: 55 additions & 0 deletions test/fixtures/multiple_contacts_by_emails.xml
@@ -0,0 +1,55 @@
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts</id>
<title type="text">Contacts for Customer: joesflowers</title>
<link href="contacts" />
<link href="contacts" rel="self" />
<author>
<name>joesflowers</name>
</author>
<updated>2010-04-21T23:24:32.247Z</updated>
<link href="/ws/customers/joesflowers/contacts" rel="first" />
<link href="/ws/customers/joesflowers/contacts?email=jon%40example.com&email=my%40example.com" rel="current" />
<entry>
<link href="/ws/customers/joesflowers/contacts/2" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/2</id>
<title type="text">Contact: jon@example.com</title>
<updated>2010-04-21T23:24:32.268Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/2">
<Status>Active</Status>
<EmailAddress>jon@example.com</EmailAddress>
<EmailType>HTML</EmailType>
<Name>smith, jon</Name>
<OptInTime>2010-04-21T18:35:34.066Z</OptInTime>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<Confirmed>false</Confirmed>
<InsertTime>2010-04-21T18:35:34.066Z</InsertTime>
</Contact>
</content>
</entry>
<entry>
<link href="/ws/customers/joesflowers/contacts/2" rel="edit" />
<id>http://api.constantcontact.com/ws/customers/joesflowers/contacts/1</id>
<title type="text">Contact: my@example.com</title>
<updated>2010-04-21T23:24:32.268Z</updated>
<author>
<name>Constant Contact</name>
</author>
<content type="application/vnd.ctct+xml">
<Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/joesflowers/contacts/1">
<Status>Active</Status>
<EmailAddress>my@example.com</EmailAddress>
<EmailType>HTML</EmailType>
<Name>Doe, Marvin</Name>
<OptInTime>2010-04-14T22:09:42.950Z</OptInTime>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<Confirmed>false</Confirmed>
<InsertTime>2010-04-21T18:35:34.066Z</InsertTime>
</Contact>
</content>
</entry>
</feed>

0 comments on commit 4104e83

Please sign in to comment.