patientslikeme-campaign_monitor 1.2 → 1.2.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.
data/README.rdoc CHANGED
@@ -1,18 +1,52 @@
1
1
  = campaign_monitor
2
2
 
3
- This library provides access to the Campaign Monitor API (http://www.campaignmonitor.com/api)
3
+ This RubyGem provides access to the Campaign Monitor API (http://www.campaignmonitor.com/api).
4
4
 
5
- == Pre-requisites
5
+ Campaign Monitor recently made some changes to their API.
6
6
 
7
- An account with Campaign Monitor and the API Key. Accounts are free and can be obtained from
8
- http://www.campaignmonitor.com
7
+ This fork makes the following changes:
9
8
 
10
- == Resources
9
+ * host changed from http://app.campaignmonitor.com to http://api.createsend.com
10
+ * ID values are no longer sent #to_i because they are hex strings
11
+ * added support for subscribers with custom fields using SOAP API
12
+ * refactored gemspec to build on github
13
+ * misc. cleanup and refactoring
11
14
 
12
- Install
13
15
 
14
- * gem install patientslikeme-campaign_monitor
16
+ == Pre-requisites
15
17
 
16
- Git Repository
18
+ An account with Campaign Monitor and the API Key. Accounts are free and can be created at
19
+ http://www.campaignmonitor.com.
20
+
21
+ == Resources
17
22
 
18
- * http://github.com/patientslikeme/campaign_monitor
23
+ === Install
24
+ gem install patientslikeme-campaign_monitor
25
+
26
+ === Git Repository
27
+ http://github.com/patientslikeme/campaign_monitor
28
+
29
+
30
+ == Usage
31
+
32
+ cm = CampaignMonitor.new # assumes you've set CAMPAIGN_MONITOR_API_KEY in your project
33
+
34
+ for client in cm.clients
35
+ for list in client.lists
36
+ client.name # => returns the name
37
+
38
+ # modify a subscriber list
39
+ list.add_subscriber(email, name, custom_fields_hash)
40
+ list.remove_subscriber(email)
41
+ list.add_and_resubscribe(email, name, custom_fields_hash)
42
+
43
+ # get subscriber list details
44
+ subscribers = list.active_subscribers(since_time)
45
+ unsubscribed = list.unsubscribed(since_time)
46
+ bounced = list.bounced(since_time)
47
+ end
48
+
49
+ for campaign in client.campaigns
50
+
51
+ end
52
+ end
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.platform = Gem::Platform::RUBY
3
3
  s.name = 'campaign_monitor'
4
- s.version = "1.2"
4
+ s.version = "1.2.1"
5
5
  s.summary = 'Provides access to the Campaign Monitor API.'
6
6
  s.description = <<-EOF
7
7
  A simple wrapper class that provides basic access to the Campaign Monitor API.
@@ -25,14 +25,15 @@ Gem::Specification.new do |s|
25
25
  'lib/campaign_monitor.rb',
26
26
  'lib/campaign_monitor/campaign.rb',
27
27
  'lib/campaign_monitor/client.rb',
28
+ 'lib/campaign_monitor/helpers.rb',
28
29
  'lib/campaign_monitor/list.rb',
29
30
  'lib/campaign_monitor/result.rb',
30
31
  'lib/campaign_monitor/subscriber.rb',
31
32
 
32
33
  'support/faster-xml-simple/lib/faster_xml_simple.rb',
33
- 'support/faster-xml-simple/test/xml_simple_comparison_test.rb',
34
34
  'support/faster-xml-simple/test/regression_test.rb',
35
- 'support/faster-xml-simple/lib/faster_xml_simple.rb',
35
+ 'support/faster-xml-simple/test/test_helper.rb',
36
+ 'support/faster-xml-simple/test/xml_simple_comparison_test.rb',
36
37
 
37
38
  'test/campaign_monitor_test.rb',
38
39
  ]
@@ -68,8 +68,8 @@ 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
-
71
+ include CampaignMonitor::Helpers
72
+
73
73
  attr_reader :api_key, :api_url
74
74
 
75
75
  # Replace this API key with your own (http://www.campaignmonitor.com/api/)
@@ -92,7 +92,12 @@ class CampaignMonitor
92
92
  # Takes a CampaignMonitor API method name and set of parameters; returns the correct URL for the REST API.
93
93
  def request_url(method, params={})
94
94
  params.merge!('ApiKey' => api_key)
95
- "#{api_url}/#{method}?#{params.to_query}"
95
+
96
+ query = params.collect do |key, value|
97
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
98
+ end.sort * '&'
99
+
100
+ "#{api_url}/#{method}?#{query}"
96
101
  end
97
102
 
98
103
  # Does an HTTP GET on a given URL and returns the response body
@@ -116,17 +121,13 @@ class CampaignMonitor
116
121
  # puts client.name
117
122
  # end
118
123
  def clients
119
- response = User_GetClients()
120
- handle_response(response) do
124
+ handle_response(User_GetClients()) do |response|
121
125
  response["Client"].collect{|c| Client.new(c["ClientID"], c["Name"])}
122
126
  end
123
127
  end
124
128
 
125
129
  def system_date
126
- response = User_GetSystemDate()
127
- handle_response(response) do
128
- response
129
- end
130
+ User_GetSystemDate()
130
131
  end
131
132
 
132
133
  def parsed_system_date
@@ -143,8 +144,7 @@ class CampaignMonitor
143
144
  # puts campaign.subject
144
145
  # end
145
146
  def campaigns(client_id)
146
- response = Client_GetCampaigns("ClientID" => client_id)
147
- handle_response(response) do
147
+ handle_response(Client_GetCampaigns("ClientID" => client_id)) do |response|
148
148
  response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
149
149
  end
150
150
  end
@@ -159,8 +159,7 @@ class CampaignMonitor
159
159
  # puts list.name
160
160
  # end
161
161
  def lists(client_id)
162
- response = Client_GetLists("ClientID" => client_id)
163
- handle_response(response) do
162
+ handle_response(Client_GetLists("ClientID" => client_id)) do |response|
164
163
  response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
165
164
  end
166
165
  end
@@ -177,6 +176,7 @@ class CampaignMonitor
177
176
  def add_subscriber(list_id, email, name)
178
177
  Result.new(Subscriber_Add("ListID" => list_id, "Email" => email, "Name" => name))
179
178
  end
179
+
180
180
 
181
181
  # Encapsulates
182
182
  class SubscriberBounce
@@ -1,7 +1,7 @@
1
1
  class CampaignMonitor
2
2
  # Provides access to the information about a campaign
3
3
  class Campaign
4
- include Helpers
4
+ include CampaignMonitor::Helpers
5
5
 
6
6
  attr_reader :id, :subject, :sent_date, :total_recipients, :cm_client
7
7
 
@@ -1,7 +1,7 @@
1
1
  class CampaignMonitor
2
2
  # Provides access to the lists and campaigns associated with a client
3
3
  class Client
4
- include Helpers
4
+ include CampaignMonitor::Helpers
5
5
 
6
6
  attr_reader :id, :name, :cm_client
7
7
 
@@ -21,9 +21,7 @@ class CampaignMonitor
21
21
  # puts list.name
22
22
  # end
23
23
  def lists
24
- handle_response(cm_client.Client_GetLists("ClientID" => self.id)) do |response|
25
- response["List"].collect{|l| List.new(l["ListID"], l["Name"])}
26
- end
24
+ cm_client.lists(self.id)
27
25
  end
28
26
 
29
27
  # Example
@@ -34,9 +32,7 @@ class CampaignMonitor
34
32
  # puts campaign.subject
35
33
  # end
36
34
  def campaigns
37
- handle_response(cm_client.Client_GetCampaigns("ClientID" => self.id)) do |response|
38
- response["Campaign"].collect{|c| Campaign.new(c["CampaignID"], c["Subject"], c["SentDate"], c["TotalRecipients"].to_i)}
39
- end
35
+ cm_client.campaigns(self.id)
40
36
  end
41
37
  end
42
38
  end
@@ -0,0 +1,35 @@
1
+ class CampaignMonitor
2
+ module Helpers
3
+
4
+ def handle_response(response)
5
+ return [] if response.empty?
6
+
7
+ if response["Code"].to_i == 0
8
+ # success!
9
+ yield(response)
10
+ else
11
+ # error!
12
+ raise response["Code"] + " - " + response["Message"]
13
+ end
14
+ end
15
+
16
+ def wsdl_driver_factory
17
+ SOAP::WSDLDriverFactory.new("#{api_url}?WSDL")
18
+ end
19
+
20
+ def using_soap
21
+ driver = wsdl_driver_factory.create_rpc_driver
22
+ response = yield(driver)
23
+ driver.reset_stream
24
+ end
25
+
26
+ def timestamp_format
27
+ '%Y-%m-%d %H:%M:%S'
28
+ end
29
+
30
+ def formatted_timestamp(datetime, format=timestamp_format)
31
+ datetime.strftime(format)
32
+ end
33
+
34
+ end
35
+ end
@@ -4,8 +4,8 @@ class CampaignMonitor
4
4
  # Provides access to the subscribers and info about subscribers
5
5
  # associated with a Mailing List
6
6
  class List
7
- include Helpers
8
-
7
+ include CampaignMonitor::Helpers
8
+
9
9
  attr_reader :id, :name, :cm_client
10
10
 
11
11
  # Example
@@ -1,9 +1,10 @@
1
1
  class CampaignMonitor
2
2
  # Provides the ability to add/remove subscribers from a list
3
3
  class Subscriber
4
- include Helpers
4
+ include CampaignMonitor::Helpers
5
5
 
6
6
  attr_accessor :email_address, :name, :date_subscribed
7
+ attr_reader :cm_client
7
8
 
8
9
  def initialize(email_address, name=nil, date=nil)
9
10
  @email_address = email_address
@@ -16,25 +17,25 @@ class CampaignMonitor
16
17
  # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
17
18
  # @subscriber.add(12345)
18
19
  def add(list_id)
19
- Result.new(@cm_client.Subscriber_Add("ListID" => list_id, "Email" => @email_address, "Name" => @name))
20
+ Result.new(cm_client.Subscriber_Add("ListID" => list_id, "Email" => @email_address, "Name" => @name))
20
21
  end
21
22
 
22
23
  # Example
23
24
  # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
24
25
  # @subscriber.add_and_resubscribe(12345)
25
26
  def add_and_resubscribe(list_id)
26
- Result.new(@cm_client.Subscriber_AddAndResubscribe("ListID" => list_id, "Email" => @email_address, "Name" => @name))
27
+ Result.new(cm_client.Subscriber_AddAndResubscribe("ListID" => list_id, "Email" => @email_address, "Name" => @name))
27
28
  end
28
29
 
29
30
  # Example
30
31
  # @subscriber = Subscriber.new("ralph.wiggum@simpsons.net")
31
32
  # @subscriber.unsubscribe(12345)
32
33
  def unsubscribe(list_id)
33
- Result.new(@cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => @email_address))
34
+ Result.new(cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => @email_address))
34
35
  end
35
36
 
36
37
  def is_subscribed?(list_id)
37
- result = @cm_client.Subscribers_GetIsSubscribed("ListID" => list_id, "Email" => @email_address)
38
+ result = cm_client.Subscribers_GetIsSubscribed("ListID" => list_id, "Email" => @email_address)
38
39
  return true if result == 'True'
39
40
  return false if result == 'False'
40
41
  raise "Invalid value for is_subscribed?: #{result}"
@@ -0,0 +1,17 @@
1
+
2
+ require 'test/unit'
3
+ require 'faster_xml_simple'
4
+
5
+ class FasterXSTest < Test::Unit::TestCase
6
+ def default_test
7
+ end
8
+
9
+ def silence_stderr
10
+ str = STDERR.dup
11
+ STDERR.reopen("/dev/null")
12
+ STDERR.sync=true
13
+ yield
14
+ ensure
15
+ STDERR.reopen(str)
16
+ end
17
+ end
@@ -1,6 +1,58 @@
1
+ require 'rubygems'
2
+ require 'campaign_monitor'
3
+ require 'test/unit'
4
+
5
+ CAMPAIGN_MONITOR_API_KEY = 'Your API Key'
6
+ CLIENT_NAME = '_Test'
7
+ LIST_NAME = '_List1'
8
+
1
9
  class CampaignMonitorTest < Test::Unit::TestCase
2
10
 
3
11
  def setup
12
+ @cm = CampaignMonitor.new
13
+ end
14
+
15
+ def test_clients
16
+ clients = @cm.clients
17
+ assert clients.size > 0
4
18
 
19
+ assert_equal CLIENT_NAME, find_test_client(clients).name
20
+ end
21
+
22
+ def test_lists
23
+ client = find_test_client
24
+ assert_not_nil client
25
+
26
+ lists = client.lists
27
+ assert lists.size > 0
28
+
29
+ list = find_test_list(lists)
30
+ assert_equal LIST_NAME, list.name
31
+ end
32
+
33
+ def test_list_add_subscriber
34
+ list = find_test_list
35
+
36
+ assert_success list.add_and_resubscribe('a@test.com', 'Test A')
37
+ assert_success list.remove_subscriber('a@test.com')
5
38
  end
39
+
40
+ def test_campaigns
41
+ client = find_test_client
42
+ assert_not_nil client.campaigns
43
+ end
44
+
45
+
46
+ protected
47
+ def assert_success(result)
48
+ assert result.succeeded?, result.message
49
+ end
50
+
51
+ def find_test_client(clients=@cm.clients)
52
+ clients.detect { |c| c.name == CLIENT_NAME }
53
+ end
54
+
55
+ def find_test_list(lists=find_test_client.lists)
56
+ lists.detect { |l| l.name == LIST_NAME }
57
+ end
6
58
  end
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.2"
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Weiskotten
@@ -31,12 +31,14 @@ files:
31
31
  - lib/campaign_monitor.rb
32
32
  - lib/campaign_monitor/campaign.rb
33
33
  - lib/campaign_monitor/client.rb
34
+ - lib/campaign_monitor/helpers.rb
34
35
  - lib/campaign_monitor/list.rb
35
36
  - lib/campaign_monitor/result.rb
36
37
  - lib/campaign_monitor/subscriber.rb
37
38
  - support/faster-xml-simple/lib/faster_xml_simple.rb
38
- - support/faster-xml-simple/test/xml_simple_comparison_test.rb
39
39
  - support/faster-xml-simple/test/regression_test.rb
40
+ - support/faster-xml-simple/test/test_helper.rb
41
+ - support/faster-xml-simple/test/xml_simple_comparison_test.rb
40
42
  - test/campaign_monitor_test.rb
41
43
  has_rdoc: true
42
44
  homepage: http://github.com/patientslikeme/campaign_monitor/