Permalink
Browse files

Provide functionality for Constant Contact events to be posted with r…

…aw data from caller.
  • Loading branch information...
1 parent 9967f02 commit 4276233867e866fdddf561d32cdc3aeaee338a5d @freerobby freerobby committed with idris Dec 13, 2010
Showing with 58 additions and 33 deletions.
  1. +11 −8 lib/constant_contact/activity.rb
  2. +47 −25 test/constant_contact/activity_test.rb
@@ -2,7 +2,7 @@
module ConstantContact
class Activity < Base
self.format = ActiveResource::Formats::HtmlEncodedFormat
- attr_accessor :contacts, :lists, :activity_type
+ attr_accessor :contacts, :lists, :activity_type, :raw_data # Data is a reserved word in Rails
def self.parse_id(url)
url.to_s.split('/').last
@@ -29,16 +29,19 @@ def activity_type
protected
def encoded_data
result = "&data="
- result += CGI.escape("Email Address,First Name,Last Name\n")
- contact_strings = []
- self.contacts.each do |contact|
- contact_strings << "#{contact.email_address}, #{contact.first_name}, #{contact.last_name}"
- end
- result += CGI.escape(contact_strings.join("\n"))
+ if self.raw_data.nil?
+ result += CGI.escape("Email Address,First Name,Last Name\n")
+ contact_strings = []
+ self.contacts.each do |contact|
+ contact_strings << "#{contact.email_address}, #{contact.first_name}, #{contact.last_name}"
+ end
+ result += CGI.escape(contact_strings.join("\n"))
+ else
+ result += CGI.escape(self.raw_data)
+ end
return result
end
-
def encoded_lists
result = ""
self.lists.each do |list|
@@ -3,33 +3,55 @@
class Activity < Test::Unit::TestCase
context 'encode' do
- setup do
- @contacts = []
- 3.times do |n|
- @contacts << ConstantContact::Contact.new(:first_name => "Fred#{n}",
- :last_name => "Test#{n}",
- :email_address => "email#{n}@gmail.com")
- end
- @activity = ConstantContact::Activity.new(:activity_type => "SV_ADD")
- @activity.contacts = @contacts
- @list = ConstantContact::List.new
- @list.stubs(:id).returns('http://api.constantcontact.com/ws/customers/joesflowers/lists/2')
- @activity.lists = [@list]
+ context 'with raw data' do
+ setup do
+ @data = "Email Address,Custom Field1\nemail1@domain.com,custom1\nemail2@domain.com,custom2"
+ @list = ConstantContact::List.new
+ @list.stubs(:id).returns('http://api.constantcontact.com/ws/customers/freerobby/lists/3')
+ @activity = ConstantContact::Activity.new(
+ :activity_type => "SV_ADD"
+ )
+ @activity.raw_data = @data
+ @activity.lists = [@list]
+ end
+ should 'use raw data, bypassing contact parsing' do
+ assert_match(CGI.escape(@data), @activity.encode)
+ end
+ should 'include activity type' do
+ assert_match('activityType=SV_ADD', @activity.encode)
+ end
+ should 'include list' do
+ assert_match(CGI.escape(@list.id), @activity.encode)
+ end
end
-
- should 'include activity type' do
- assert_match(/activityType=SV_ADD/, @activity.encode)
- end
-
- should 'include contacts data' do
- assert_match(/\&data\=Email\+Address\%2CFirst\+Name\%2CLast\+Name\%0A/, @activity.encode)
- assert_match(/email0\%40gmail\.com\%2C\+Fred0\%2C\+Test0\%0A/, @activity.encode)
- end
-
- should 'include lists' do
- assert_match(/Test2\&lists\=http\%3A\%2F\%2Fapi\.constantcontact\.com\%2Fws\%2Fcustomers\%2Fjoesflowers\%2Flists\%2F2/, @activity.encode)
+ context 'with contact data' do
+ setup do
+ @contacts = []
+ 3.times do |n|
+ @contacts << ConstantContact::Contact.new(:first_name => "Fred#{n}",
+ :last_name => "Test#{n}",
+ :email_address => "email#{n}@gmail.com")
+ end
+ @activity = ConstantContact::Activity.new(:activity_type => "SV_ADD")
+ @activity.contacts = @contacts
+ @list = ConstantContact::List.new
+ @list.stubs(:id).returns('http://api.constantcontact.com/ws/customers/joesflowers/lists/2')
+ @activity.lists = [@list]
+ end
+
+ should 'include activity type' do
+ assert_match(/activityType=SV_ADD/, @activity.encode)
+ end
+
+ should 'include contacts data' do
+ assert_match(/\&data\=Email\+Address\%2CFirst\+Name\%2CLast\+Name\%0A/, @activity.encode)
+ assert_match(/email0\%40gmail\.com\%2C\+Fred0\%2C\+Test0\%0A/, @activity.encode)
+ end
+
+ should 'include lists' do
+ assert_match(/Test2\&lists\=http\%3A\%2F\%2Fapi\.constantcontact\.com\%2Fws\%2Fcustomers\%2Fjoesflowers\%2Flists\%2F2/, @activity.encode)
+ end
end
-
end
context 'format' do

0 comments on commit 4276233

Please sign in to comment.