sevenwire-campaign_monitor 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/init.rb +1 -0
  2. data/install.rb +0 -0
  3. data/lib/campaign_monitor.rb +511 -0
  4. metadata +55 -0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'campaign_monitor'
File without changes
@@ -0,0 +1,511 @@
1
+ # CampaignMonitor
2
+ # A wrapper class to access the Campaign Monitor API. Written using the wonderful
3
+ # Flickr interface by Scott Raymond as a guide on how to access remote web services
4
+ #
5
+ # For more information on the Campaign Monitor API, visit http://campaignmonitor.com/api
6
+ #
7
+ # Author:: Jordan Brock <jordan@spintech.com.au>
8
+ # Copyright:: Copyright (c) 2006 Jordan Brock <jordan@spintech.com.au>
9
+ # License:: MIT <http://www.opensource.org/licenses/mit-license.php>
10
+ #
11
+ # USAGE:
12
+ # require 'campaign_monitor'
13
+ # cm = CampaignMonitor.new(API_KEY) # creates a CampaignMonitor object
14
+ # # Can set CAMPAIGN_MONITOR_API_KEY in environment.rb
15
+ # cm.clients # Returns an array of clients associated with
16
+ # # the user account
17
+ # cm.campaigns(client_id)
18
+ # cm.lists(client_id)
19
+ # cm.add_subscriber(list_id, email, name)
20
+ #
21
+ # CLIENT
22
+ # client = Client.new(client_id)
23
+ # client.lists
24
+ # client.campaigns
25
+ #
26
+ # LIST
27
+ # list = List.new(list_id)
28
+ # list.add_subscriber(email, name)
29
+ # list.remove_subscriber(email)
30
+ # list.active_subscribers(date)
31
+ # list.unsubscribed(date)
32
+ # list.bounced(date)
33
+ #
34
+ # CAMPAIGN
35
+ # campaign = Campaign.new(campaign_id)
36
+ # campaign.clicks
37
+ # campaign.opens
38
+ # campaign.bounces
39
+ # campaign.unsubscribes
40
+ # campaign.number_recipients
41
+ # campaign.number_clicks
42
+ # campaign.number_opens
43
+ # campaign.number_bounces
44
+ # campaign.number_unsubscribes
45
+ #
46
+ #
47
+ # SUBSCRIBER
48
+ # subscriber = Subscriber.new(email)
49
+ # subscriber.add(list_id)
50
+ # subscriber.unsubscribe(list_id)
51
+ #
52
+ # Data Types
53
+ # SubscriberBounce
54
+ # SubscriberClick
55
+ # SubscriberOpen
56
+ # SubscriberUnsubscribe
57
+ # Result
58
+ #
59
+
60
+ require 'cgi'
61
+ require 'net/http'
62
+
63
+ class CampaignMonitor
64
+ # Replace this API key with your own (http://www.campaignmonitor.com/api/)
65
+ def initialize(api_key=CAMPAIGN_MONITOR_API_KEY)
66
+ @api_key = api_key
67
+ @host = 'http://app.campaignmonitor.com'
68
+ @api = '/api/api.asmx/'
69
+ end
70
+
71
+ # Takes a CampaignMonitor API method name and set of parameters;
72
+ # returns an XmlSimple object with the response
73
+ def request(method, *params)
74
+ response = XmlSimple.xml_in(http_get(request_url(method, params)), { 'ForceArray' => false, 'ForceArray' => %r(List$|Campaign$|Subscriber$|Client$|SubscriberOpen$|SubscriberUnsubscribe$|SubscriberClick$|SubscriberBounce$), 'NoAttr' => true })
75
+ response
76
+ end
77
+
78
+ # Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.
79
+ def request_url(method, *params)
80
+ url = "#{@host}#{@api}/#{method}?ApiKey=#{@api_key}"
81
+ params[0][0].each_key do |key| url += "&#{key}=" + CGI::escape(params[0][0][key].to_s) end if params[0][0]
82
+ url
83
+ end
84
+
85
+ # Does an HTTP GET on a given URL and returns the response body
86
+ def http_get(url)
87
+ Net::HTTP.get_response(URI.parse(url)).body.to_s
88
+ end
89
+
90
+ # By overriding the method_missing method, it is possible to easily support all of the methods
91
+ # available in the API
92
+ def method_missing(method_id, *params)
93
+ request(method_id.id2name.gsub(/_/, '.'), params[0])
94
+ end
95
+
96
+ # Returns an array of Client objects associated with the API Key
97
+ #
98
+ # Example
99
+ # @cm = CampaignMonitor.new()
100
+ # @clients = @cm.clients
101
+ #
102
+ # for client in @clients
103
+ # puts client.name
104
+ # end
105
+ def clients
106
+ response = User_GetClients()
107
+ unless response["Code"].to_i != 0
108
+ response["Client"].collect{|c| Client.new(c["ClientID"].to_i, c["Name"])}
109
+ else
110
+ raise response["Code"] + " - " + response["Message"]
111
+ end
112
+ end
113
+
114
+ # Returns an array of Campaign objects associated with the specified Client ID
115
+ #
116
+ # Example
117
+ # @cm = CampaignMonitor.new()
118
+ # @campaigns = @cm.campaigns(12345)
119
+ #
120
+ # for campaign in @campaigns
121
+ # puts campaign.subject
122
+ # end
123
+ def campaigns(client_id)
124
+ response = Client_GetCampaigns("ClientID" => client_id)
125
+ unless response["Code"].to_i != 0
126
+ response["Campaign"].collect{|c| Campaign.new(c["CampaignID"].to_i, c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
127
+ else
128
+ raise response["Code"] + " - " + response["Message"]
129
+ end
130
+ end
131
+
132
+ # Returns an array of Subscriber Lists for the specified Client ID
133
+ #
134
+ # Example
135
+ # @cm = CampaignMonitor.new()
136
+ # @lists = @cm.lists(12345)
137
+ #
138
+ # for list in @lists
139
+ # puts list.name
140
+ # end
141
+ def lists(client_id)
142
+ response = Client_GetLists("ClientID" => client_id)
143
+ unless response["Code"].to_i != 0
144
+ response["List"].collect{|l| List.new(l["ListID"].to_i, l["Name"])}
145
+ else
146
+ raise response["Code"] + " - " + response["Message"]
147
+ end
148
+ end
149
+
150
+ # A quick method of adding a subscriber to a list. Returns a Result object
151
+ #
152
+ # Example
153
+ # @cm = CampaignMonitor.new()
154
+ # result = @cm.add_subscriber(12345, "ralph.wiggum@simpsons.net", "Ralph Wiggum")
155
+ #
156
+ # if result.code == 0
157
+ # puts "Subscriber Added to List"
158
+ # end
159
+ def add_subscriber(list_id, email, name)
160
+ response = Subscriber_Add("ListID" => list_id, "Email" => email, "Name" => name)
161
+ Result.new(response["Message"], response["Code"].to_i)
162
+ end
163
+
164
+ # Provides access to the lists and campaigns associated with a client
165
+ class Client
166
+ attr_reader :id, :name, :cm_client
167
+
168
+ # Example
169
+ # @client = new Client(12345)
170
+ def initialize(id, name=nil)
171
+ @id = id
172
+ @name = name
173
+ @cm_client = CampaignMonitor.new
174
+ end
175
+
176
+ # Example
177
+ # @client = new Client(12345)
178
+ # @lists = @client.lists
179
+ #
180
+ # for list in @lists
181
+ # puts list.name
182
+ # end
183
+ def lists
184
+ response = @cm_client.Client_GetLists("ClientID" => @id)
185
+ unless response["Code"].to_i != 0
186
+ response["List"].collect{|l| List.new(l["ListID"].to_i, l["Name"])}
187
+ else
188
+ raise response["Code"] + " - " + response["Message"]
189
+ end
190
+ end
191
+
192
+ # Example
193
+ # @client = new Client(12345)
194
+ # @campaigns = @client.campaigns
195
+ #
196
+ # for campaign in @campaigns
197
+ # puts campaign.subject
198
+ # end
199
+ def campaigns
200
+ response = @cm_client.Client_GetCampaigns("ClientID" => @id)
201
+ unless response["Code"].to_i != 0
202
+ response["Campaign"].collect{|c| Campaign.new(c["CampaignID"].to_i, c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
203
+ else
204
+ raise response["Code"] + " - " + response["Message"]
205
+ end
206
+ end
207
+ end
208
+
209
+ # Provides access to the subscribers and info about subscribers
210
+ # associated with a Mailing List
211
+ class List
212
+ attr_reader :id, :name, :cm_client
213
+
214
+ # Example
215
+ # @list = new List(12345)
216
+ def initialize(id=nil, name=nil)
217
+ @id = id
218
+ @name = name
219
+ @cm_client = CampaignMonitor.new
220
+ end
221
+
222
+ # Example
223
+ # @list = new List(12345)
224
+ # result = @list.add_subscriber("ralph.wiggum@simpsons.net")
225
+ #
226
+ # if result.code == 0
227
+ # puts "Added Subscriber"
228
+ # end
229
+ def add_subscriber(email, name = nil)
230
+ response = @cm_client.Subscriber_Add("ListID" => @id, "Email" => email, "Name" => name)
231
+ Result.new(response["Message"], response["Code"].to_i)
232
+ end
233
+
234
+ # Example
235
+ # @list = new List(12345)
236
+ # result = @list.remove_subscriber("ralph.wiggum@simpsons.net")
237
+ #
238
+ # if result.code == 0
239
+ # puts "Deleted Subscriber"
240
+ # end
241
+ def remove_subscriber(email)
242
+ response = @cm_client.Subscriber_Unsubscribe("ListID" => @id, "Email" => email)
243
+ Result.new(response["Message"], response["Code"].to_i)
244
+ end
245
+
246
+ # Example
247
+ # current_date = DateTime.new
248
+ # @list = new List(12345)
249
+ # @subscribers = @list.active_subscribers(current_date)
250
+ #
251
+ # for subscriber in @subscribers
252
+ # puts subscriber.email
253
+ # end
254
+ def active_subscribers(date)
255
+ response = @cm_client.Subscribers_GetActive('ListID' => @id, "Date" => date.strftime("%Y-%m-%d %H:%M:%S"))
256
+ unless response["Code"].to_i != 0
257
+ response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
258
+ else
259
+ raise response["Code"] + " - " + response["Message"]
260
+ end
261
+ end
262
+
263
+ # Example
264
+ # current_date = DateTime.new
265
+ # @list = new List(12345)
266
+ # @subscribers = @list.unsubscribed(current_date)
267
+ #
268
+ # for subscriber in @subscribers
269
+ # puts subscriber.email
270
+ # end
271
+ def unsubscribed(date)
272
+ response = @cm_client.Subscribers_GetUnsubscribed('ListID' => @id, 'Date' => date.strftime("%Y-%m-%d %H:%M:%S"))
273
+ unless response["Code"].to_i != 0
274
+ response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
275
+ else
276
+ raise response["Code"] + " - " + response["Message"]
277
+ end
278
+ end
279
+
280
+ # Example
281
+ # current_date = DateTime.new
282
+ # @list = new List(12345)
283
+ # @subscribers = @list.bounced(current_date)
284
+ #
285
+ # for subscriber in @subscribers
286
+ # puts subscriber.email
287
+ # end
288
+ def bounced(date)
289
+ response = @cm_client.Subscribers_GetBounced('ListID' => @id, 'Date' => date.strftime("%Y-%m-%d %H:%M:%S"))
290
+ unless response["Code"].to_i != 0
291
+ response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
292
+ else
293
+ raise response["Code"] + " - " + response["Message"]
294
+ end
295
+ end
296
+
297
+ end
298
+
299
+ # Provides access to the information about a campaign
300
+ class Campaign
301
+ attr_reader :id, :subject, :sent_date, :total_recipients
302
+
303
+ def initialize(id=nil, subject=nil, sent_date=nil, total_recipients=nil)
304
+ @id = id
305
+ @subject = subject
306
+ @sent_date = sent_date
307
+ @total_recipients = total_recipients
308
+ @cm_client = CampaignMonitor.new
309
+ end
310
+
311
+ # Example
312
+ # @campaign = Campaign.new(12345)
313
+ # @subscriber_opens = @campaign.opens
314
+ #
315
+ # for subscriber in @subscriber_opens
316
+ # puts subscriber.email
317
+ # end
318
+ def opens
319
+ response = @cm_client.Campaign_GetOpens("CampaignID" => @id)
320
+ unless response["Code"].to_i != 0
321
+ response["SubscriberOpen"].collect{|s| SubscriberOpen.new(s["EmailAddress"], s["ListID"].to_i, s["NumberOfOpens"])}
322
+ else
323
+ raise response["Code"] + " - " + response["Message"]
324
+ end
325
+ end
326
+
327
+ # Example
328
+ # @campaign = Campaign.new(12345)
329
+ # @subscriber_bounces = @campaign.bounces
330
+ #
331
+ # for subscriber in @subscriber_bounces
332
+ # puts subscriber.email
333
+ # end
334
+ def bounces
335
+ response = @cm_client.Campaign_GetBounces("CampaignID"=> @id)
336
+ unless response["Code"].to_i != 0
337
+ response["SubscriberBounce"].collect{|s| SubscriberBounce.new(s["EmailAddress"], s["ListID"].to_i, s["BounceType"])}
338
+ else
339
+ raise response["Code"] + " - " + response["Message"]
340
+ end
341
+ end
342
+
343
+ # Example
344
+ # @campaign = Campaign.new(12345)
345
+ # @subscriber_clicks = @campaign.clicks
346
+ #
347
+ # for subscriber in @subscriber_clicks
348
+ # puts subscriber.email
349
+ # end
350
+ def clicks
351
+ response = @cm_client.Campaign_GetSubscriberClicks("CampaignID" => @id)
352
+ unless response["Code"].to_i != 0
353
+ response["SubscriberClick"].collect{|s| SubscriberClick.new(s["EmailAddress"], s["ListID"].to_i, s["ClickedLinks"])}
354
+ else
355
+ raise response["Code"] + " - " + response["Message"]
356
+ end
357
+ end
358
+
359
+ # Example
360
+ # @campaign = Campaign.new(12345)
361
+ # @subscriber_unsubscribes = @campaign.unsubscribes
362
+ #
363
+ # for subscriber in @subscriber_unsubscribes
364
+ # puts subscriber.email
365
+ # end
366
+ def unsubscribes
367
+ response = @cm_client.Campaign_GetUnsubscribes("CampaignID" => @id)
368
+ unless response["Code"].to_i != 0
369
+ response["SubscriberUnsubscribe"].collect{|s| SubscriberUnsubscribe.new(s["EmailAddress"], s["ListID"].to_i)}
370
+ else
371
+ raise response["Code"] + " - " + response["Message"]
372
+ end
373
+ end
374
+
375
+ # Example
376
+ # @campaign = Campaign.new(12345)
377
+ # puts @campaign.number_recipients
378
+ def number_recipients
379
+ @number_recipients.nil? ? getInfo.number_recipients : @number_recipients
380
+ end
381
+
382
+ # Example
383
+ # @campaign = Campaign.new(12345)
384
+ # puts @campaign.number_opened
385
+ def number_opened
386
+ @number_opened.nil? ? getInfo.number_opened : @number_opened
387
+ end
388
+
389
+ # Example
390
+ # @campaign = Campaign.new(12345)
391
+ # puts @campaign.number_clicks
392
+ def number_clicks
393
+ @number_clicks.nil? ? getInfo.number_clicks : @number_clicks
394
+ end
395
+
396
+ # Example
397
+ # @campaign = Campaign.new(12345)
398
+ # puts @campaign.number_unsubscribed
399
+ def number_unsubscribed
400
+ @number_unsubscribed.nil? ? getInfo.number_unsubscribed : @number_unsubscribed
401
+ end
402
+
403
+ # Example
404
+ # @campaign = Campaign.new(12345)
405
+ # puts @campaign.number_bounced
406
+ def number_bounced
407
+ @number_bounced.nil? ? getInfo.number_bounced : @number_bounced
408
+ end
409
+
410
+ private
411
+ def getInfo
412
+ info = @cm_client.Campaign_GetSummary('CampaignID'=>@id)
413
+ @title = info['title']
414
+ @number_recipients = info["Recipients"].to_i
415
+ @number_opened = info["TotalOpened"].to_i
416
+ @number_clicks = info["Click"].to_i
417
+ @number_unsubscribed = info["Unsubscribed"].to_i
418
+ @number_bounced = info["Bounced"].to_i
419
+ self
420
+ end
421
+ end
422
+
423
+ # Provides the ability to add/remove subscribers from a list
424
+ class Subscriber
425
+ attr_accessor :email_address, :name, :date_subscribed
426
+
427
+ def initialize(email_address, name=nil, date=nil)
428
+ @email_address = email_address
429
+ @name = name
430
+ @date_subscribed = date_subscribed
431
+ @cm_client = CampaignMonitor.new
432
+ end
433
+
434
+ # Example
435
+ # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
436
+ # @subscriber.add(12345)
437
+ def add(list_id)
438
+ response = @cm_client.Subscriber_Add("ListID" => list_id, "Email" => @email_address, "Name" => @name)
439
+ Result.new(response["Message"], response["Code"].to_i)
440
+ end
441
+
442
+ # Example
443
+ # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
444
+ # @subscriber.add_and_resubscribe(12345)
445
+ def add_and_resubscribe(list_id)
446
+ response = @cm_client.Subscriber_AddAndResubscribe("ListID" => list_id, "Email" => @email_address, "Name" => @name)
447
+ Result.new(response["Message"], response["Code"].to_i)
448
+ end
449
+
450
+ # Example
451
+ # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
452
+ # @subscriber.unsubscribe(12345)
453
+ def unsubscribe(list_id)
454
+ response = @cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => @email_address)
455
+ Result.new(response["Message"], response["Code"].to_i)
456
+ end
457
+ end
458
+
459
+ # Encapsulates
460
+ class SubscriberBounce
461
+ attr_reader :email_address, :bounce_type, :list_id
462
+
463
+ def initialize(email_address, list_id, bounce_type)
464
+ @email_address = email_address
465
+ @bounce_type = bounce_type
466
+ @list_id = list_id
467
+ end
468
+ end
469
+
470
+ # Encapsulates
471
+ class SubscriberOpen
472
+ attr_reader :email_address, :list_id, :opens
473
+
474
+ def initialize(email_address, list_id, opens)
475
+ @email_address = email_address
476
+ @list_id = list_id
477
+ @opens = opens
478
+ end
479
+ end
480
+
481
+ # Encapsulates
482
+ class SubscriberClick
483
+ attr_reader :email_address, :list_id, :clicked_links
484
+
485
+ def initialize(email_address, list_id, clicked_links)
486
+ @email_address = email_address
487
+ @list_id = list_id
488
+ @clicked_links = clicked_links
489
+ end
490
+ end
491
+
492
+ # Encapsulates
493
+ class SubscriberUnsubscribe
494
+ attr_reader :email_address, :list_id
495
+
496
+ def initialize(email_address, list_id)
497
+ @email_address = email_address
498
+ @list_id = list_id
499
+ end
500
+ end
501
+
502
+ # Encapsulates the response received from the CampaignMonitor webservice.
503
+ class Result
504
+ attr_reader :message, :code
505
+
506
+ def initialize(message, code)
507
+ @message = message
508
+ @code = code
509
+ end
510
+ end
511
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sevenwire-campaign_monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Brock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple wrapper class that provides basic access to the Campaign Monitor API
17
+ email: jordan@spintech.com.au
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/campaign_monitor.rb
26
+ - init.rb
27
+ - install.rb
28
+ has_rdoc: false
29
+ homepage: http://spintech.com.au/projects/plugins/campaign_monitor
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements:
48
+ - none
49
+ rubyforge_project: campaignmonitor
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: Provides access to the Campaign Monitor API
54
+ test_files: []
55
+