createsend 2.5.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,11 @@
1
- require 'createsend'
2
- require 'json'
3
-
4
1
  module CreateSend
5
2
  # Represents a subscriber list and associated functionality.
6
- class List
3
+ class List < CreateSend
7
4
  attr_reader :list_id
8
5
 
9
- def initialize(list_id)
6
+ def initialize(auth, list_id)
10
7
  @list_id = list_id
8
+ super
11
9
  end
12
10
 
13
11
  # Creates a new list for a client.
@@ -23,7 +21,7 @@ module CreateSend
23
21
  # unsubscribe_setting - A String which must be either "AllClientLists" or
24
22
  # "OnlyThisList". See the documentation for details:
25
23
  # http://www.campaignmonitor.com/api/lists/#creating_a_list
26
- def self.create(client_id, title, unsubscribe_page, confirmed_opt_in,
24
+ def self.create(auth, client_id, title, unsubscribe_page, confirmed_opt_in,
27
25
  confirmation_success_page, unsubscribe_setting="AllClientLists")
28
26
  options = { :body => {
29
27
  :Title => title,
@@ -31,13 +29,14 @@ module CreateSend
31
29
  :ConfirmedOptIn => confirmed_opt_in,
32
30
  :ConfirmationSuccessPage => confirmation_success_page,
33
31
  :UnsubscribeSetting => unsubscribe_setting }.to_json }
34
- response = CreateSend.post "/lists/#{client_id}.json", options
32
+ cs = CreateSend.new auth
33
+ response = cs.post "/lists/#{client_id}.json", options
35
34
  response.parsed_response
36
35
  end
37
36
 
38
37
  # Deletes this list.
39
38
  def delete
40
- response = CreateSend.delete "/lists/#{list_id}.json", {}
39
+ response = super "/lists/#{list_id}.json", {}
41
40
  end
42
41
 
43
42
  # Creates a new custom field for this list.
@@ -78,7 +77,7 @@ module CreateSend
78
77
  # Deletes a custom field associated with this list.
79
78
  def delete_custom_field(custom_field_key)
80
79
  custom_field_key = CGI.escape(custom_field_key)
81
- response = CreateSend.delete(
80
+ response = cs_delete(
82
81
  "/lists/#{list_id}/customfields/#{custom_field_key}.json", {})
83
82
  end
84
83
 
@@ -94,7 +93,7 @@ module CreateSend
94
93
 
95
94
  # Gets the details of this list.
96
95
  def details
97
- response = CreateSend.get "/lists/#{list_id}.json", {}
96
+ response = cs_get "/lists/#{list_id}.json"
98
97
  Hashie::Mash.new(response)
99
98
  end
100
99
 
@@ -119,66 +118,36 @@ module CreateSend
119
118
  # Gets the active subscribers for this list.
120
119
  def active(date="", page=1, page_size=1000, order_field="email",
121
120
  order_direction="asc")
122
- options = { :query => {
123
- :date => date,
124
- :page => page,
125
- :pagesize => page_size,
126
- :orderfield => order_field,
127
- :orderdirection => order_direction } }
128
- response = get "active", options
129
- Hashie::Mash.new(response)
121
+ paged_result_by_date("active", date, page, page_size, order_field,
122
+ order_direction)
130
123
  end
131
124
 
132
125
  # Gets the unconfirmed subscribers for this list.
133
126
  def unconfirmed(date="", page=1, page_size=1000, order_field="email",
134
127
  order_direction="asc")
135
- options = { :query => {
136
- :date => date,
137
- :page => page,
138
- :pagesize => page_size,
139
- :orderfield => order_field,
140
- :orderdirection => order_direction } }
141
- response = get "unconfirmed", options
142
- Hashie::Mash.new(response)
128
+ paged_result_by_date("unconfirmed", date, page, page_size, order_field,
129
+ order_direction)
143
130
  end
144
131
 
145
132
  # Gets the bounced subscribers for this list.
146
133
  def bounced(date="", page=1, page_size=1000, order_field="email",
147
134
  order_direction="asc")
148
- options = { :query => {
149
- :date => date,
150
- :page => page,
151
- :pagesize => page_size,
152
- :orderfield => order_field,
153
- :orderdirection => order_direction } }
154
- response = get "bounced", options
155
- Hashie::Mash.new(response)
135
+ paged_result_by_date("bounced", date, page, page_size, order_field,
136
+ order_direction)
156
137
  end
157
138
 
158
139
  # Gets the unsubscribed subscribers for this list.
159
140
  def unsubscribed(date="", page=1, page_size=1000, order_field="email",
160
141
  order_direction="asc")
161
- options = { :query => {
162
- :date => date,
163
- :page => page,
164
- :pagesize => page_size,
165
- :orderfield => order_field,
166
- :orderdirection => order_direction } }
167
- response = get "unsubscribed", options
168
- Hashie::Mash.new(response)
142
+ paged_result_by_date("unsubscribed", date, page, page_size, order_field,
143
+ order_direction)
169
144
  end
170
145
 
171
146
  # Gets the deleted subscribers for this list.
172
147
  def deleted(date="", page=1, page_size=1000, order_field="email",
173
148
  order_direction="asc")
174
- options = { :query => {
175
- :date => date,
176
- :page => page,
177
- :pagesize => page_size,
178
- :orderfield => order_field,
179
- :orderdirection => order_direction } }
180
- response = get "deleted", options
181
- Hashie::Mash.new(response)
149
+ paged_result_by_date("deleted", date, page, page_size, order_field,
150
+ order_direction)
182
151
  end
183
152
 
184
153
  # Updates this list.
@@ -209,7 +178,7 @@ module CreateSend
209
178
  :UnsubscribeSetting => unsubscribe_setting,
210
179
  :AddUnsubscribesToSuppList => add_unsubscribes_to_supp_list,
211
180
  :ScrubActiveWithSuppList => scrub_active_with_supp_list }.to_json }
212
- response = CreateSend.put "/lists/#{list_id}.json", options
181
+ response = cs_put "/lists/#{list_id}.json", options
213
182
  end
214
183
 
215
184
  # Gets the webhooks for this list.
@@ -239,7 +208,7 @@ module CreateSend
239
208
 
240
209
  # Deletes a webhook associated with this list.
241
210
  def delete_webhook(webhook_id)
242
- response = CreateSend.delete(
211
+ response = cs_delete(
243
212
  "/lists/#{list_id}/webhooks/#{webhook_id}.json", {})
244
213
  end
245
214
 
@@ -256,17 +225,29 @@ module CreateSend
256
225
  end
257
226
 
258
227
  private
228
+
229
+ def paged_result_by_date(resource, date, page, page_size, order_field,
230
+ order_direction)
231
+ options = { :query => {
232
+ :date => date,
233
+ :page => page,
234
+ :pagesize => page_size,
235
+ :orderfield => order_field,
236
+ :orderdirection => order_direction } }
237
+ response = get resource, options
238
+ Hashie::Mash.new(response)
239
+ end
259
240
 
260
241
  def get(action, options = {})
261
- CreateSend.get uri_for(action), options
242
+ super uri_for(action), options
262
243
  end
263
244
 
264
245
  def post(action, options = {})
265
- CreateSend.post uri_for(action), options
246
+ super uri_for(action), options
266
247
  end
267
248
 
268
249
  def put(action, options = {})
269
- CreateSend.put uri_for(action), options
250
+ super uri_for(action), options
270
251
  end
271
252
 
272
253
  def uri_for(action)
@@ -1,33 +1,33 @@
1
- require 'createsend'
2
- require 'json'
3
-
4
1
  module CreateSend
5
2
  # Represents a person and associated functionality.
6
- class Person
3
+ class Person < CreateSend
7
4
  attr_reader :client_id
8
5
  attr_reader :email_address
9
6
 
10
- def initialize(client_id, email_address)
7
+ def initialize(auth, client_id, email_address)
11
8
  @client_id = client_id
12
9
  @email_address = email_address
10
+ super
13
11
  end
14
12
 
15
13
  # Gets a person by client ID and email address.
16
- def self.get(client_id, email_address)
14
+ def self.get(auth, client_id, email_address)
17
15
  options = { :query => { :email => email_address } }
18
- response = CreateSend.get "/clients/#{client_id}/people.json", options
16
+ cs = CreateSend.new auth
17
+ response = cs.get "/clients/#{client_id}/people.json", options
19
18
  Hashie::Mash.new(response)
20
19
  end
21
20
 
22
- # Adds a person to the client. password is optional. if ommitted, an
21
+ # Adds a person to the client. Password is optional. If ommitted, an
23
22
  # email invitation will be sent to the person
24
- def self.add(client_id, email_address, name, access_level, password)
23
+ def self.add(auth, client_id, email_address, name, access_level, password)
25
24
  options = { :body => {
26
25
  :EmailAddress => email_address,
27
26
  :Name => name,
28
27
  :AccessLevel => access_level,
29
28
  :Password => password }.to_json }
30
- response = CreateSend.post "/clients/#{client_id}/people.json", options
29
+ cs = CreateSend.new auth
30
+ response = cs.post "/clients/#{client_id}/people.json", options
31
31
  Hashie::Mash.new(response)
32
32
  end
33
33
 
@@ -41,7 +41,7 @@ module CreateSend
41
41
  :Name => name,
42
42
  :AccessLevel => access_level,
43
43
  :Password => password }.to_json }
44
- CreateSend.put uri_for(client_id), options
44
+ put uri_for(client_id), options
45
45
  # Update @email_address, so this object can continue to be used reliably
46
46
  @email_address = new_email_address
47
47
  end
@@ -49,11 +49,11 @@ module CreateSend
49
49
  # deletes this person from the client
50
50
  def delete
51
51
  options = { :query => { :email => @email_address } }
52
- CreateSend.delete uri_for(client_id), options
52
+ super uri_for(client_id), options
53
53
  end
54
54
 
55
55
  def uri_for(client_id)
56
- "/clients/#{client_id}/people.json"
56
+ "/clients/#{client_id}/people.json"
57
57
  end
58
58
  end
59
59
  end
@@ -1,21 +1,20 @@
1
- require 'createsend'
2
- require 'json'
3
-
4
1
  module CreateSend
5
2
  # Represents a subscriber list segment and associated functionality.
6
- class Segment
3
+ class Segment < CreateSend
7
4
  attr_reader :segment_id
8
5
 
9
- def initialize(segment_id)
6
+ def initialize(auth, segment_id)
10
7
  @segment_id = segment_id
8
+ super
11
9
  end
12
10
 
13
11
  # Creates a new segment.
14
- def self.create(list_id, title, rules)
12
+ def self.create(auth, list_id, title, rules)
15
13
  options = { :body => {
16
14
  :Title => title,
17
15
  :Rules => rules }.to_json }
18
- response = CreateSend.post "/segments/#{list_id}.json", options
16
+ cs = CreateSend.new auth
17
+ response = cs.post "/segments/#{list_id}.json", options
19
18
  response.parsed_response
20
19
  end
21
20
 
@@ -24,7 +23,7 @@ module CreateSend
24
23
  options = { :body => {
25
24
  :Title => title,
26
25
  :Rules => rules }.to_json }
27
- response = CreateSend.put "/segments/#{segment_id}.json", options
26
+ response = cs_put "/segments/#{segment_id}.json", options
28
27
  end
29
28
 
30
29
  # Adds a rule to this segment.
@@ -32,7 +31,7 @@ module CreateSend
32
31
  options = { :body => {
33
32
  :Subject => subject,
34
33
  :Clauses => clauses }.to_json }
35
- response = CreateSend.post "/segments/#{segment_id}/rules.json", options
34
+ response = post "rules", options
36
35
  end
37
36
 
38
37
  # Gets the active subscribers in this segment.
@@ -50,32 +49,32 @@ module CreateSend
50
49
 
51
50
  # Gets the details of this segment
52
51
  def details
53
- response = CreateSend.get "/segments/#{segment_id}.json", {}
52
+ response = cs_get "/segments/#{segment_id}.json", {}
54
53
  Hashie::Mash.new(response)
55
54
  end
56
55
 
57
56
  # Clears all rules of this segment.
58
57
  def clear_rules
59
- response = CreateSend.delete "/segments/#{segment_id}/rules.json", {}
58
+ response = cs_delete "/segments/#{segment_id}/rules.json", {}
60
59
  end
61
60
 
62
61
  # Deletes this segment.
63
62
  def delete
64
- response = CreateSend.delete "/segments/#{segment_id}.json", {}
63
+ response = super "/segments/#{segment_id}.json", {}
65
64
  end
66
65
 
67
66
  private
68
67
 
69
68
  def get(action, options = {})
70
- CreateSend.get uri_for(action), options
69
+ super uri_for(action), options
71
70
  end
72
71
 
73
72
  def post(action, options = {})
74
- CreateSend.post uri_for(action), options
73
+ super uri_for(action), options
75
74
  end
76
75
 
77
76
  def put(action, options = {})
78
- CreateSend.put uri_for(action), options
77
+ super uri_for(action), options
79
78
  end
80
79
 
81
80
  def uri_for(action)
@@ -1,26 +1,25 @@
1
- require 'createsend'
2
- require 'json'
3
-
4
1
  module CreateSend
5
2
  # Represents a subscriber and associated functionality.
6
- class Subscriber
3
+ class Subscriber < CreateSend
7
4
  attr_reader :list_id
8
5
  attr_reader :email_address
9
6
 
10
- def initialize(list_id, email_address)
7
+ def initialize(auth, list_id, email_address)
11
8
  @list_id = list_id
12
9
  @email_address = email_address
10
+ super
13
11
  end
14
12
 
15
13
  # Gets a subscriber by list ID and email address.
16
- def self.get(list_id, email_address)
14
+ def self.get(auth, list_id, email_address)
17
15
  options = { :query => { :email => email_address } }
18
- response = CreateSend.get "/subscribers/#{list_id}.json", options
16
+ cs = CreateSend.new auth
17
+ response = cs.get "/subscribers/#{list_id}.json", options
19
18
  Hashie::Mash.new(response)
20
19
  end
21
20
 
22
21
  # Adds a subscriber to a subscriber list.
23
- def self.add(list_id, email_address, name, custom_fields, resubscribe,
22
+ def self.add(auth, list_id, email_address, name, custom_fields, resubscribe,
24
23
  restart_subscription_based_autoresponders=false)
25
24
  options = { :body => {
26
25
  :EmailAddress => email_address,
@@ -29,12 +28,13 @@ module CreateSend
29
28
  :Resubscribe => resubscribe,
30
29
  :RestartSubscriptionBasedAutoresponders =>
31
30
  restart_subscription_based_autoresponders }.to_json }
32
- response = CreateSend.post "/subscribers/#{list_id}.json", options
31
+ cs = CreateSend.new auth
32
+ response = cs.post "/subscribers/#{list_id}.json", options
33
33
  response.parsed_response
34
34
  end
35
35
 
36
36
  # Imports subscribers into a subscriber list.
37
- def self.import(list_id, subscribers, resubscribe,
37
+ def self.import(auth, list_id, subscribers, resubscribe,
38
38
  queue_subscription_based_autoresponders=false,
39
39
  restart_subscription_based_autoresponders=false)
40
40
  options = { :body => {
@@ -45,7 +45,8 @@ module CreateSend
45
45
  :RestartSubscriptionBasedAutoresponders =>
46
46
  restart_subscription_based_autoresponders }.to_json }
47
47
  begin
48
- response = CreateSend.post(
48
+ cs = CreateSend.new auth
49
+ response = cs.post(
49
50
  "/subscribers/#{list_id}/import.json", options)
50
51
  rescue BadRequest => br
51
52
  # Subscriber import will throw BadRequest if some subscribers are not
@@ -75,7 +76,7 @@ module CreateSend
75
76
  :Resubscribe => resubscribe,
76
77
  :RestartSubscriptionBasedAutoresponders =>
77
78
  restart_subscription_based_autoresponders }.to_json }
78
- CreateSend.put "/subscribers/#{@list_id}.json", options
79
+ put "/subscribers/#{@list_id}.json", options
79
80
  # Update @email_address, so this object can continue to be used reliably
80
81
  @email_address = new_email_address
81
82
  end
@@ -84,20 +85,20 @@ module CreateSend
84
85
  def unsubscribe
85
86
  options = { :body => {
86
87
  :EmailAddress => @email_address }.to_json }
87
- CreateSend.post "/subscribers/#{@list_id}/unsubscribe.json", options
88
+ post "/subscribers/#{@list_id}/unsubscribe.json", options
88
89
  end
89
90
 
90
91
  # Gets the historical record of this subscriber's trackable actions.
91
92
  def history
92
93
  options = { :query => { :email => @email_address } }
93
- response = CreateSend.get "/subscribers/#{@list_id}/history.json", options
94
+ response = cs_get "/subscribers/#{@list_id}/history.json", options
94
95
  response.map{|item| Hashie::Mash.new(item)}
95
96
  end
96
97
 
97
98
  # Moves this subscriber to the Deleted state in the associated list.
98
99
  def delete
99
100
  options = { :query => { :email => @email_address } }
100
- CreateSend.delete "/subscribers/#{@list_id}.json", options
101
+ super "/subscribers/#{@list_id}.json", options
101
102
  end
102
103
  end
103
104
  end
@@ -1,28 +1,27 @@
1
- require 'createsend'
2
- require 'json'
3
-
4
1
  module CreateSend
5
2
  # Represents an email template and associated functionality.
6
- class Template
3
+ class Template < CreateSend
7
4
  attr_reader :template_id
8
5
 
9
- def initialize(template_id)
6
+ def initialize(auth, template_id)
10
7
  @template_id = template_id
8
+ super
11
9
  end
12
10
 
13
11
  # Creates a new email template.
14
- def self.create(client_id, name, html_url, zip_url)
12
+ def self.create(auth, client_id, name, html_url, zip_url)
15
13
  options = { :body => {
16
14
  :Name => name,
17
15
  :HtmlPageURL => html_url,
18
16
  :ZipFileURL => zip_url }.to_json }
19
- response = CreateSend.post "/templates/#{client_id}.json", options
17
+ cs = CreateSend.new auth
18
+ response = cs.post "/templates/#{client_id}.json", options
20
19
  response.parsed_response
21
20
  end
22
21
 
23
22
  # Gets the details of this email template.
24
23
  def details
25
- response = CreateSend.get "/templates/#{template_id}.json", {}
24
+ response = get "/templates/#{template_id}.json", {}
26
25
  Hashie::Mash.new(response)
27
26
  end
28
27
 
@@ -32,12 +31,12 @@ module CreateSend
32
31
  :Name => name,
33
32
  :HtmlPageURL => html_url,
34
33
  :ZipFileURL => zip_url }.to_json }
35
- response = CreateSend.put "/templates/#{template_id}.json", options
34
+ response = put "/templates/#{template_id}.json", options
36
35
  end
37
36
 
38
37
  # Deletes this email template.
39
38
  def delete
40
- response = CreateSend.delete "/templates/#{template_id}.json", {}
39
+ response = super "/templates/#{template_id}.json", {}
41
40
  end
42
41
  end
43
42
  end