Class: MarketingCloudSDK::Client
- Inherits:
-
Object
- Object
- MarketingCloudSDK::Client
- Defined in:
- lib/new.rb,
lib/marketingcloudsdk/client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#auth_token ⇒ Object
Returns the value of attribute auth_token.
-
#auth_token_expiration ⇒ Object
Returns the value of attribute auth_token_expiration.
-
#base_api_url ⇒ Object
Returns the value of attribute base_api_url.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#id ⇒ Object
Returns the value of attribute id.
-
#internal_token ⇒ Object
Returns the value of attribute internal_token.
-
#package_folders ⇒ Object
Returns the value of attribute package_folders.
-
#package_name ⇒ Object
Returns the value of attribute package_name.
-
#parent_folders ⇒ Object
Returns the value of attribute parent_folders.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#request_token_url ⇒ Object
Returns the value of attribute request_token_url.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#signature ⇒ Object
Returns the value of attribute signature.
-
#soap_endpoint ⇒ Object
Returns the value of attribute soap_endpoint.
-
#use_oAuth2_authentication ⇒ Object
Returns the value of attribute use_oAuth2_authentication.
Attributes included from Targeting
Attributes included from Soap
Instance Method Summary collapse
- #AddSubscriberToList(email, ids, subscriber_key = nil) ⇒ Object
- #CreateAndStartDataExtensionImport(dataExtensionCustomerKey, fileName, overwrite) ⇒ Object
- #CreateAndStartListImport(listId, fileName) ⇒ Object
- #CreateContentAreas(arrayOfContentAreas) ⇒ Object
- #CreateDataExtensions(definitions) ⇒ Object
- #CreateProfileAttributes(allAttributes) ⇒ Object
-
#initialize(params = {}, debug = false) ⇒ Client
constructor
A new instance of Client.
- #jwt=(encoded_jwt) ⇒ Object
- #refresh(force = false) ⇒ Object
- #refresh! ⇒ Object
- #refreshWithOAuth2(force = false) ⇒ Object
- #SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassificationCustomerKey) ⇒ Object
- #SendEmailToList(emailID, listID, sendClassificationCustomerKey) ⇒ Object
- #SendTriggeredSends(arrayOfTriggeredRecords) ⇒ Object
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
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
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_token ⇒ Object
Returns the value of attribute access_token.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def access_token @access_token end |
#account_id ⇒ Object
Returns the value of attribute account_id.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def account_id @account_id end |
#auth_token ⇒ Object
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_expiration ⇒ Object
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_url ⇒ Object
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 |
#debug ⇒ Object
Returns the value of attribute debug.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def debug @debug end |
#id ⇒ Object
Returns the value of attribute id.
67 68 69 |
# File 'lib/new.rb', line 67 def id @id end |
#internal_token ⇒ Object
Returns the value of attribute internal_token.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def internal_token @internal_token end |
#package_folders ⇒ Object
Returns the value of attribute package_folders.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def package_folders @package_folders end |
#package_name ⇒ Object
Returns the value of attribute package_name.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def package_name @package_name end |
#parent_folders ⇒ Object
Returns the value of attribute parent_folders.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def parent_folders @parent_folders end |
#refresh_token ⇒ Object
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_url ⇒ Object
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 |
#scope ⇒ Object
Returns the value of attribute scope.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def scope @scope end |
#secret ⇒ Object
Returns the value of attribute secret.
67 68 69 |
# File 'lib/new.rb', line 67 def secret @secret end |
#signature ⇒ Object
Returns the value of attribute signature.
67 68 69 |
# File 'lib/new.rb', line 67 def signature @signature end |
#soap_endpoint ⇒ Object
Returns the value of attribute soap_endpoint.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def soap_endpoint @soap_endpoint end |
#use_oAuth2_authentication ⇒ Object
Returns the value of attribute use_oAuth2_authentication.
79 80 81 |
# File 'lib/marketingcloudsdk/client.rb', line 79 def use_oAuth2_authentication @use_oAuth2_authentication end |
Instance Method Details
#AddSubscriberToList(email, ids, subscriber_key = nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/marketingcloudsdk/client.rb', line 213 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
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/marketingcloudsdk/client.rb', line 307 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
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/marketingcloudsdk/client.rb', line 285 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
340 341 342 343 344 345 346 |
# File 'lib/marketingcloudsdk/client.rb', line 340 def CreateContentAreas(arrayOfContentAreas) postC = ET_ContentArea.new postC.authStub = self postC.properties = arrayOfContentAreas sendResponse = postC.post return sendResponse end |
#CreateDataExtensions(definitions) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/marketingcloudsdk/client.rb', line 229 def CreateDataExtensions(definitions) de = MarketingCloudSDK::DataExtension.new de.client = self de.properties = definitions de.post end |
#CreateProfileAttributes(allAttributes) ⇒ Object
333 334 335 336 337 338 |
# File 'lib/marketingcloudsdk/client.rb', line 333 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
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 |
# File 'lib/marketingcloudsdk/client.rb', line 134 def refresh force=false @refresh_mutex.synchronize do raise 'Require Client Id and Client Secret to refresh tokens' unless (id && secret) if (self.use_oAuth2_authentication == true) self.refreshWithOAuth2(force) return end #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 = Hash.new.tap do |h| h['data'] = payload h['content_type'] = 'application/json' h['params'] = {'legacy' => 1} end response = post(request_token_url, ) 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
209 210 211 |
# File 'lib/marketingcloudsdk/client.rb', line 209 def refresh! refresh true end |
#refreshWithOAuth2(force = false) ⇒ Object
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 |
# File 'lib/marketingcloudsdk/client.rb', line 171 def refreshWithOAuth2 force=false 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['client_id']= id h['client_secret'] = secret h['grant_type'] = 'client_credentials' if (not self.account_id.to_s.strip.empty?)then h['account_id'] = account_id end if (not self.scope.to_s.strip.empty?)then h['scope'] = scope end end = Hash.new.tap do |h| h['data'] = payload h['content_type'] = 'application/json' end self.request_token_url += '/v2/token' response = post(request_token_url, ) raise "Unable to refresh token: #{response['message']}" unless response.has_key?('access_token') self.access_token = response['access_token'] self.auth_token_expiration = Time.new + response['expires_in'] self.soap_endpoint = response['soap_instance_url'] + 'service.asmx' self.base_api_url = response['rest_instance_url'] return true else return false end end |
#SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassificationCustomerKey) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/marketingcloudsdk/client.rb', line 265 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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/marketingcloudsdk/client.rb', line 244 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
235 236 237 238 239 240 241 242 243 |
# File 'lib/marketingcloudsdk/client.rb', line 235 def SendTriggeredSends(arrayOfTriggeredRecords) sendTS = ET_TriggeredSend.new sendTS.authStub = self sendTS.properties = arrayOfTriggeredRecords sendResponse = sendTS.send return sendResponse end |