Class: FuelSDK::Client

Inherits:
Object
  • Object
show all
Includes:
Rest, Soap
Defined in:
lib/new.rb,
lib/fuelsdk/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 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/fuelsdk/client.rb', line 79

def access_token
  @access_token
end

#auth_tokenObject

Returns the value of attribute auth_token.



79
80
81
# File 'lib/fuelsdk/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/fuelsdk/client.rb', line 79

def auth_token_expiration
  @auth_token_expiration
end

#debugObject

Returns the value of attribute debug.



79
80
81
# File 'lib/fuelsdk/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/fuelsdk/client.rb', line 79

def internal_token
  @internal_token
end

#package_foldersObject

Returns the value of attribute package_folders.



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

def package_folders
  @package_folders
end

#package_nameObject

Returns the value of attribute package_name.



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

def package_name
  @package_name
end

#parent_foldersObject

Returns the value of attribute parent_folders.



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

def parent_folders
  @parent_folders
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



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

def refresh_token
  @refresh_token
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

Instance Method Details

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fuelsdk/client.rb', line 147

def AddSubscriberToList(email, ids, subscriber_key = nil)
	s = FuelSDK::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



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fuelsdk/client.rb', line 241

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"] = {"ObjectID"=>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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/fuelsdk/client.rb', line 219

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



274
275
276
277
278
279
280
# File 'lib/fuelsdk/client.rb', line 274

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

#CreateDataExtensions(definitions) ⇒ Object



163
164
165
166
167
168
# File 'lib/fuelsdk/client.rb', line 163

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

#CreateProfileAttributes(allAttributes) ⇒ Object



267
268
269
270
271
272
# File 'lib/fuelsdk/client.rb', line 267

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

#jwt=(encoded_jwt) ⇒ Object



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

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fuelsdk/client.rb', line 112

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("https://auth.exacttargetapis.com/v1/requestToken", 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



143
144
145
# File 'lib/fuelsdk/client.rb', line 143

def refresh!
	refresh true
end

#SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassficationCustomerKey) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fuelsdk/client.rb', line 199

def SendEmailToDataExtension(emailID, sendableDataExtensionCustomerKey, sendClassficationCustomerKey)
	email = ET_Email::SendDefinition.new 
	email.properties = {"Name"=>SecureRandom.uuid, "CustomerKey"=>SecureRandom.uuid, "Description"=>"Created with RubySDK"} 
	email.properties["SendClassification"] = {"CustomerKey"=> sendClassficationCustomerKey}
	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, sendClassficationCustomerKey) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fuelsdk/client.rb', line 178

def SendEmailToList(emailID, listID, sendClassficationCustomerKey)
	email = ET_Email::SendDefinition.new 
	email.properties = {"Name"=>SecureRandom.uuid, "CustomerKey"=>SecureRandom.uuid, "Description"=>"Created with RubySDK"} 
	email.properties["SendClassification"] = {"CustomerKey"=>sendClassficationCustomerKey}
	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



169
170
171
172
173
174
175
176
177
# File 'lib/fuelsdk/client.rb', line 169

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