Class: CampaignMonitor::Campaign
- Includes:
- Helpers
- Defined in:
- lib/campaign_monitor/campaign.rb
Overview
The Client class aims to impliment the full functionality of the CampaignMonitor Clients API as detailed at: www.campaignmonitor.com/api/
Attributes
Attriutes can be read and set as if Campaign were a Hash
@client["CompanyName"]="Road Running, Inc."
@client["ContactName"] => "Wiley Coyote"
Convenience attribute readers are provided for name and id
@campaign.id == @client["CampaignID"]
@campaign.name == @client["CampaignName"]
API calls supported
-
Campaign.Create
-
Campaign.Send
-
Campaign.GetBounces
-
Campaign.GetLists
-
Campaign.GetOpens
-
Campaign.GetSubscriberClicks
-
Campaign.GetUnsubscribes
-
Campaign.GetSummary
Defined Under Namespace
Classes: MissingParameter
Instance Attribute Summary
Attributes inherited from Base
#attributes, #cm_client, #result
Class Method Summary collapse
-
.[](id) ⇒ Object
Creates a new list object with the given id.
Instance Method Summary collapse
-
#Create ⇒ Object
Calls Campaign.Create It will return true if successful and false if not.
-
#GetBounces ⇒ Object
(also: #bounces)
Example @campaign = Campaign @subscriber_bounces = @campaign.bounces.
-
#GetLists ⇒ Object
Calls Campaign.GetLists.
-
#GetOpens ⇒ Object
(also: #opens)
Example @campaign = Campaign @subscriber_opens = @campaign.opens.
-
#GetSubscriberClicks ⇒ Object
(also: #clicks)
Example @campaign = Campaign @subscriber_clicks = @campaign.clicks.
-
#GetSummary ⇒ Object
Calls Campaign.GetSummary.
-
#GetUnsubscribes ⇒ Object
(also: #unsubscribes)
Example @campaign = Campaign @subscriber_unsubscribes = @campaign.unsubscribes.
-
#initialize(attrs = {}) ⇒ Campaign
constructor
A new instance of Campaign.
-
#lists ⇒ Object
Convenience method for accessing or adding lists to a new (uncreated) campaign Calls GetLists behind the scenes if needed.
-
#method_missing(m, *args) ⇒ Object
hook up the old API calls.
-
#Send(options = {}) ⇒ Object
Calls Campaign.Send It will return true if successful and false if not.
-
#summary(refresh = false) ⇒ Object
Convenience method for accessing summary details of a campaign.
Methods included from Helpers
#formatted_timestamp, #handle_response, #timestamp_format
Methods inherited from Base
#[], #[]=, client, client=, #id, #id=, #name
Constructor Details
#initialize(attrs = {}) ⇒ Campaign
Returns a new instance of Campaign.
38 39 40 41 |
# File 'lib/campaign_monitor/campaign.rb', line 38 def initialize(attrs={}) super @attributes=attrs end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
hook up the old API calls
188 189 190 191 192 193 194 |
# File 'lib/campaign_monitor/campaign.rb', line 188 def method_missing(m, *args) if %w{number_bounced number_unsubscribed number_clicks number_opened number_recipients}.include?(m.to_s) summary[m] else super end end |
Class Method Details
.[](id) ⇒ Object
Creates a new list object with the given id. You’ll still need to call another method to load data or actually do anything useful as this method just generators a new object and doesn’t hit the API at all. This was added as a quick way to setup an object to request data from it
Example
@campaign=Campaign[1234]
@campaign.lists.each do ...
111 112 113 |
# File 'lib/campaign_monitor/campaign.rb', line 111 def self.[](id) Campaign.new("CampaignID" => id) end |
Instance Method Details
#Create ⇒ Object
Calls Campaign.Create It will return true if successful and false if not. Campaign#result will have the result of the API call
Example
@camp=@client.new_campaign
@camp["CampaignName"]="Yummy Gummy Bears"
@camp["CampaignSubject"]="Yummy Gummy Bears"
@camp["FromName"]="Mr Yummy"
@camp["FromEmail"]="[email protected]"
@camp["ReplyTo"]="[email protected]"
@camp["HtmlUrl"]="http://www.gummybears.com/newsletter2009.html"
@camp["TextUrl"]="http://www.gummybears.com/newsletter2009.txt"
@camp.Create
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/campaign_monitor/campaign.rb', line 57 def Create required_params=%w{CampaignName CampaignSubject FromName FromEmail ReplyTo HtmlUrl TextUrl} required_params.each do |f| raise MissingParameter, "'#{f}' is required to call Create" unless self[f] end response = cm_client.using_soap do |driver| opts=attributes.merge(:ApiKey => cm_client.api_key, :SubscriberListIDs => @lists.map {|x| x.id}) driver.createCampaign opts end @result=Result.new(response["Campaign.CreateResult"]) self.id=@result.content if @result.success? @result.success? end |
#GetBounces ⇒ Object Also known as: bounces
Example
@campaign = Campaign[12345]
@subscriber_bounces = @campaign.bounces
for subscriber in @subscriber_bounces
puts subscriber.email
end
152 153 154 155 156 |
# File 'lib/campaign_monitor/campaign.rb', line 152 def GetBounces handle_response(cm_client.Campaign_GetBounces("CampaignID"=> self.id)) do |response| response["SubscriberBounce"].collect{|s| SubscriberBounce.new(s["EmailAddress"], s["ListID"], s["BounceType"])} end end |
#GetLists ⇒ Object
Calls Campaign.GetLists. Often you probably should just use Campaign#lists It will raise an ApiError if an error occurs Campaign#result will have the result of the API call
Example
@camp=@client.campaigns.first
@camp.GetLists
96 97 98 99 100 101 |
# File 'lib/campaign_monitor/campaign.rb', line 96 def GetLists handle_response(@cm_client.Campaign_GetLists(:CampaignID => id)) do |response| @result=Result.new(response) @lists=response["List"].collect{|l| List.new({"ListID" => l["ListID"], "Title" => l["Name"]})} end end |
#GetOpens ⇒ Object Also known as: opens
Example
@campaign = Campaign[12345]
@subscriber_opens = @campaign.opens
for subscriber in @subscriber_opens
puts subscriber.email
end
138 139 140 141 142 |
# File 'lib/campaign_monitor/campaign.rb', line 138 def GetOpens handle_response(cm_client.Campaign_GetOpens("CampaignID" => self.id)) do |response| response["SubscriberOpen"].collect{|s| SubscriberOpen.new(s["EmailAddress"], s["ListID"], s["NumberOfOpens"])} end end |
#GetSubscriberClicks ⇒ Object Also known as: clicks
Example
@campaign = Campaign[12345]
@subscriber_clicks = @campaign.clicks
for subscriber in @subscriber_clicks
puts subscriber.email
end
166 167 168 169 170 |
# File 'lib/campaign_monitor/campaign.rb', line 166 def GetSubscriberClicks handle_response(cm_client.Campaign_GetSubscriberClicks("CampaignID" => self.id)) do |response| response["SubscriberClick"].collect{|s| SubscriberClick.new(s["EmailAddress"], s["ListID"], s["ClickedLinks"])} end end |
#GetSummary ⇒ Object
Calls Campaign.GetSummary. OYou probably should just use Campaign#summary which caches results It will raise ApiError if an error occurs Campaign#result will have the result of the API call
Example
@camp=@client.campaigns.first
@camp.GetSummary["Clicks"]
203 204 205 206 207 208 209 |
# File 'lib/campaign_monitor/campaign.rb', line 203 def GetSummary handle_response(cm_client.Campaign_GetSummary('CampaignID' => self.id)) do |response| @result=Result.new(response) @summary=parse_summary(@result.raw) end @summary end |
#GetUnsubscribes ⇒ Object Also known as: unsubscribes
Example
@campaign = Campaign[12345]
@subscriber_unsubscribes = @campaign.unsubscribes
for subscriber in @subscriber_unsubscribes
puts subscriber.email
end
180 181 182 183 184 |
# File 'lib/campaign_monitor/campaign.rb', line 180 def GetUnsubscribes handle_response(cm_client.Campaign_GetUnsubscribes("CampaignID" => self.id)) do |response| response["SubscriberUnsubscribe"].collect{|s| SubscriberUnsubscribe.new(s["EmailAddress"], s["ListID"])} end end |
#lists ⇒ Object
Convenience method for accessing or adding lists to a new (uncreated) campaign Calls GetLists behind the scenes if needed
Example
@[email protected]
@camp.lists.each do
@[email protected]_campaign(attributes)
@camp.lists << @client.lists.first
@camp.Create
125 126 127 128 129 |
# File 'lib/campaign_monitor/campaign.rb', line 125 def lists # pull down the list of lists if we have an id self.GetLists if @lists.nil? and id @lists||=[] end |
#Send(options = {}) ⇒ Object
Calls Campaign.Send It will return true if successful and false if not. Campaign#result will have the result of the API call
Example
@camp=@client.new_campaign(attributes)
@camp.Create
@camp.Send("ConfirmationEmail" => "[email protected]", "SendDate" => "Immediately")
79 80 81 82 83 84 85 86 87 |
# File 'lib/campaign_monitor/campaign.rb', line 79 def Send(={}) required_params=%w{ConfirmationEmail SendDate} required_params.each do |f| raise MissingParameter, "'#{f}' is required to call Send" unless [f] end .merge!("CampaignID" => self.id) @result=Result.new(@cm_client.Campaign_Send()) @result.success? end |
#summary(refresh = false) ⇒ Object
Convenience method for accessing summary details of a campaign
Examples
@camp.summary["Recipients"]
@camp.summary['Recipients']
@camp.summary['TotalOpened']
@camp.summary['Clicks']
@camp.summary['Unsubscribed']
@camp.summary['Bounced']
220 221 222 223 |
# File 'lib/campaign_monitor/campaign.rb', line 220 def summary(refresh=false) self.GetSummary if refresh or @summary.nil? @summary end |