This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
sample-subscriber.rb
135 lines (116 loc) · 5.36 KB
/
sample-subscriber.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
require 'marketingcloudsdk'
require_relative 'sample_helper' # contains auth with credentials
begin
stubObj = MarketingCloudSDK::Client.new auth
# NOTE: These examples only work in accounts where the SubscriberKey functionality is not enabled
# SubscriberKey will need to be included in the props if that feature is enabled
SubscriberTestEmail = "RubySDKExample@jb.kevy.com"
# Create Subscriber
p '>>> Create Subscriber'
postSub = MarketingCloudSDK::Subscriber.new
postSub.authStub = stubObj
postSub.props = {"EmailAddress" => SubscriberTestEmail}
p '>>> Posting'
postResponse = postSub.post
p "Post Status: #{postResponse.success? ? 'Success' : 'Failure'}"
p 'Code: ' + postResponse.code.to_s
p 'Message: ' + postResponse.message.to_s
p 'Result Count: ' + postResponse.results.length.to_s
p 'Results: ' + postResponse.results.inspect
raise 'Failure creating subscriber' unless postResponse.success?
# Retrieve newly created Subscriber
p '>>> Retrieve newly created Subscriber'
getSub = MarketingCloudSDK::Subscriber.new()
getSub.authStub = stubObj
getSub.props = ["SubscriberKey", "EmailAddress", "Status"]
getSub.filter = {'Property' => 'SubscriberKey', 'SimpleOperator' => 'equals', 'Value' => SubscriberTestEmail}
getResponse = getSub.get
p "Retrieve Status: #{getResponse.success? ? 'Success' : 'Failure'}"
p 'Code: ' + getResponse.code.to_s
p 'Message: ' + getResponse.message.to_s
p 'MoreResults: ' + getResponse.more?.to_s
p 'Results Length: ' + getResponse.results.length.to_s
p 'Results: ' + getResponse.results.to_s
raise 'Failure retrieving subscriber' unless getResponse.success?
# Update Subscriber
p '>>> Update Subscriber'
patchSub = MarketingCloudSDK::Subscriber.new
patchSub.authStub = stubObj
patchSub.props = {"EmailAddress" => SubscriberTestEmail, "Status" => "Unsubscribed"}
patchResponse = patchSub.patch
p 'Patch Status: ' + patchResponse.status.to_s
p 'Code: ' + patchResponse.code.to_s
p 'Message: ' + patchResponse.message.to_s
p 'Result Count: ' + patchResponse.results.length.to_s
p 'Results: ' + patchResponse.results.inspect
raise 'Failure updating subscriber' unless patchResponse.success?
# Retrieve Subscriber that should have status unsubscribed now
p '>>> Retrieve Subscriber that should have status unsubscribed now'
getSub = MarketingCloudSDK::Subscriber.new()
getSub.authStub = stubObj
getSub.props = ["SubscriberKey", "EmailAddress", "Status"]
getSub.filter = {'Property' => 'SubscriberKey','SimpleOperator' => 'equals','Value' => SubscriberTestEmail};
getResponse = getSub.get
p 'Retrieve Status: ' + getResponse.status.to_s
p 'Code: ' + getResponse.code.to_s
p 'Message: ' + getResponse.message.to_s
p 'MoreResults: ' + getResponse.more?.to_s
p 'Results Length: ' + getResponse.results.length.to_s
p 'Results: ' + getResponse.results.to_s
raise 'Failure retrieving subscriber' unless getResponse.success?
# Delete Subscriber
p '>>> Delete Subscriber'
deleteSub = MarketingCloudSDK::Subscriber.new()
deleteSub.authStub = stubObj
deleteSub.props = {"EmailAddress" => SubscriberTestEmail}
deleteResponse = deleteSub.delete
p 'Delete Status: ' + deleteResponse.status.to_s
p 'Code: ' + deleteResponse.code.to_s
p 'Message: ' + deleteResponse.message.to_s
p 'Results Length: ' + deleteResponse.results.length.to_s
p 'Results: ' + deleteResponse.results.to_s
raise 'Failure deleting subscriber' unless deleteResponse.success?
# Retrieve Subscriber to confirm deletion
p '>>> Retrieve Subscriber to confirm deletion'
getSub = MarketingCloudSDK::Subscriber.new()
getSub.authStub = stubObj
getSub.props = ["SubscriberKey", "EmailAddress", "Status"]
getSub.filter = {'Property' => 'SubscriberKey','SimpleOperator' => 'equals','Value' => SubscriberTestEmail};
getResponse = getSub.get
p 'Retrieve Status: ' + getResponse.status.to_s
p 'Code: ' + getResponse.code.to_s
p 'Message: ' + getResponse.message.to_s
p 'MoreResults: ' + getResponse.more?.to_s
p 'Results Length: ' + getResponse.results.length.to_s
p 'Results: ' + getResponse.results.to_s
raise 'Failure retrieving subscriber' unless getResponse.success?
=begin
# Do not run the "Retrieve All Subscribers" request for testing if you have more than 100,000 records in your account as it will take a long time to complete.
# Retrieve All Subcribers with GetMoreResults
p '>>> Retrieve All Subcribers with GetMoreResults'
getSub = MarketingCloudSDK::Subscriber.new()
getSub.authStub = stubObj
getSub.props = ["SubscriberKey", "EmailAddress", "Status"]
getResponse = getSub.get
p 'Retrieve Status: ' + getResponse.status.to_s
p 'Code: ' + getResponse.code.to_s
p 'Message: ' + getResponse.message.to_s
p 'MoreResults: ' + getResponse.more?.to_s
p 'RequestID: ' + getResponse.request_id.to_s
p 'Results Length: ' + getResponse.results.length.to_s
#p 'Results: ' + getResponse.results.to_s
while getResponse.more? do
p '>>> Continue Retrieve All Subcribers with GetMoreResults'
getResponse.continue
p 'Retrieve Status: ' + getResponse.status.to_s
p 'Code: ' + getResponse.code.to_s
p 'Message: ' + getResponse.message.to_s
p 'MoreResults: ' + getResponse.more?.to_s
p 'RequestID: ' + getResponse.request_id.to_s
p 'Results Length: ' + getResponse.results.length.to_s
end
=end
rescue => e
p "Caught exception: #{e.message}"
p e.backtrace
end