gnumarcelo-campaigning 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,16 @@
1
1
  # Campaign is defined in soap/default.rb which is automatically generated.
2
2
  # In this file we add additional methods to the Campaign class.
3
- require File.expand_path(File.dirname(__FILE__)) + '/../helpers/helpers'
4
-
3
+ require File.expand_path(File.dirname(__FILE__)) + '/module_mixin'
5
4
 
6
5
  module Campaigning
7
-
8
6
  class List
9
- include Helpers
7
+ include ModuleMixin
10
8
  attr_accessor :listID
11
9
  attr_accessor :name
12
10
 
13
11
  def initialize(listID = nil, name = nil)
14
12
  @listID = listID
15
13
  @name = name
16
- @soap = Campaigning::SOAPDriver.instance.get_driver
17
14
  end
18
15
 
19
16
  #Creates a brand new subscriber list
@@ -32,19 +29,19 @@ module Campaigning
32
29
  #*Success*: Upon a successful call, this method will return a Campaigning::List object representing the newly created list.
33
30
  #
34
31
  #*Error*: An Exception containing the cause of the error will be raised.
35
- def self.create(params)
36
- response = Campaigning::SOAPDriver.instance.get_driver.createList(
37
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
38
- :clientID => params[:clientID],
39
- :title => params[:title],
40
- :unsubscribePage => params.fetch(:unsubscribePage, ""),
41
- :confirmOptIn => params[:confirmOptIn],
42
- :confirmationSuccessPage => params.fetch(:confirmationSuccessPage, "")
32
+ def self.create!(params)
33
+ response = @@soap.createList(
34
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
35
+ :clientID => params[:clientID],
36
+ :title => params[:title],
37
+ :unsubscribePage => params.fetch(:unsubscribePage, ""),
38
+ :confirmOptIn => params[:confirmOptIn],
39
+ :confirmationSuccessPage => params.fetch(:confirmationSuccessPage, "")
43
40
  )
44
41
  new_list_id = handle_response response.list_CreateResult
45
42
  List.new(new_list_id, params[:title])
46
43
  end
47
-
44
+
48
45
  #Creates a new custom field for a list
49
46
  #
50
47
  #Available _params_ argument are:
@@ -59,17 +56,17 @@ module Campaigning
59
56
  #containing a successful message.
60
57
  #
61
58
  #*Error*: An Exception containing the cause of the error will be raised.
62
- def create_custom_field(params)
63
- response = @soap.createListCustomField(
64
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
65
- :listID => @listID,
66
- :fieldName => params[:fieldName],
67
- :dataType => params[:dataType],
68
- :options => params.fetch(:options, "")
59
+ def create_custom_field!(params)
60
+ response = @@soap.createListCustomField(
61
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
62
+ :listID => @listID,
63
+ :fieldName => params[:fieldName],
64
+ :dataType => params[:dataType],
65
+ :options => params.fetch(:options, "")
69
66
  )
70
67
  handle_response response.list_CreateCustomFieldResult
71
68
  end
72
-
69
+
73
70
  #Deletes a list
74
71
  #
75
72
  #*Return*:
@@ -78,8 +75,8 @@ module Campaigning
78
75
  #containing a successful message.
79
76
  #
80
77
  #*Error*: An Exception containing the cause of the error will be raised.
81
- def delete
82
- List.delete(@listID)
78
+ def delete!
79
+ List.delete!(@listID)
83
80
  self.listID, self.name = nil, nil
84
81
  end
85
82
 
@@ -91,11 +88,11 @@ module Campaigning
91
88
  #containing a successful message.
92
89
  #
93
90
  #*Error*: An Exception containing the cause of the error will be raised.
94
- def self.delete(list_id)
95
- response = Campaigning::SOAPDriver.instance.get_driver.deleteList(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => list_id)
91
+ def self.delete!(list_id)
92
+ response = @@soap.deleteList(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => list_id)
96
93
  handle_response response.list_DeleteResult
97
94
  end
98
-
95
+
99
96
  #Deletes a custom field from a list
100
97
  #
101
98
  #*Return*:
@@ -104,11 +101,11 @@ module Campaigning
104
101
  #containing a successful message.
105
102
  #
106
103
  #*Error*: An Exception containing the cause of the error will be raised.
107
- def delete_custom_field(key)
108
- response = @soap.deleteListCustomField(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID, :key => '['+key+']')
104
+ def delete_custom_field!(key)
105
+ response = @@soap.deleteListCustomField(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID, :key => '['+key+']')
109
106
  handle_response response.list_DeleteCustomFieldResult
110
107
  end
111
-
108
+
112
109
  #Gets all the Custom Fields available for a list
113
110
  #
114
111
  #*Return*:
@@ -118,10 +115,10 @@ module Campaigning
118
115
  #
119
116
  #*Error*: An Exception containing the cause of the error will be raised.
120
117
  def custom_fields
121
- response = @soap.getListCustomFields(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID)
118
+ response = @@soap.getListCustomFields(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID)
122
119
  handle_response response.list_GetCustomFieldsResult
123
120
  end
124
-
121
+
125
122
  #Gets a list's configuration detail
126
123
  #
127
124
  #*Return*:
@@ -131,10 +128,10 @@ module Campaigning
131
128
  #
132
129
  #*Error*: An Exception containing the cause of the error will be raised.
133
130
  def details
134
- response = @soap.getListDetail(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID)
131
+ response = @@soap.getListDetail(:apiKey => CAMPAIGN_MONITOR_API_KEY, :listID => @listID)
135
132
  handle_response response.list_GetDetailResult
136
133
  end
137
-
134
+
138
135
  #Gets a list of all active subscribers for a list.
139
136
  #
140
137
  #*Return*:
@@ -145,7 +142,7 @@ module Campaigning
145
142
  def get_all_active_subscribers
146
143
  find_active_subscribers(DateTime.new(y=1911,m=1,d=01, h=01,min=00,s=00))
147
144
  end
148
-
145
+
149
146
  #Gets a list of all active subscribers for a list that have been joined since the specified date.
150
147
  #The +joined_at+ param has to be a DateTime object, like:
151
148
  #
@@ -157,10 +154,10 @@ module Campaigning
157
154
  #
158
155
  #*Error*: An Exception containing the cause of the error will be raised.
159
156
  def find_active_subscribers(joined_at)
160
- response = @soap.getSubscribers(
161
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
162
- :listID => @listID,
163
- :date =>joined_at.strftime('%Y-%m-%d %H:%M:%S')
157
+ response = @@soap.getSubscribers(
158
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
159
+ :listID => @listID,
160
+ :date =>joined_at.strftime('%Y-%m-%d %H:%M:%S')
164
161
  )
165
162
  handle_response response.subscribers_GetActiveResult
166
163
  end
@@ -177,14 +174,14 @@ module Campaigning
177
174
  #
178
175
  #*Error*: An Exception containing the cause of the error will be raised.
179
176
  def find_unsubscribed(unjoined_at)
180
- response = @soap.getUnsubscribed(
181
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
182
- :listID => @listID,
183
- :date => unjoined_at.strftime('%Y-%m-%d %H:%M:%S') # TODO: Move that to a helper method
177
+ response = @@soap.getUnsubscribed(
178
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
179
+ :listID => @listID,
180
+ :date => unjoined_at.strftime('%Y-%m-%d %H:%M:%S') # TODO: Move that to a helper method
184
181
  )
185
182
  handle_response response.subscribers_GetUnsubscribedResult
186
183
  end
187
-
184
+
188
185
  #This method returns all of a particular subscribers details, including email address, name, active/inactive
189
186
  #status and all custom field data. If a subscriber with that email address does not exist in that list, a +nil+ value is returned.
190
187
  #
@@ -194,14 +191,14 @@ module Campaigning
194
191
  #
195
192
  #*Error*: An Exception containing the cause of the error will be raised.
196
193
  def find_single_subscriber(email_address) # TODO: Create a mehod to handle with custom fields returned like (names from "State Name" to "state_name")
197
- response = @soap.getSingleSubscriber(
198
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
199
- :listID => @listID,
200
- :emailAddress => email_address
194
+ response = @@soap.getSingleSubscriber(
195
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
196
+ :listID => @listID,
197
+ :emailAddress => email_address
201
198
  )
202
199
  handle_response response.subscribers_GetSingleSubscriberResult
203
200
  end
204
-
201
+
205
202
  #Update a subscriber list’s details
206
203
  #
207
204
  #Available _params_ argument are:
@@ -219,23 +216,23 @@ module Campaigning
219
216
  #containing a successful message.
220
217
  #
221
218
  #*Error*: An Exception containing the cause of the error will be raised.
222
- def update(params)
223
- response = @soap.updateList(
224
- :apiKey => CAMPAIGN_MONITOR_API_KEY,
225
- :listID => @listID,
226
- :title => params[:title],
227
- :unsubscribePage => params.fetch(:unsubscribePage, ""),
228
- :confirmOptIn => params[:confirmOptIn],
229
- :confirmationSuccessPage => params.fetch(:confirmationSuccessPage, "")
219
+ def update!(params)
220
+ response = @@soap.updateList(
221
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
222
+ :listID => @listID,
223
+ :title => params[:title],
224
+ :unsubscribePage => params.fetch(:unsubscribePage, ""),
225
+ :confirmOptIn => params[:confirmOptIn],
226
+ :confirmationSuccessPage => params.fetch(:confirmationSuccessPage, "")
230
227
  )
231
228
  handle_response response.list_UpdateResult
232
229
  end
233
-
230
+
234
231
  protected
235
232
 
236
- def custom_field_options(custom_fields) #:nodoc:
237
- custom_fields.join("||").to_s
238
- end
233
+ def custom_field_options(custom_fields) #:nodoc:
234
+ custom_fields.join("||").to_s
235
+ end
239
236
 
240
237
  end
241
238
  end
@@ -1,17 +1,21 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + '/soap/generated/defaultDriver.rb'
1
2
  module Campaigning
2
- module Helpers #:nodoc:
3
+ module ModuleMixin #:nodoc:
4
+ #DefaultEndpointUrl = "http://api.createsend.com/api/api.asmx"
5
+ @@soap = Campaigning::ApiSoap.new
6
+
3
7
  def self.included(base)
4
8
  base.extend(ClassMethods) # Make all ClassMethods methods avaiable to the object including this module.
5
9
  private :handle_response
6
10
  end
7
-
11
+
8
12
  def handle_response(response)
9
13
  self.class.handle_response(response)
10
14
  end
11
-
15
+
12
16
  # All methods above will became Object methods
13
17
  module ClassMethods #:nodoc:
14
-
18
+
15
19
  #Method responsable to handle all response from the API server and raising an exception when
16
20
  #the API returns an error code (different from 0 (zero) ).
17
21
  def handle_response(response)
@@ -20,7 +24,7 @@ module Campaigning
20
24
  end
21
25
  response
22
26
  end
23
-
27
+
24
28
  end
25
29
 
26
30
  end
@@ -0,0 +1,145 @@
1
+ # Subscriber is defined in default.rb which is automatically generated.
2
+ # In this file we add additional methods to the Subscriber class.
3
+ require File.expand_path(File.dirname(__FILE__)) + '/module_mixin'
4
+
5
+ module Campaigning
6
+ class Subscriber
7
+ include ModuleMixin
8
+ attr_accessor :emailAddress
9
+ attr_accessor :name
10
+ attr_accessor :date
11
+ attr_accessor :state
12
+ attr_accessor :customFields
13
+
14
+ def initialize(emailAddress = nil, name = nil, date = nil, state = nil, customFields = nil)
15
+ @emailAddress = emailAddress
16
+ @name = name
17
+ @date = date
18
+ @state = state
19
+ @customFields = customFields
20
+ end
21
+
22
+
23
+ #Adds a subscriber to a subscriber list, including adding custom field data for the subscriber. If the subscriber
24
+ #(email address) already exists, the name value is updated with whatever is passed in.
25
+ #
26
+ #If the list has been set as double opt-in, they will be sent the verification email, otherwise they will be sent the
27
+ #confirmation email you have set up for the list being subscribed to.
28
+ #
29
+ #<b>Please note</b>: If the subscriber is in an inactive state or has previously been unsubscribed, they will *not* be re-added
30
+ #to the active list. Therefore, this method should be used with caution and only where suitable.
31
+ #
32
+ #*Return*:
33
+ #
34
+ #*Success*: Upon a successful call, this method will return a Campaigning::Result object wich consists of a +code+ and +message+ fields
35
+ #containing a successful message.
36
+ #
37
+ #*Error*: An Exception containing the cause of the error will be raised.
38
+ def add!(list_id, custom_fields={})
39
+ params = {
40
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
41
+ :listID => list_id,
42
+ :email => @emailAddress,
43
+ :name => @name
44
+ }
45
+ if custom_fields.empty?
46
+ response = @@soap.addSubscriber(params)
47
+ handle_response response.subscriber_AddResult
48
+ else
49
+ params.merge!({:customFields => custom_fields_array(custom_fields)})
50
+ response = @@soap.addSubscriberWithCustomFields(params)
51
+ handle_response response.subscriber_AddWithCustomFieldsResult
52
+ end
53
+ end
54
+
55
+
56
+ #Adds a subscriber to a subscriber list, including adding custom field data for the subscriber. If the subscriber (email address) already exists,
57
+ #the name value is updated with whatever is passed in.
58
+ #
59
+ #If the list has been set as double opt-in, they will be sent the verification email, otherwise they will be sent the
60
+ #confirmation email you have set up for the list being subscribed to.
61
+ #
62
+ #<b>Please note</b>: If the subscriber is in an inactive state or has previously been unsubscribed, they will be re-added to
63
+ #the active list. Therefore, this method should be used with caution and only where suitable.
64
+ #
65
+ #*Return*:
66
+ #
67
+ #*Success*: Upon a successful call, this method will return a Campaigning::Result object wich consists of a +code+ and +message+ fields
68
+ #containing a successful message.
69
+ #
70
+ #*Error*: An Exception containing the cause of the error will be raised.
71
+ def add_and_resubscribe!(list_id, custom_fields={})
72
+ params = {
73
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
74
+ :listID => list_id,
75
+ :email => @emailAddress,
76
+ :name => @name
77
+ }
78
+ if custom_fields.empty?
79
+ response = @@soap.addAndResubscribe(params)
80
+ handle_response response.subscriber_AddAndResubscribeResult
81
+ else
82
+ params.merge!({:customFields => custom_fields_array(custom_fields)})
83
+ response = @@soap.addAndResubscribeWithCustomFields(params)
84
+ handle_response response.subscriber_AddAndResubscribeWithCustomFieldsResult
85
+ end
86
+ end
87
+
88
+
89
+ #Changes the status of an Active Subscriber to an Unsubscribed Subscriber who will no longer receive
90
+ #campaigns sent to that Subscriber List.
91
+ #
92
+ #If the list is set to add unsubscribing subscribers to the suppression list, then the subscriber’s email address will
93
+ #also be added to the suppression list.
94
+ #
95
+ #*Return*:
96
+ #
97
+ #*Success*: Upon a successful call, this method will return a Campaigning::Result object wich consists of a +code+ and +message+ fields
98
+ #containing a successful message.
99
+ #
100
+ #*Error*: An Exception containing the cause of the error will be raised.
101
+ def unsubscribe(list_id)
102
+ Subscriber.unsubscribe!(@emailAddress, list_id)
103
+ end
104
+
105
+
106
+ #Changes the status of an Active Subscriber to an Unsubscribed Subscriber who will no longer receive
107
+ #campaigns sent to that Subscriber List (Same that the instance method with the same name).
108
+ def self.unsubscribe!(email, list_id)
109
+ response = @@soap.unsubscribe(
110
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
111
+ :listID => list_id,
112
+ :email => email
113
+ )
114
+ handle_response response.subscriber_UnsubscribeResult
115
+ end
116
+
117
+ #Returns True or False as to the existence of the given email address in the list supplied.
118
+ def is_subscribed?(list_id)
119
+ Subscriber.is_subscribed?(@emailAddress, list_id)
120
+ end
121
+
122
+ #Returns True or False as to the existence of the given email address in the list supplied.
123
+ def self.is_subscribed?(email, list_id)
124
+ response = @@soap.getIsSubscribed(
125
+ :apiKey => CAMPAIGN_MONITOR_API_KEY,
126
+ :listID => list_id,
127
+ :email => email
128
+ )
129
+ response = handle_response response.subscribers_GetIsSubscribedResult
130
+ response == 'True' ? true : false
131
+ end
132
+
133
+
134
+ protected
135
+
136
+ def custom_fields_array(custom_fields) #:nodoc:
137
+ arr = []
138
+ custom_fields.each do |key, value|
139
+ arr << { :key => key, :value => value }
140
+ end
141
+ arr
142
+ end
143
+
144
+ end
145
+ end