patientslikeme-campaign_monitor 1.1 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -11,16 +11,8 @@ http://www.campaignmonitor.com
11
11
 
12
12
  Install
13
13
 
14
- * gem install campaign_monitor
15
-
16
- Home Page
17
-
18
- * http://github.com/jordanbrock/campaign-monitor-ruby/wikis
19
-
20
- Bugtracking
21
-
22
- * http://jordanbrock.lighthouseapp.com/projects/13212/home
14
+ * gem install patientslikeme-campaign_monitor
23
15
 
24
16
  Git Repository
25
17
 
26
- * http://github.com/jordanbrock/campaign-monitor-ruby
18
+ * http://github.com/patientslikeme/campaign_monitor
@@ -1,10 +1,10 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.name = 'campaign_monitor'
4
- s.version = "1.1"
4
+ s.version = "1.2"
5
5
  s.summary = 'Provides access to the Campaign Monitor API.'
6
6
  s.description = <<-EOF
7
- A simple wrapper class that provides basic access to the Campaign Monitor API
7
+ A simple wrapper class that provides basic access to the Campaign Monitor API.
8
8
  EOF
9
9
  s.author = 'Jeremy Weiskotten'
10
10
  s.email = 'jweiskotten@patientslikeme.com'
@@ -1,7 +1,9 @@
1
1
  class CampaignMonitor
2
2
  # Provides access to the information about a campaign
3
3
  class Campaign
4
- attr_reader :id, :subject, :sent_date, :total_recipients
4
+ include Helpers
5
+
6
+ attr_reader :id, :subject, :sent_date, :total_recipients, :cm_client
5
7
 
6
8
  def initialize(id=nil, subject=nil, sent_date=nil, total_recipients=nil)
7
9
  @id = id
@@ -19,12 +21,8 @@ class CampaignMonitor
19
21
  # puts subscriber.email
20
22
  # end
21
23
  def opens
22
- response = @cm_client.Campaign_GetOpens("CampaignID" => @id)
23
- return [] if response.empty?
24
- unless response["Code"].to_i != 0
24
+ handle_response(cm_client.Campaign_GetOpens("CampaignID" => self.id)) do |response|
25
25
  response["SubscriberOpen"].collect{|s| SubscriberOpen.new(s["EmailAddress"], s["ListID"], s["NumberOfOpens"])}
26
- else
27
- raise response["Code"] + " - " + response["Message"]
28
26
  end
29
27
  end
30
28
 
@@ -36,12 +34,8 @@ class CampaignMonitor
36
34
  # puts subscriber.email
37
35
  # end
38
36
  def bounces
39
- response = @cm_client.Campaign_GetBounces("CampaignID"=> @id)
40
- return [] if response.empty?
41
- unless response["Code"].to_i != 0
37
+ handle_response(cm_client.Campaign_GetBounces("CampaignID"=> self.id)) do |response|
42
38
  response["SubscriberBounce"].collect{|s| SubscriberBounce.new(s["EmailAddress"], s["ListID"], s["BounceType"])}
43
- else
44
- raise response["Code"] + " - " + response["Message"]
45
39
  end
46
40
  end
47
41
 
@@ -53,12 +47,8 @@ class CampaignMonitor
53
47
  # puts subscriber.email
54
48
  # end
55
49
  def clicks
56
- response = @cm_client.Campaign_GetSubscriberClicks("CampaignID" => @id)
57
- return [] if response.empty?
58
- unless response["Code"].to_i != 0
50
+ handle_response(cm_client.Campaign_GetSubscriberClicks("CampaignID" => self.id)) do |response|
59
51
  response["SubscriberClick"].collect{|s| SubscriberClick.new(s["EmailAddress"], s["ListID"], s["ClickedLinks"])}
60
- else
61
- raise response["Code"] + " - " + response["Message"]
62
52
  end
63
53
  end
64
54
 
@@ -70,12 +60,8 @@ class CampaignMonitor
70
60
  # puts subscriber.email
71
61
  # end
72
62
  def unsubscribes
73
- response = @cm_client.Campaign_GetUnsubscribes("CampaignID" => @id)
74
- return [] if response.empty?
75
- unless response["Code"].to_i != 0
63
+ handle_response(cm_client.Campaign_GetUnsubscribes("CampaignID" => self.id)) do |response|
76
64
  response["SubscriberUnsubscribe"].collect{|s| SubscriberUnsubscribe.new(s["EmailAddress"], s["ListID"])}
77
- else
78
- raise response["Code"] + " - " + response["Message"]
79
65
  end
80
66
  end
81
67
 
@@ -83,47 +69,51 @@ class CampaignMonitor
83
69
  # @campaign = Campaign.new(12345)
84
70
  # puts @campaign.number_recipients
85
71
  def number_recipients
86
- @number_recipients.nil? ? getInfo.number_recipients : @number_recipients
72
+ @number_recipients ||= attributes[:number_recipients]
87
73
  end
88
74
 
89
75
  # Example
90
76
  # @campaign = Campaign.new(12345)
91
77
  # puts @campaign.number_opened
92
78
  def number_opened
93
- @number_opened.nil? ? getInfo.number_opened : @number_opened
79
+ @number_opened ||= attributes[:number_opened]
94
80
  end
95
81
 
96
82
  # Example
97
83
  # @campaign = Campaign.new(12345)
98
84
  # puts @campaign.number_clicks
99
85
  def number_clicks
100
- @number_clicks.nil? ? getInfo.number_clicks : @number_clicks
86
+ @number_clicks ||= attributes[:number_clicks]
101
87
  end
102
88
 
103
89
  # Example
104
90
  # @campaign = Campaign.new(12345)
105
91
  # puts @campaign.number_unsubscribed
106
92
  def number_unsubscribed
107
- @number_unsubscribed.nil? ? getInfo.number_unsubscribed : @number_unsubscribed
93
+ @number_unsubscribed ||= attributes[:number_unsubscribed]
108
94
  end
109
95
 
110
96
  # Example
111
97
  # @campaign = Campaign.new(12345)
112
98
  # puts @campaign.number_bounced
113
99
  def number_bounced
114
- @number_bounced.nil? ? getInfo.number_bounced : @number_bounced
100
+ @number_bounced ||= attributes[:number_bounced]
115
101
  end
116
102
 
117
103
  private
118
- def getInfo
119
- info = @cm_client.Campaign_GetSummary('CampaignID'=>@id)
120
- @title = info['title']
121
- @number_recipients = info["Recipients"].to_i
122
- @number_opened = info["TotalOpened"].to_i
123
- @number_clicks = info["Click"].to_i
124
- @number_unsubscribed = info["Unsubscribed"].to_i
125
- @number_bounced = info["Bounced"].to_i
126
- self
104
+ def attributes
105
+ if @attributes.nil?
106
+ summary = cm_client.Campaign_GetSummary('CampaignID' => self.id)
107
+ @attributes = {
108
+ :number_recipients => summary['Recipients'].to_i,
109
+ :number_opened => summary['TotalOpened'].to_i,
110
+ :number_clicks => summary['Click'].to_i,
111
+ :number_unsubscribed => summary['Unsubscribed'].to_i,
112
+ :number_bounced => summary['Bounced'].to_i
113
+ }
114
+ end
115
+
116
+ @attributes
127
117
  end
128
118
  end
129
119
  end
@@ -1,6 +1,8 @@
1
1
  class CampaignMonitor
2
2
  # Provides access to the lists and campaigns associated with a client
3
3
  class Client
4
+ include Helpers
5
+
4
6
  attr_reader :id, :name, :cm_client
5
7
 
6
8
  # Example
@@ -19,12 +21,8 @@ class CampaignMonitor
19
21
  # puts list.name
20
22
  # end
21
23
  def lists
22
- response = @cm_client.Client_GetLists("ClientID" => @id)
23
- return [] if response.empty?
24
- unless response["Code"].to_i != 0
24
+ handle_response(cm_client.Client_GetLists("ClientID" => self.id)) do |response|
25
25
  response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
26
- else
27
- raise response["Code"] + " - " + response["Message"]
28
26
  end
29
27
  end
30
28
 
@@ -36,11 +34,8 @@ class CampaignMonitor
36
34
  # puts campaign.subject
37
35
  # end
38
36
  def campaigns
39
- response = @cm_client.Client_GetCampaigns("ClientID" => @id)
40
- unless response["Code"].to_i != 0
37
+ handle_response(cm_client.Client_GetCampaigns("ClientID" => self.id)) do |response|
41
38
  response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
42
- else
43
- raise response["Code"] + " - " + response["Message"]
44
39
  end
45
40
  end
46
41
  end
@@ -1,7 +1,11 @@
1
+ require 'soap/wsdlDriver'
2
+
1
3
  class CampaignMonitor
2
4
  # Provides access to the subscribers and info about subscribers
3
5
  # associated with a Mailing List
4
6
  class List
7
+ include Helpers
8
+
5
9
  attr_reader :id, :name, :cm_client
6
10
 
7
11
  # Example
@@ -19,8 +23,20 @@ class CampaignMonitor
19
23
  # if result.succeeded?
20
24
  # puts "Added Subscriber"
21
25
  # end
22
- def add_subscriber(email, name = nil)
23
- Result.new(@cm_client.Subscriber_Add("ListID" => @id, "Email" => email, "Name" => name))
26
+ def add_subscriber(email, name=nil, custom_fields=nil)
27
+ if custom_fields.nil?
28
+ Result.new(cm_client.Subscriber_Add("ListID" => self.id, "Email" => email, "Name" => name))
29
+ else
30
+ add_subscriber_with_custom_fields(email, name, custom_fields)
31
+ end
32
+ end
33
+
34
+ def add_and_resubscribe(email, name=nil, custom_fields=nil)
35
+ if custom_fields.nil?
36
+ Result.new(cm_client.Subscriber_AddAndResubscribe("ListID" => self.id, "Email" => email, "Name" => name))
37
+ else
38
+ add_and_resubscribe_with_custom_fields(email, name, custom_fields)
39
+ end
24
40
  end
25
41
 
26
42
  # Example
@@ -31,7 +47,39 @@ class CampaignMonitor
31
47
  # puts "Deleted Subscriber"
32
48
  # end
33
49
  def remove_subscriber(email)
34
- Result.new(@cm_client.Subscriber_Unsubscribe("ListID" => @id, "Email" => email))
50
+ Result.new(cm_client.Subscriber_Unsubscribe("ListID" => self.id, "Email" => email))
51
+ end
52
+
53
+ # email The subscriber's email address.
54
+ # name The subscriber's name.
55
+ # custom_fields A hash of field name => value pairs.
56
+ def add_subscriber_with_custom_fields(email, name, custom_fields)
57
+ response = using_soap do |driver|
58
+ driver.addSubscriberWithCustomFields \
59
+ :ApiKey => api_key,
60
+ :ListID => self.id,
61
+ :Email => email,
62
+ :Name => name,
63
+ :CustomFields => { :SubscriberCustomField => custom_fields_array(custom_fields) }
64
+ end
65
+
66
+ response.subscriber_AddWithCustomFieldsResult
67
+ end
68
+
69
+ # email The subscriber's email address.
70
+ # name The subscriber's name.
71
+ # custom_fields A hash of field name => value pairs.
72
+ def add_and_resubscribe_with_custom_fields(email, name, custom_fields)
73
+ response = using_soap do |driver|
74
+ driver.addAndResubscribeWithCustomFields \
75
+ :ApiKey => api_key,
76
+ :ListID => self.id,
77
+ :Email => email,
78
+ :Name => name,
79
+ :CustomFields => { :SubscriberCustomField => custom_fields_array(custom_fields) }
80
+ end
81
+
82
+ response.subscriber_AddAndResubscribeWithCustomFieldsResult
35
83
  end
36
84
 
37
85
  # Example
@@ -43,12 +91,9 @@ class CampaignMonitor
43
91
  # puts subscriber.email
44
92
  # end
45
93
  def active_subscribers(date)
46
- response = @cm_client.Subscribers_GetActive('ListID' => @id, "Date" => date.strftime("%Y-%m-%d %H:%M:%S"))
47
- return [] if response.empty?
48
- unless response["Code"].to_i != 0
49
- response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
50
- else
51
- raise response["Code"] + " - " + response["Message"]
94
+ response = cm_client.Subscribers_GetActive('ListID' => self.id, 'Date' => formatted_timestamp(date))
95
+ handle_response(response) do
96
+ response['Subscriber'].collect{|s| Subscriber.new(s['EmailAddress'], s['Name'], s['Date'])}
52
97
  end
53
98
  end
54
99
 
@@ -61,12 +106,12 @@ class CampaignMonitor
61
106
  # puts subscriber.email
62
107
  # end
63
108
  def unsubscribed(date)
64
- response = @cm_client.Subscribers_GetUnsubscribed('ListID' => @id, 'Date' => date.strftime("%Y-%m-%d %H:%M:%S"))
65
- return [] if response.empty?
66
- unless response["Code"].to_i != 0
67
- response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
68
- else
69
- raise response["Code"] + " - " + response["Message"]
109
+ date = formatted_timestamp(date) unless date.is_a?(String)
110
+
111
+ response = cm_client.Subscribers_GetUnsubscribed('ListID' => self.id, 'Date' => date)
112
+
113
+ handle_response(response) do
114
+ response['Subscriber'].collect{|s| Subscriber.new(s['EmailAddress'], s['Name'], s['Date'])}
70
115
  end
71
116
  end
72
117
 
@@ -79,14 +124,24 @@ class CampaignMonitor
79
124
  # puts subscriber.email
80
125
  # end
81
126
  def bounced(date)
82
- response = @cm_client.Subscribers_GetBounced('ListID' => @id, 'Date' => date.strftime("%Y-%m-%d %H:%M:%S"))
83
- return [] if response.empty?
84
- unless response["Code"].to_i != 0
85
- response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
86
- else
87
- raise response["Code"] + " - " + response["Message"]
127
+ response = cm_client.Subscribers_GetBounced('ListID' => self.id, 'Date' => formatted_timestamp(date))
128
+
129
+ handle_response(response) do
130
+ response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
88
131
  end
89
132
  end
133
+
134
+
135
+ protected
136
+
137
+ # Converts hash of custom field name/values to array of hashes for the SOAP API.
138
+ def custom_fields_array(custom_fields)
139
+ arr = []
140
+ custom_fields.each do |key, value|
141
+ arr << { "Key" => key, "Value" => value }
142
+ end
143
+ arr
144
+ end
90
145
 
91
146
  end
92
147
  end
@@ -1,6 +1,8 @@
1
1
  class CampaignMonitor
2
2
  # Provides the ability to add/remove subscribers from a list
3
3
  class Subscriber
4
+ include Helpers
5
+
4
6
  attr_accessor :email_address, :name, :date_subscribed
5
7
 
6
8
  def initialize(email_address, name=nil, date=nil)
@@ -68,12 +68,16 @@ Dir[File.join(File.dirname(__FILE__), 'campaign_monitor/*.rb')].each do |f|
68
68
  end
69
69
 
70
70
  class CampaignMonitor
71
+ include Helpers
72
+
73
+ attr_reader :api_key, :api_url
74
+
71
75
  # Replace this API key with your own (http://www.campaignmonitor.com/api/)
72
76
  def initialize(api_key=CAMPAIGN_MONITOR_API_KEY)
73
77
  @api_key = api_key
74
- @host = 'http://api.createsend.com'
75
- @api = '/api/api.asmx/'
78
+ @api_url = 'http://api.createsend.com/api/api.asmx'
76
79
  end
80
+
77
81
 
78
82
  # Takes a CampaignMonitor API method name and set of parameters;
79
83
  # returns an XmlSimple object with the response
@@ -87,11 +91,8 @@ class CampaignMonitor
87
91
 
88
92
  # Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.
89
93
  def request_url(method, params={})
90
- url = "#{@host}#{@api}/#{method}?ApiKey=#{@api_key}"
91
- params.each_pair do |key, val|
92
- url += "&#{key}=" + CGI::escape(val.to_s)
93
- end
94
- url
94
+ params.merge!('ApiKey' => api_key)
95
+ "#{api_url}/#{method}?#{params.to_query}"
95
96
  end
96
97
 
97
98
  # Does an HTTP GET on a given URL and returns the response body
@@ -116,14 +117,22 @@ class CampaignMonitor
116
117
  # end
117
118
  def clients
118
119
  response = User_GetClients()
119
- return [] if response.empty?
120
- unless response["Code"].to_i != 0
120
+ handle_response(response) do
121
121
  response["Client"].collect{|c| Client.new(c["ClientID"], c["Name"])}
122
- else
123
- raise response["Code"] + " - " + response["Message"]
122
+ end
123
+ end
124
+
125
+ def system_date
126
+ response = User_GetSystemDate()
127
+ handle_response(response) do
128
+ response
124
129
  end
125
130
  end
126
131
 
132
+ def parsed_system_date
133
+ DateTime.strptime(system_date, timestamp_format)
134
+ end
135
+
127
136
  # Returns an array of Campaign objects associated with the specified Client ID
128
137
  #
129
138
  # Example
@@ -135,11 +144,8 @@ class CampaignMonitor
135
144
  # end
136
145
  def campaigns(client_id)
137
146
  response = Client_GetCampaigns("ClientID" => client_id)
138
- return [] if response.empty?
139
- unless response["Code"].to_i != 0
147
+ handle_response(response) do
140
148
  response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
141
- else
142
- raise response["Code"] + " - " + response["Message"]
143
149
  end
144
150
  end
145
151
 
@@ -154,11 +160,8 @@ class CampaignMonitor
154
160
  # end
155
161
  def lists(client_id)
156
162
  response = Client_GetLists("ClientID" => client_id)
157
- return [] if response.empty?
158
- unless response["Code"].to_i != 0
163
+ handle_response(response) do
159
164
  response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
160
- else
161
- raise response["Code"] + " - " + response["Message"]
162
165
  end
163
166
  end
164
167
 
@@ -217,6 +220,7 @@ class CampaignMonitor
217
220
  @list_id = list_id
218
221
  end
219
222
  end
223
+
220
224
  end
221
225
 
222
226
  # If libxml is installed, we use the FasterXmlSimple library, that provides most of the functionality of XmlSimple
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patientslikeme-campaign_monitor
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.1"
4
+ version: "1.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Weiskotten
@@ -13,7 +13,7 @@ date: 2008-12-22 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: A simple wrapper class that provides basic access to the Campaign Monitor API
16
+ description: A simple wrapper class that provides basic access to the Campaign Monitor API.
17
17
  email: jweiskotten@patientslikeme.com
18
18
  executables: []
19
19