Class: MarketingCloudSDK::Client

Inherits:
Object
  • Object
show all
Includes:
Rest, Soap
Defined in:
lib/new.rb,
lib/marketingcloudsdk/client.rb

Direct Known Subclasses

RestClient, SoapClient

Instance Attribute Summary collapse

Attributes included from Targeting

#endpoint

Attributes included from Soap

#wsdl

Instance Method Summary collapse

Methods included from Rest

#complete_url, #get_url_properties, #normalize_keys, #parse_properties, #rest_client, #rest_delete, #rest_get, #rest_patch, #rest_post

Methods included from Targeting

#cache_file, #get_soap_endpoint_from_file, #set_soap_endpoint_to_file

Methods included from HTTPRequest

#generate_uri, #request

Methods included from Soap

#header, #soap_client, #soap_configure, #soap_delete, #soap_describe, #soap_get, #soap_patch, #soap_perform, #soap_post, #soap_put

Constructor Details

#initialize(params = {}, debug = false) ⇒ Client

Returns a new instance of Client.



69
70
71
72
73
74
# File 'lib/new.rb', line 69

def initialize params={}, debug=false
  @debug = debug
  @id = indifferent_access :clientid, params
  @secret = indifferent_access :clientsecret, params
  @signature = indifferent_access :appsignature, params
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def access_token
  @access_token
end

#auth_tokenObject

Returns the value of attribute auth_token.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def auth_token
  @auth_token
end

#auth_token_expirationObject

Returns the value of attribute auth_token_expiration.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def auth_token_expiration
  @auth_token_expiration
end

#base_api_urlObject

Returns the value of attribute base_api_url.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def base_api_url
  @base_api_url
end

#debugObject

Returns the value of attribute debug.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def debug
  @debug
end

#idObject

Returns the value of attribute id.



67
68
69
# File 'lib/new.rb', line 67

def id
  @id
end

#internal_tokenObject

Returns the value of attribute internal_token.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def internal_token
  @internal_token
end

#package_foldersObject

Returns the value of attribute package_folders.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def package_folders
  @package_folders
end

#package_nameObject

Returns the value of attribute package_name.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def package_name
  @package_name
end

#parent_foldersObject

Returns the value of attribute parent_folders.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def parent_folders
  @parent_folders
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def refresh_token
  @refresh_token
end

#request_token_urlObject

Returns the value of attribute request_token_url.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def request_token_url
  @request_token_url
end

#secretObject

Returns the value of attribute secret.



67
68
69
# File 'lib/new.rb', line 67

def secret
  @secret
end

#signatureObject

Returns the value of attribute signature.



67
68
69
# File 'lib/new.rb', line 67

def signature
  @signature
end

#soap_endpointObject

Returns the value of attribute soap_endpoint.



79
80
81
# File 'lib/marketingcloudsdk/client.rb', line 79

def soap_endpoint
  @soap_endpoint
end

Instance Method Details

#AddSubscriberToList(email, ids, subscriber_key = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/marketingcloudsdk/client.rb', line 161

def AddSubscriberToList(email, ids, subscriber_key = nil)
  s = MarketingCloudSDK::Subscriber.new
  s.client = self
  lists = ids.collect{|id| {'ID' => id}}
  s.properties = {"EmailAddress" => email, "Lists" => lists}
  p s.properties 
  s.properties['SubscriberKey'] = subscriber_key if subscriber_key

  # Try to add the subscriber
  if(rsp = s.post and rsp.results.first[:error_code] == '12014')
  # subscriber already exists we need to update.
  rsp = s.patch
  end
  rsp
end

#CreateAndStartDataExtensionImport(dataExtensionCustomerKey, fileName, overwrite) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/marketingcloudsdk/client.rb', line 255

def CreateAndStartDataExtensionImport(dataExtensionCustomerKey, fileName, overwrite)
  import = ET_Import.new 
  import.authStub = self
  import.properties = {"Name"=> "SDK Generated Import #{DateTime.now.to_s}"}
  import.properties["CustomerKey"] = SecureRandom.uuid
  import.properties["Description"] = "SDK Generated Import"
  import.properties["AllowErrors"] = "true"
  import.properties["DestinationObject"] = {"CustomerKey"=>dataExtensionCustomerKey}
  import.properties["FieldMappingType"] = "InferFromColumnHeadings"
  import.properties["FileSpec"] = fileName
  import.properties["FileType"] = "CSV"
  import.properties["RetrieveFileTransferLocation"] = {"CustomerKey"=>"ExactTarget Enhanced FTP"}
  if overwrite then
    import.properties["UpdateType"] = "Overwrite"
  else 
    import.properties["UpdateType"] = "AddAndUpdate"
  end 
  result = import.post
  
  if result.status then 
    return import.start 
  else
    raise "Unable to create import definition due to: #{result.results[0][:status_message]}"
  end 
end

#CreateAndStartListImport(listId, fileName) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/marketingcloudsdk/client.rb', line 233

def CreateAndStartListImport(listId,fileName)
  import = ET_Import.new 
  import.authStub = self
  import.properties = {"Name"=> "SDK Generated Import #{DateTime.now.to_s}"}
  import.properties["CustomerKey"] = SecureRandom.uuid
  import.properties["Description"] = "SDK Generated Import"
  import.properties["AllowErrors"] = "true"
  import.properties["DestinationObject"] = {"ID"=>listId}
  import.properties["FieldMappingType"] = "InferFromColumnHeadings"
  import.properties["FileSpec"] = fileName
  import.properties["FileType"] = "CSV"
  import.properties["RetrieveFileTransferLocation"] = {"CustomerKey"=>"ExactTarget Enhanced FTP"}
  import.properties["UpdateType"] = "AddAndUpdate"
  result = import.post
  
  if result.status then 
    return import.start 
  else
    raise "Unable to create import definition due to: #{result.results[0][:status_message]}"
  end 
end

#CreateContentAreas(arrayOfContentAreas) ⇒ Object



288
289
290
291
292
293
294
# File 'lib/marketingcloudsdk/client.rb', line 288

def CreateContentAreas(arrayOfContentAreas)
  postC = ET_ContentArea.new
  postC.authStub = self
  postC.properties = arrayOfContentAreas
  sendResponse = postC.post      
  return sendResponse
end

#CreateDataExtensions(definitions) ⇒ Object



177
178
179
180
181
182
# File 'lib/marketingcloudsdk/client.rb', line 177

def CreateDataExtensions(definitions)
  de = MarketingCloudSDK::DataExtension.new
  de.client = self
  de.properties = definitions
  de.post
end

#CreateProfileAttributes(allAttributes) ⇒ Object



281
282
283
284
285
286
# File 'lib/marketingcloudsdk/client.rb', line 281

def CreateProfileAttributes(allAttributes)
  attrs = ET_ProfileAttribute.new 
  attrs.authStub = self
  attrs.properties = allAttributes
  return attrs.post
end

#jwt=(encoded_jwt) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/marketingcloudsdk/client.rb', line 86

def jwt= encoded_jwt
  raise 'Require app signature to decode JWT' unless self.signature
  decoded_jwt = JWT.decode(encoded_jwt, self.signature, true)

  self.auth_token = decoded_jwt['request']['user']['oauthToken']
  self.internal_token = decoded_jwt['request']['user']['internalOauthToken']
  self.refresh_token = decoded_jwt['request']['user']['refreshToken']
  self.auth_token_expiration = Time.new + decoded_jwt['request']['user']['expiresIn']
  self.package_name = decoded_jwt['request']['application']['package']
end

#refresh(force = false) ⇒ Object



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
# File 'lib/marketingcloudsdk/client.rb', line 126

def refresh force=false
  @refresh_mutex.synchronize do
    raise 'Require Client Id and Client Secret to refresh tokens' unless (id && secret)
    #If we don't already have a token or the token expires within 5 min(300 seconds)
    if (self.access_token.nil? || Time.new + 300 > self.auth_token_expiration || force) then
    payload = Hash.new.tap do |h|
      h['clientId']= id
      h['clientSecret'] = secret
      h['refreshToken'] = refresh_token if refresh_token
      h['accessType'] = 'offline'
    end

    options = Hash.new.tap do |h|
      h['data'] = payload
      h['content_type'] = 'application/json'
      h['params'] = {'legacy' => 1}
    end
    response = post(request_token_url, options)
    raise "Unable to refresh token: #{response['message']}" unless response.has_key?('accessToken')

    self.access_token = response['accessToken']
    self.internal_token = response['legacyToken']
    self.auth_token_expiration = Time.new + response['expiresIn']
    self.refresh_token = response['refreshToken'] if response.has_key?("refreshToken")
    return true
    else 
    return false
    end
  end
end

#refresh!Object



157
158
159
# File 'lib/marketingcloudsdk/client.rb', line 157

def refresh!
  refresh true
end

#SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassificationCustomerKey) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/marketingcloudsdk/client.rb', line 213

def SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassificationCustomerKey)
  email = ET_Email::SendDefinition.new 
  email.properties = {"Name"=>SecureRandom.uuid, "CustomerKey"=>SecureRandom.uuid, "Description"=>"Created with RubySDK"} 
  email.properties["SendClassification"] = {"CustomerKey"=> sendClassificationCustomerKey}
  email.properties["SendDefinitionList"] = {"CustomerKey"=> sendableDataExtensionCustomerKey, "DataSourceTypeID"=>"CustomObject"}
  email.properties["Email"] = {"ID"=>emailID}
  email.authStub = self
  result = email.post
  if result.status then 
    sendresult = email.send 
    if sendresult.status then 
      deleteresult = email.delete
      return sendresult
    else 
      raise "Unable to send using send definition due to: #{result.results[0][:status_message]}"
    end 
  else
    raise "Unable to create send definition due to: #{result.results[0][:status_message]}"
  end 
end

#SendEmailToList(emailID, listID, sendClassificationCustomerKey) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/marketingcloudsdk/client.rb', line 192

def SendEmailToList(emailID, listID, sendClassificationCustomerKey)
  email = ET_Email::SendDefinition.new 
  email.properties = {"Name"=>SecureRandom.uuid, "CustomerKey"=>SecureRandom.uuid, "Description"=>"Created with RubySDK"} 
  email.properties["SendClassification"] = {"CustomerKey"=>sendClassificationCustomerKey}
  email.properties["SendDefinitionList"] = {"List"=> {"ID"=>listID}, "DataSourceTypeID"=>"List"}
  email.properties["Email"] = {"ID"=>emailID}
  email.authStub = self
  result = email.post
  if result.status then 
    sendresult = email.send 
    if sendresult.status then 
      deleteresult = email.delete
      return sendresult
    else 
      raise "Unable to send using send definition due to: #{result.results[0][:status_message]}"
    end 
  else
    raise "Unable to create send definition due to: #{result.results[0][:status_message]}"
  end 
end

#SendTriggeredSends(arrayOfTriggeredRecords) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/marketingcloudsdk/client.rb', line 183

def SendTriggeredSends(arrayOfTriggeredRecords)
  sendTS = ET_TriggeredSend.new
  sendTS.authStub = self
  
  sendTS.properties = arrayOfTriggeredRecords
  sendResponse = sendTS.send
  
  return sendResponse
end