Skip to content

Commit

Permalink
Refactoring. Added support for SOAP calls in List. DRYed up result ha…
Browse files Browse the repository at this point in the history
…ndling.
  • Loading branch information
patientslikeme committed Jan 6, 2009
1 parent 12ac63a commit e6f653c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 84 deletions.
42 changes: 23 additions & 19 deletions lib/campaign_monitor.rb
Expand Up @@ -68,12 +68,16 @@
end

class CampaignMonitor
include Helpers

attr_reader :api_key, :api_url

# Replace this API key with your own (http://www.campaignmonitor.com/api/)
def initialize(api_key=CAMPAIGN_MONITOR_API_KEY)
@api_key = api_key
@host = 'http://api.createsend.com'
@api = '/api/api.asmx/'
@api_url = 'http://api.createsend.com/api/api.asmx'
end


# Takes a CampaignMonitor API method name and set of parameters;
# returns an XmlSimple object with the response
Expand All @@ -87,11 +91,8 @@ def request(method, params)

# Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.
def request_url(method, params={})
url = "#{@host}#{@api}/#{method}?ApiKey=#{@api_key}"
params.each_pair do |key, val|
url += "&#{key}=" + CGI::escape(val.to_s)
end
url
params.merge!('ApiKey' => api_key)
"#{api_url}/#{method}?#{params.to_query}"
end

# Does an HTTP GET on a given URL and returns the response body
Expand All @@ -116,14 +117,22 @@ def method_missing(method_id, params = {})
# end
def clients
response = User_GetClients()
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(response) do
response["Client"].collect{|c| Client.new(c["ClientID"], c["Name"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

def system_date
response = User_GetSystemDate()
handle_response(response) do
response
end
end

def parsed_system_date
DateTime.strptime(system_date, timestamp_format)
end

# Returns an array of Campaign objects associated with the specified Client ID
#
# Example
Expand All @@ -135,11 +144,8 @@ def clients
# end
def campaigns(client_id)
response = Client_GetCampaigns("ClientID" => client_id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(response) do
response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand All @@ -154,11 +160,8 @@ def campaigns(client_id)
# end
def lists(client_id)
response = Client_GetLists("ClientID" => client_id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(response) do
response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand Down Expand Up @@ -217,6 +220,7 @@ def initialize(email_address, list_id)
@list_id = list_id
end
end

end

# If libxml is installed, we use the FasterXmlSimple library, that provides most of the functionality of XmlSimple
Expand Down
60 changes: 25 additions & 35 deletions lib/campaign_monitor/campaign.rb
@@ -1,7 +1,9 @@
class CampaignMonitor
# Provides access to the information about a campaign
class Campaign
attr_reader :id, :subject, :sent_date, :total_recipients
include Helpers

attr_reader :id, :subject, :sent_date, :total_recipients, :cm_client

def initialize(id=nil, subject=nil, sent_date=nil, total_recipients=nil)
@id = id
Expand All @@ -19,12 +21,8 @@ def initialize(id=nil, subject=nil, sent_date=nil, total_recipients=nil)
# puts subscriber.email
# end
def opens
response = @cm_client.Campaign_GetOpens("CampaignID" => @id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(cm_client.Campaign_GetOpens("CampaignID" => self.id)) do |response|
response["SubscriberOpen"].collect{|s| SubscriberOpen.new(s["EmailAddress"], s["ListID"], s["NumberOfOpens"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand All @@ -36,12 +34,8 @@ def opens
# puts subscriber.email
# end
def bounces
response = @cm_client.Campaign_GetBounces("CampaignID"=> @id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(cm_client.Campaign_GetBounces("CampaignID"=> self.id)) do |response|
response["SubscriberBounce"].collect{|s| SubscriberBounce.new(s["EmailAddress"], s["ListID"], s["BounceType"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand All @@ -53,12 +47,8 @@ def bounces
# puts subscriber.email
# end
def clicks
response = @cm_client.Campaign_GetSubscriberClicks("CampaignID" => @id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(cm_client.Campaign_GetSubscriberClicks("CampaignID" => self.id)) do |response|
response["SubscriberClick"].collect{|s| SubscriberClick.new(s["EmailAddress"], s["ListID"], s["ClickedLinks"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand All @@ -70,60 +60,60 @@ def clicks
# puts subscriber.email
# end
def unsubscribes
response = @cm_client.Campaign_GetUnsubscribes("CampaignID" => @id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(cm_client.Campaign_GetUnsubscribes("CampaignID" => self.id)) do |response|
response["SubscriberUnsubscribe"].collect{|s| SubscriberUnsubscribe.new(s["EmailAddress"], s["ListID"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

# Example
# @campaign = Campaign.new(12345)
# puts @campaign.number_recipients
def number_recipients
@number_recipients.nil? ? getInfo.number_recipients : @number_recipients
@number_recipients ||= attributes[:number_recipients]
end

# Example
# @campaign = Campaign.new(12345)
# puts @campaign.number_opened
def number_opened
@number_opened.nil? ? getInfo.number_opened : @number_opened
@number_opened ||= attributes[:number_opened]
end

# Example
# @campaign = Campaign.new(12345)
# puts @campaign.number_clicks
def number_clicks
@number_clicks.nil? ? getInfo.number_clicks : @number_clicks
@number_clicks ||= attributes[:number_clicks]
end

# Example
# @campaign = Campaign.new(12345)
# puts @campaign.number_unsubscribed
def number_unsubscribed
@number_unsubscribed.nil? ? getInfo.number_unsubscribed : @number_unsubscribed
@number_unsubscribed ||= attributes[:number_unsubscribed]
end

# Example
# @campaign = Campaign.new(12345)
# puts @campaign.number_bounced
def number_bounced
@number_bounced.nil? ? getInfo.number_bounced : @number_bounced
@number_bounced ||= attributes[:number_bounced]
end

private
def getInfo
info = @cm_client.Campaign_GetSummary('CampaignID'=>@id)
@title = info['title']
@number_recipients = info["Recipients"].to_i
@number_opened = info["TotalOpened"].to_i
@number_clicks = info["Click"].to_i
@number_unsubscribed = info["Unsubscribed"].to_i
@number_bounced = info["Bounced"].to_i
self
def attributes
if @attributes.nil?
summary = cm_client.Campaign_GetSummary('CampaignID' => self.id)
@attributes = {
:number_recipients => summary['Recipients'].to_i,
:number_opened => summary['TotalOpened'].to_i,
:number_clicks => summary['Click'].to_i,
:number_unsubscribed => summary['Unsubscribed'].to_i,
:number_bounced => summary['Bounced'].to_i
}
end

@attributes
end
end
end
13 changes: 4 additions & 9 deletions lib/campaign_monitor/client.rb
@@ -1,6 +1,8 @@
class CampaignMonitor
# Provides access to the lists and campaigns associated with a client
class Client
include Helpers

attr_reader :id, :name, :cm_client

# Example
Expand All @@ -19,12 +21,8 @@ def initialize(id, name=nil)
# puts list.name
# end
def lists
response = @cm_client.Client_GetLists("ClientID" => @id)
return [] if response.empty?
unless response["Code"].to_i != 0
handle_response(cm_client.Client_GetLists("ClientID" => self.id)) do |response|
response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
else
raise response["Code"] + " - " + response["Message"]
end
end

Expand All @@ -36,11 +34,8 @@ def lists
# puts campaign.subject
# end
def campaigns
response = @cm_client.Client_GetCampaigns("ClientID" => @id)
unless response["Code"].to_i != 0
handle_response(cm_client.Client_GetCampaigns("ClientID" => self.id)) do |response|
response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
else
raise response["Code"] + " - " + response["Message"]
end
end
end
Expand Down

0 comments on commit e6f653c

Please sign in to comment.