Class: ResponsysClient

Inherits:
Object
  • Object
show all
Defined in:
lib/responsys_client.rb

Defined Under Namespace

Classes: MethodsNotSupportedError, ResponsysTimeoutError, TooManyMembersError

Constant Summary collapse

MAX_MEMBERS =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ ResponsysClient

Creates a client object to connect to Responsys via SOAP API

<username> - The login username <password> - The login password <options…> - Hash of additional options

:keep_alive => true|false - (Default=false) Keep session alive for multiple requests
:wiredump_dev => IO - Dump all messages (reply and responses) to IO


30
31
32
33
34
35
36
# File 'lib/responsys_client.rb', line 30

def initialize(username, password, options = {})
  @username = username
  @password = password
  @keep_alive = options[:keep_alive]
  @responsys_client = ResponsysWS.new
  @responsys_client.wiredump_dev = options[:wiredump_dev] if options[:wiredump_dev]
end

Instance Attribute Details

#keep_aliveObject

Returns the value of attribute keep_alive.



20
21
22
# File 'lib/responsys_client.rb', line 20

def keep_alive
  @keep_alive
end

#session_idObject (readonly)

Returns the value of attribute session_id.



19
20
21
# File 'lib/responsys_client.rb', line 19

def session_id
  @session_id
end

Instance Method Details

#assign_sessionObject



49
50
51
52
53
# File 'lib/responsys_client.rb', line 49

def assign_session
  session_header_request = SessionHeader.new
  session_header_request.sessionId = @session_id
  @responsys_client.headerhandler.add session_header_request
end

#create_folder(folder_name) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/responsys_client.rb', line 70

def create_folder(folder_name)
  with_session do
    create_folder_request = CreateFolder.new
    create_folder_request.folderName = folder_name
    @responsys_client.createFolder create_folder_request
  end
end

#launch_campaign(folder_name, campaign_name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/responsys_client.rb', line 106

def launch_campaign(folder_name, campaign_name)
  with_session do
    launch_campaign = LaunchCampaign.new
    interact_object = InteractObject.new
    interact_object.folderName = folder_name
    interact_object.objectName = campaign_name
    launch_campaign.campaign = interact_object
    @responsys_client.launchCampaign launch_campaign
  end
end

#list_foldersObject



64
65
66
67
68
# File 'lib/responsys_client.rb', line 64

def list_folders
  with_session do
    @responsys_client.listFolders ListFolders.new
  end
end

#loginObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/responsys_client.rb', line 38

def 
  with_application_error do
     = Login.new
    .username = @username
    .password = @password
    response = @responsys_client. 
    @session_id = response.result.sessionId
    assign_session
  end
end

#logoutObject



55
56
57
58
59
60
61
62
# File 'lib/responsys_client.rb', line 55

def logout
  begin
    logout_request = Logout.new
    @responsys_client.logout logout_request
  ensure
    @session_id = nil
  end
end

#save_members(folder_name, list_name, members, attributes = Responsys::Member.fields) ⇒ Object



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
# File 'lib/responsys_client.rb', line 78

def save_members(folder_name, list_name, members, attributes = Responsys::Member.fields)
  raise MethodsNotSupportedError unless Responsys::Member.fields.include?(:email_address) && Responsys::Member.fields.include?(:email_permission_status) && Responsys::Member.fields.include?(:customer_id)
  raise TooManyMembersError if members.size > MAX_MEMBERS

  with_session do
    list_merge_rule = ListMergeRule.new
    list_merge_rule.insertOnNoMatch = true
    list_merge_rule.updateOnMatch = UpdateOnMatch::REPLACE_ALL
    list_merge_rule.matchColumnName1 = "CUSTOMER_ID_"
    record_data = RecordData.new
    record_data.fieldNames = Responsys::Member.responsys_fields(attributes)
    record_data.records = []
    members.each do |member|
      record = Record.new
      record = member.values(attributes)
      record_data.records << record
    end
    interact_object = InteractObject.new
    interact_object.folderName = folder_name
    interact_object.objectName = list_name
    merge_list_members = MergeListMembers.new
    merge_list_members.list = interact_object
    merge_list_members.recordData = record_data
    merge_list_members.mergeRule = list_merge_rule
    @responsys_client.mergeListMembers merge_list_members
  end
end

#trigger_campaign(folder_name, campaign_name, email, options = {}) ⇒ Object



117
118
119
# File 'lib/responsys_client.rb', line 117

def trigger_campaign(folder_name, campaign_name, email, options = {})
  trigger_user_campaign(campaign_name, {:email => email}, options)
end

#trigger_user_campaign(campaign_name, recipient_info, options = {}) ⇒ Object



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
# File 'lib/responsys_client.rb', line 121

def trigger_user_campaign(campaign_name, recipient_info, options = {})
  # Responsys requires something in the optional data for SOAP bindings to work
  options[:foo] = :bar if options.size == 0

  with_session do
    trigger_campaign_message = TriggerCampaignMessage.new
    recipient = Recipient.new
    recipient.emailAddress = recipient_info[:email] if recipient_info[:email]
    recipient.customerId = recipient_info[:id] if recipient_info[:id]
    recipient_data = RecipientData.new
    recipient_data.optionalData = []
    recipient_data.recipient = recipient
    options.each_pair do |k, v|
      optional_data = OptionalData.new
      optional_data.name = k
      optional_data.value = v
      recipient_data.optionalData << optional_data
    end
    interact_object = InteractObject.new
    interact_object.folderName = 'ignored'
    interact_object.objectName = campaign_name
    trigger_campaign_message.campaign = interact_object
    trigger_campaign_message.recipientData = recipient_data
    @responsys_client.triggerCampaignMessage trigger_campaign_message
  end
end

#with_sessionObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/responsys_client.rb', line 154

def with_session
  begin
    with_timeout do
       if @session_id.nil?
    end
    with_application_error do
      with_timeout do
        yield
      end
    end
  ensure
    with_timeout do
      logout unless @keep_alive
    end
  end
end

#with_timeoutObject



148
149
150
151
152
# File 'lib/responsys_client.rb', line 148

def with_timeout
  Timeout::timeout(60, ResponsysTimeoutError) do
    yield
  end
end