Skip to content

Commit 1260df6

Browse files
committed
Unsubscribe and resubscribe methods
Summary: Adding ubsubscribe and subscribe methods to Ruby library Reviewers: muhammad Differential Revision: http://phabricator.outbound.io/D553
1 parent 825a3f3 commit 1260df6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

lib/outbound.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Outbound
1616
ERROR_INIT = "Must call init() before identify() or track()."
1717
ERROR_TOKEN = "Token must be a string."
1818
ERROR_PLATFORM = "Unsupported platform specified."
19+
ERROR_CAMPAIGN_IDS = "At least one campaign ID is required."
1920

2021
@ob = nil
2122
@logger = Logger.new $stdout
@@ -70,6 +71,24 @@ def Outbound.register(platform, user_id, token)
7071
return @ob.register(platform, user_id, token)
7172
end
7273

74+
def Outbound.unsubscribe user_id, all=false, campaign_ids=nil
75+
if @ob == nil
76+
res = Result.new Outbound::ERROR_INIT, false
77+
@logger.error res.error
78+
return res
79+
end
80+
return @ob.subscription user_id, true, all, campaign_ids
81+
end
82+
83+
def Outbound.subscribe user_id, all=false, campaign_ids=nil
84+
if @ob == nil
85+
res = Result.new Outbound::ERROR_INIT, false
86+
@logger.error res.error
87+
return res
88+
end
89+
return @ob.subscription user_id, false, all, campaign_ids
90+
end
91+
7392
class Result
7493
include Defaults
7594

@@ -108,6 +127,10 @@ def token_error?
108127
def platform_error?
109128
return @error == Outbound::ERROR_PLATFORM
110129
end
130+
131+
def campaign_id_error?
132+
return @error == Outbound::ERROR_CAMPAIGN_IDS
133+
end
111134
end
112135

113136
class Client
@@ -209,6 +232,29 @@ def register(platform, user_id, token)
209232
return post(@api_key, "/#{platform}/register", {:token => token, :user_id => user_id})
210233
end
211234

235+
def subscription user_id, unsubscribe=false, all=false, campaign_ids=nil
236+
unless user_id.is_a? String or user_id.is_a? Numeric
237+
res = Result.new Outbound::ERROR_USER_ID, false
238+
@logger.error res.error
239+
return res
240+
end
241+
242+
if !all
243+
unless !campaign_ids.nil? && campaign_ids.is_a?(Array) && campaign_ids.length > 0
244+
res = Result.new Outbound::ERROR_CAMPAIGN_IDS, false
245+
@logger.error res.error
246+
return res
247+
end
248+
end
249+
250+
url = '/' + (unsubscribe ? 'unsubscribe' : 'subscribe') + '/' + (all ? 'all' : 'campaigns')
251+
data = {:user_id => user_id}
252+
if !all
253+
data[:campaign_ids] = campaign_ids
254+
end
255+
return post(@api_key, url, data)
256+
end
257+
212258
private
213259

214260
def post(api_key, path, data)

0 commit comments

Comments
 (0)