This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
/
list.rb
217 lines (187 loc) · 6.4 KB
/
list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require 'soap/wsdlDriver'
class CampaignMonitor
# Provides access to the subscribers and info about subscribers
# associated with a Mailing List
class List < Base
include CampaignMonitor::Helpers
id_field "ListID"
name_field "Title"
VALID_ATTRIBUTES=%w{ConfirmOptIn UnsubscribePage ConfirmationSuccessPage ListID Title}
# Example
# @list = new List(12345)
def initialize(attrs={})
super
@attributes=attrs
end
# Example
#
# @list = @client.new_list.defaults
def defaults
defaults={"ConfirmOptIn" => "false",
"UnsubscribePage" => "",
"ConfirmationSuccessPage" => ""}
@attributes=defaults.merge(@attributes)
self
end
# AR like
def save
id ? Update : Create
end
# Loads a list by it's ID
#
# @list = List.GetDetail(1234)
def self.GetDetail(id)
list=self.new("ListID" => id)
list.GetDetail(true)
list.result.code == 101 ? nil : list
end
# loads a list by it's ID
#
# @list = List.GetDetail(1234)
def self.[](k)
GetDetail(k)
end
def GetDetail(overwrite=false)
@result=Result.new(cm_client.List_GetDetail("ListID" => id))
@attributes=@result.raw.merge(@attributes)
@attributes.merge!(@result.raw) if overwrite
@result.success?
end
def Update
# if we're dealing with a half baked object that Client#lists has given
# us then we need to popular all the fields before we can attempt an update
unless @fully_baked
self.GetDetail
@fully_baked=true
end
@result=Result.new(cm_client.List_Update(@attributes))
@result.success?
end
def Delete
@result=Result.new(cm_client.List_Delete("ListID" => id))
@result.success?
end
def Create
@result=Result.new(cm_client.List_Create(@attributes))
self.id = @result.content if @result.success?
@result.success?
end
# Example
# @list = new List(12345)
# result = @list.add_subscriber("ralph.wiggum@simpsons.net")
#
# if result.succeeded?
# puts "Added Subscriber"
# end
def add_subscriber(email, name=nil, custom_fields=nil)
if custom_fields.nil?
Result.new(cm_client.Subscriber_Add("ListID" => self.id, "Email" => email, "Name" => name))
else
add_subscriber_with_custom_fields(email, name, custom_fields)
end
end
def add_and_resubscribe(email, name=nil, custom_fields=nil)
if custom_fields.nil?
Result.new(cm_client.Subscriber_AddAndResubscribe("ListID" => self.id, "Email" => email, "Name" => name))
else
add_and_resubscribe_with_custom_fields(email, name, custom_fields)
end
end
# Example
# @list = new List(12345)
# result = @list.remove_subscriber("ralph.wiggum@simpsons.net")
#
# if result.succeeded?
# puts "Deleted Subscriber"
# end
def remove_subscriber(email)
Result.new(cm_client.Subscriber_Unsubscribe("ListID" => self.id, "Email" => email))
end
# email The subscriber's email address.
# name The subscriber's name.
# custom_fields A hash of field name => value pairs.
def add_subscriber_with_custom_fields(email, name, custom_fields)
response = cm_client.using_soap do |driver|
driver.addSubscriberWithCustomFields \
:ApiKey => cm_client.api_key,
:ListID => self.id,
:Email => email,
:Name => name,
:CustomFields => { :SubscriberCustomField => custom_fields_array(custom_fields) }
end
response.subscriber_AddWithCustomFieldsResult
end
# email The subscriber's email address.
# name The subscriber's name.
# custom_fields A hash of field name => value pairs.
def add_and_resubscribe_with_custom_fields(email, name, custom_fields)
response = cm_client.using_soap do |driver|
driver.addAndResubscribeWithCustomFields \
:ApiKey => cm_client.api_key,
:ListID => self.id,
:Email => email,
:Name => name,
:CustomFields => { :SubscriberCustomField => custom_fields_array(custom_fields) }
end
response.subscriber_AddAndResubscribeWithCustomFieldsResult
end
# Example
# current_date = DateTime.new
# @list = new List(12345)
# @subscribers = @list.active_subscribers(current_date)
#
# for subscriber in @subscribers
# puts subscriber.email
# end
def active_subscribers(date)
response = cm_client.Subscribers_GetActive('ListID' => self.id, 'Date' => formatted_timestamp(date))
handle_response(response) do
response['Subscriber'].collect{|s| Subscriber.new(s['EmailAddress'], s['Name'], s['Date'])}
end
end
# Example
# current_date = DateTime.new
# @list = new List(12345)
# @subscribers = @list.unsubscribed(current_date)
#
# for subscriber in @subscribers
# puts subscriber.email
# end
def unsubscribed(date)
date = formatted_timestamp(date) unless date.is_a?(String)
response = cm_client.Subscribers_GetUnsubscribed('ListID' => self.id, 'Date' => date)
handle_response(response) do
response['Subscriber'].collect{|s| Subscriber.new(s['EmailAddress'], s['Name'], s['Date'])}
end
end
# Example
# current_date = DateTime.new
# @list = new List(12345)
# @subscribers = @list.bounced(current_date)
#
# for subscriber in @subscribers
# puts subscriber.email
# end
def bounced(date)
response = cm_client.Subscribers_GetBounced('ListID' => self.id, 'Date' => formatted_timestamp(date))
handle_response(response) do
response["Subscriber"].collect{|s| Subscriber.new(s["EmailAddress"], s["Name"], s["Date"])}
end
end
protected
# Converts hash of custom field name/values to array of hashes for the SOAP API.
def custom_fields_array(custom_fields)
arr = []
custom_fields.each do |key, value|
if value.is_a? Array
value.each do |array_value|
arr << { "Key" => key, "Value" => array_value }
end
else
arr << { "Key" => key, "Value" => value }
end
end
arr
end
end
end