Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified to support multiple lists in a campaign #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/constant_contact.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require File.join(directory, 'constant_contact', 'member') require File.join(directory, 'constant_contact', 'member')
require File.join(directory, 'constant_contact', 'contact') require File.join(directory, 'constant_contact', 'contact')
require File.join(directory, 'constant_contact', 'campaign') require File.join(directory, 'constant_contact', 'campaign')
require File.join(directory, 'constant_contact', 'campaign_schedule')
require File.join(directory, 'constant_contact', 'campaign_event', 'campaign_event_base') require File.join(directory, 'constant_contact', 'campaign_event', 'campaign_event_base')
require File.join(directory, 'constant_contact', 'campaign_event', 'bounce_event') require File.join(directory, 'constant_contact', 'campaign_event', 'bounce_event')
require File.join(directory, 'constant_contact', 'campaign_event', 'forward_event') require File.join(directory, 'constant_contact', 'campaign_event', 'forward_event')
Expand Down
61 changes: 34 additions & 27 deletions lib/constant_contact/campaign.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class Campaign < Base
# SCHEDULED All campaigns that are currently scheduled to be sent some time in the future # SCHEDULED All campaigns that are currently scheduled to be sent some time in the future
# DRAFT All campaigns that have not yet been scheduled for delivery # DRAFT All campaigns that have not yet been scheduled for delivery
# RUNNING All campaigns that are currently being processed and delivered # RUNNING All campaigns that are currently being processed and delivered
@@column_names = [:archive_status, :archive_url, :bounces, :campaign_type, :clicks, :contact_lists, :date, @@column_names = [:archive_status, :archive_url, :bounces, :campaign_type, :clicks, :contact_lists, :date,
:email_content, :email_content_format, :email_text_content, :forward_email_link_text, :forwards, :email_content, :email_content_format, :email_text_content, :forward_email_link_text, :forwards,
:from_email, :from_name, :greeting_name, :greeting_salutation, :greeting_string, :from_email, :from_name, :greeting_name, :greeting_salutation, :greeting_string,
:include_forward_email, :include_subscribe_link, :last_edit_date, :name, :opens, :opt_outs, :include_forward_email, :include_subscribe_link, :last_edit_date, :name, :opens, :opt_outs,
:organization_address1, :organization_address2, :organization_address3, :organization_city, :organization_address1, :organization_address2, :organization_address3, :organization_city,
:organization_country, :organization_international_state, :organization_name, :organization_postal_code, :organization_country, :organization_international_state, :organization_name, :organization_postal_code,
:organization_state, :permission_reminder, :reply_to_email, :sent, :spam_reports, :status, :organization_state, :permission_reminder, :reply_to_email, :sent, :spam_reports, :status,
:style_sheet, :subject, :subscribe_link_text, :view_as_webpage, :view_as_webpage_link_text, :view_as_webpage_text] :style_sheet, :subject, :subscribe_link_text, :view_as_webpage, :view_as_webpage_link_text, :view_as_webpage_text]



# Setup defaults when creating a new object since # Setup defaults when creating a new object since
# CC requires so many extraneous fields to be present # CC requires so many extraneous fields to be present
Expand All @@ -24,33 +24,40 @@ def initialize(*args)
obj.set_defaults obj.set_defaults
obj obj
end end

def to_xml def to_xml
xml = Builder::XmlMarkup.new xml = Builder::XmlMarkup.new
xml.tag!("Campaign", :xmlns => "http://ws.constantcontact.com/ns/1.0/") do xml.tag!("Campaign", :xmlns => "http://ws.constantcontact.com/ns/1.0/") do
self.attributes.reject {|k,v| k == 'FromEmail' || k == 'ReplyToEmail' || k == 'ContactList'}.each{|k, v| xml.tag!( k.to_s.camelize, v )} self.attributes.reject {|k,v| k == 'FromEmail' || k == 'ReplyToEmail' || k == 'ContactList'}.each{|k, v| xml.tag!( k.to_s.camelize, v )}

# Overrides the default formatting above to CC's required format. # Overrides the default formatting above to CC's required format.
xml.tag!("ReplyToEmail") do xml.tag!("ReplyToEmail") do
xml.tag!('Email', :id => self.reply_to_email_url) xml.tag!('Email', :id => self.reply_to_email_url)
end end

xml.tag!("FromEmail") do xml.tag!("FromEmail") do
xml.tag!('Email', :id => self.from_email_url) xml.tag!('Email', :id => self.from_email_url)
end end
xml.tag!("ContactLists") do
xml.tag!("ContactList", :id => self.contact_list) xml.tag!("ContactLists") do
if self.contact_list.kind_of?(Array) then
self.contact_list.each {|x| xml.tag!("ContactList", :id => x)}
else
xml.tag!("ContactList", :id => self.contact_list)
end
end end
end end
end end

def from_email_url def from_email_url
EmailAddress.find(self.from_email).id EmailAddress.find(self.from_email).id
end end


def reply_to_email_url def reply_to_email_url
from_email_url from_email_url
end end


protected protected
def set_defaults def set_defaults
self.view_as_webpage = 'NO' unless attributes.has_key?('ViewAsWebpage') self.view_as_webpage = 'NO' unless attributes.has_key?('ViewAsWebpage')
Expand All @@ -76,31 +83,31 @@ def set_defaults
self.organization_country = 'US' unless attributes.has_key?('OrganizationCountry') self.organization_country = 'US' unless attributes.has_key?('OrganizationCountry')
self.organization_postal_code = '64108' unless attributes.has_key?('OrganizationPostalCode') self.organization_postal_code = '64108' unless attributes.has_key?('OrganizationPostalCode')
end end

# Formats data if present. # Formats data if present.
def before_save def before_save
self.email_text_content = "<Text>#{email_text_content}</Text>" unless email_text_content.match /^\<Text/ self.email_text_content = "<Text>#{email_text_content}</Text>" unless email_text_content.match /^\<Text/
self.date = self.date.strftime(DATE_FORMAT) if attributes.has_key?('Date') self.date = self.date.strftime(DATE_FORMAT) if attributes.has_key?('Date') && self.date.is_a?(Time)
end end


def validate def validate
# NOTE: Needs to be uppercase! # NOTE: Needs to be uppercase!
unless attributes.has_key?('EmailContentFormat') && ['HTML', 'XHTML'].include?(email_content_format) unless attributes.has_key?('EmailContentFormat') && ['HTML', 'XHTML'].include?(email_content_format)
errors.add(:email_content_format, 'must be either HTML or XHTML (the latter for advanced email features)') errors.add(:email_content_format, 'must be either HTML or XHTML (the latter for advanced email features)')
end end

if attributes.has_key?('ViewAsWebpage') && view_as_webpage.downcase == 'yes' if attributes.has_key?('ViewAsWebpage') && view_as_webpage.downcase == 'yes'
unless attributes['ViewAsWebpageLinkText'].present? && attributes['ViewAsWebpageText'].present? unless attributes['ViewAsWebpageLinkText'].present? && attributes['ViewAsWebpageText'].present?
errors.add(:view_as_webpage, "You need to set view_as_webpage_link_text and view_as_webpage_link if view_as_webpage is YES") errors.add(:view_as_webpage, "You need to set view_as_webpage_link_text and view_as_webpage_link if view_as_webpage is YES")
end end
end end


errors.add(:email_content, 'cannot be blank') unless attributes.has_key?('EmailContent') errors.add(:email_content, 'cannot be blank') unless attributes.has_key?('EmailContent')
errors.add(:email_text_content, 'cannot be blank') unless attributes.has_key?('EmailTextContent') errors.add(:email_text_content, 'cannot be blank') unless attributes.has_key?('EmailTextContent')
errors.add(:name, 'cannot be blank') unless attributes.has_key?('Name') errors.add(:name, 'cannot be blank') unless attributes.has_key?('Name')
errors.add(:subject, 'cannot be blank') unless attributes.has_key?('Subject') errors.add(:subject, 'cannot be blank') unless attributes.has_key?('Subject')
end end

end end
end end
56 changes: 56 additions & 0 deletions lib/constant_contact/campaign_schedule.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
module ConstantContact
class CampaignSchedule < Base
DATE_FORMAT = "%Y-%m-%dT%H:%M:%SZ"

attr_accessor :campaign_id

self.prefix = "/campaigns/:campaign_id/"

def initialize(*args)
obj = super
obj
end

def self.collection_name
'schedules'
end

def to_xml
xml = Builder::XmlMarkup.new
xml.tag!('Schedule', :xmlns => 'http://ws.constantcontact.com/ns/1.0/', :id => schedule_url) do
self.attributes.reject {|k,v| k.to_s.camelize == 'CampaignId'}.each{|k, v| xml.tag!( k.to_s.camelize, v )}
end
end

def campaign_url
'http://api.constantcontact.com'
end

def campaign_path
'/ws/customers/' + self.class.user + '/campaigns/' + self.prefix_options[:campaign_id].to_s
end

def schedule_path
'/schedules/1'
end

def schedule_url
campaign_url + campaign_path + schedule_path
end

# Overridden from CTCT::Base
def encode
tn = Time.now.strftime(DATE_FORMAT)
"<entry xmlns=\"http://www.w3.org/2005/Atom\">
<link href=\"#{self.campaign_path}#{self.schedule_path}\" rel=\"edit\"/>
<id>#{self.schedule_url}</id>
<title type=\"text\">#{tn}</title>
<updated>#{tn}</updated>
<author><name>WHERE, Inc</name></author>
<content type=\"application/vnd.ctct+xml\">
#{self.to_xml}
</content>
</entry>"
end
end
end
4 changes: 2 additions & 2 deletions lib/constant_contact/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConstantContact module ConstantContact
VERSION = '1.4.0'.freeze VERSION = '1.5.1'.freeze
end end