fuelsdk 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,7 @@ require 'date'
5
5
  require 'jwt'
6
6
 
7
7
  module FuelSDK
8
+ require 'fuelsdk/utils'
8
9
  autoload :HTTPRequest, 'fuelsdk/http_request'
9
10
  autoload :Targeting, 'fuelsdk/targeting'
10
11
  autoload :Soap, 'fuelsdk/soap'
@@ -18,6 +19,7 @@ ET_Client = FuelSDK::Client
18
19
  ET_BounceEvent = FuelSDK::BounceEvent
19
20
  ET_ClickEvent = FuelSDK::ClickEvent
20
21
  ET_ContentArea = FuelSDK::ContentArea
22
+ ET_DataExtension = FuelSDK::DataExtension
21
23
  ET_DataFolder = FuelSDK::DataFolder
22
24
  ET_Folder = FuelSDK::Folder
23
25
  ET_Email = FuelSDK::Email
@@ -28,227 +30,3 @@ ET_Subscriber = FuelSDK::Subscriber
28
30
  ET_UnsubEvent = FuelSDK::UnsubEvent
29
31
  ET_TriggeredSend = FuelSDK::TriggeredSend
30
32
  ET_Campaign = FuelSDK::Campaign
31
-
32
- =begin
33
- class ET_DataExtension < ET_CUDSupport
34
- attr_accessor :columns
35
-
36
- def initialize
37
- super
38
- @obj = 'DataExtension'
39
- end
40
-
41
- def post
42
- originalProps = @props
43
-
44
- if @props.is_a? Array then
45
- multiDE = []
46
- @props.each { |currentDE|
47
- currentDE['Fields'] = {}
48
- currentDE['Fields']['Field'] = []
49
- currentDE['columns'].each { |key|
50
- currentDE['Fields']['Field'].push(key)
51
- }
52
- currentDE.delete('columns')
53
- multiDE.push(currentDE.dup)
54
- }
55
-
56
- @props = multiDE
57
- else
58
- @props['Fields'] = {}
59
- @props['Fields']['Field'] = []
60
-
61
- @columns.each { |key|
62
- @props['Fields']['Field'].push(key)
63
- }
64
- end
65
-
66
- obj = super
67
- @props = originalProps
68
- return obj
69
- end
70
-
71
- def patch
72
- @props['Fields'] = {}
73
- @props['Fields']['Field'] = []
74
- @columns.each { |key|
75
- @props['Fields']['Field'].push(key)
76
- }
77
- obj = super
78
- @props.delete("Fields")
79
- return obj
80
- end
81
-
82
- class Column < ET_GetSupport
83
- def initialize
84
- super
85
- @obj = 'DataExtensionField'
86
- end
87
-
88
- def get
89
-
90
- if props and props.is_a? Array then
91
- @props = props
92
- end
93
-
94
- if @props and @props.is_a? Hash then
95
- @props = @props.keys
96
- end
97
-
98
- if filter and filter.is_a? Hash then
99
- @filter = filter
100
- end
101
-
102
- fixCustomerKey = false
103
- if filter and filter.is_a? Hash then
104
- @filter = filter
105
- if @filter.has_key?("Property") && @filter["Property"] == "CustomerKey" then
106
- @filter["Property"] = "DataExtension.CustomerKey"
107
- fixCustomerKey = true
108
- end
109
- end
110
-
111
- obj = ET_Get.new(@authStub, @obj, @props, @filter)
112
- @lastRequestID = obj.request_id
113
-
114
- if fixCustomerKey then
115
- @filter["Property"] = "CustomerKey"
116
- end
117
-
118
- return obj
119
- end
120
- end
121
-
122
- class Row < ET_CUDSupport
123
- attr_accessor :Name, :CustomerKey
124
-
125
- def initialize()
126
- super
127
- @obj = "DataExtensionObject"
128
- end
129
-
130
- def get
131
- getName
132
- if props and props.is_a? Array then
133
- @props = props
134
- end
135
-
136
- if @props and @props.is_a? Hash then
137
- @props = @props.keys
138
- end
139
-
140
- if filter and filter.is_a? Hash then
141
- @filter = filter
142
- end
143
-
144
- obj = ET_Get.new(@authStub, "DataExtensionObject[#{@Name}]", @props, @filter)
145
- @lastRequestID = obj.request_id
146
-
147
- return obj
148
- end
149
-
150
- def post
151
- getCustomerKey
152
- originalProps = @props
153
- ## FIX THIS
154
- if @props.is_a? Array then
155
- # multiRow = []
156
- # @props.each { |currentDE|
157
-
158
- # currentDE['columns'].each { |key|
159
- # currentDE['Fields'] = {}
160
- # currentDE['Fields']['Field'] = []
161
- # currentDE['Fields']['Field'].push(key)
162
- # }
163
- # currentDE.delete('columns')
164
- # multiRow.push(currentDE.dup)
165
- # }
166
-
167
- # @props = multiRow
168
- else
169
- currentFields = []
170
- currentProp = {}
171
-
172
- @props.each { |key,value|
173
- currentFields.push({"Name" => key, "Value" => value})
174
- }
175
- currentProp['CustomerKey'] = @CustomerKey
176
- currentProp['Properties'] = {}
177
- currentProp['Properties']['Property'] = currentFields
178
- end
179
-
180
- obj = ET_Post.new(@authStub, @obj, currentProp)
181
- @props = originalProps
182
- obj
183
- end
184
-
185
- def patch
186
- getCustomerKey
187
- currentFields = []
188
- currentProp = {}
189
-
190
- @props.each { |key,value|
191
- currentFields.push({"Name" => key, "Value" => value})
192
- }
193
- currentProp['CustomerKey'] = @CustomerKey
194
- currentProp['Properties'] = {}
195
- currentProp['Properties']['Property'] = currentFields
196
-
197
- ET_Patch.new(@authStub, @obj, currentProp)
198
- end
199
- def delete
200
- getCustomerKey
201
- currentFields = []
202
- currentProp = {}
203
-
204
- @props.each { |key,value|
205
- currentFields.push({"Name" => key, "Value" => value})
206
- }
207
- currentProp['CustomerKey'] = @CustomerKey
208
- currentProp['Keys'] = {}
209
- currentProp['Keys']['Key'] = currentFields
210
-
211
- ET_Delete.new(@authStub, @obj, currentProp)
212
- end
213
-
214
- private
215
- def getCustomerKey
216
- if @CustomerKey.nil? then
217
- if @CustomerKey.nil? && @Name.nil? then
218
- raise 'Unable to process DataExtension::Row request due to CustomerKey and Name not being defined on ET_DatExtension::row'
219
- else
220
- de = ET_DataExtension.new
221
- de.authStub = @authStub
222
- de.props = ["Name","CustomerKey"]
223
- de.filter = {'Property' => 'CustomerKey','SimpleOperator' => 'equals','Value' => @Name}
224
- getResponse = de.get
225
- if getResponse.status && (getResponse.results.length == 1) then
226
- @CustomerKey = getResponse.results[0][:customer_key]
227
- else
228
- raise 'Unable to process DataExtension::Row request due to unable to find DataExtension based on Name'
229
- end
230
- end
231
- end
232
- end
233
-
234
- def getName
235
- if @Name.nil? then
236
- if @CustomerKey.nil? && @Name.nil? then
237
- raise 'Unable to process DataExtension::Row request due to CustomerKey and Name not being defined on ET_DatExtension::row'
238
- else
239
- de = ET_DataExtension.new
240
- de.authStub = @authStub
241
- de.props = ["Name","CustomerKey"]
242
- de.filter = {'Property' => 'CustomerKey','SimpleOperator' => 'equals','Value' => @CustomerKey}
243
- getResponse = de.get
244
- if getResponse.status && (getResponse.results.length == 1) then
245
- @Name = getResponse.results[0][:name]
246
- else
247
- raise 'Unable to process DataExtension::Row request due to unable to find DataExtension based on CustomerKey'
248
- end
249
- end
250
- end
251
- end
252
- end
253
- end
254
- =end
@@ -100,42 +100,26 @@ module FuelSDK
100
100
  refresh true
101
101
  end
102
102
 
103
- #def AddSubscriberToList(emailAddress, listIDs, subscriberKey = nil)
104
- # newSub = FuelSDK::ET_Subscriber.new
105
- # newSub.authStub = self
106
- # lists = []
107
-
108
- # listIDs.each{ |p|
109
- # lists.push({"ID"=> p})
110
- # }
111
-
112
- # newSub.props = {"EmailAddress" => emailAddress, "Lists" => lists}
113
- # if !subscriberKey.nil? then
114
- # newSub.props['SubscriberKey'] = subscriberKey;
115
- # end
116
-
117
- # # Try to add the subscriber
118
- # postResponse = newSub.post
119
-
120
- # if postResponse.status == false then
121
- # # If the subscriber already exists in the account then we need to do an update.
122
- # # Update Subscriber On List
123
- # if postResponse.results[0][:error_code] == "12014" then
124
- # patchResponse = newSub.patch
125
- # return patchResponse
126
- # end
127
- # end
128
- # return postResponse
129
- #end
130
-
131
- #def CreateDataExtensions(dataExtensionDefinitions)
132
- # newDEs = FuelSDK::ET_DataExtension.new
133
- # newDEs.authStub = self
134
-
135
- # newDEs.props = dataExtensionDefinitions
136
- # postResponse = newDEs.post
137
-
138
- # return postResponse
139
- #end
103
+ def AddSubscriberToList(email, ids, subscriber_key = nil)
104
+ s = FuelSDK::Subscriber.new
105
+ s.client = self
106
+ lists = ids.collect{|id| {'ID' => id}}
107
+ s.properties = {"EmailAddress" => email, "Lists" => lists}
108
+ s.propertiess['SubscriberKey'] = subscriber_key if subscriber_key
109
+
110
+ # Try to add the subscriber
111
+ if(rsp = s.post and rsp.results.first[:error_code] == '12014')
112
+ # subscriber already exists we need to update.
113
+ rsp = s.patch
114
+ end
115
+ rsp
116
+ end
117
+
118
+ def CreateDataExtensions(definitions)
119
+ de = FuelSDK::DataExtension.new
120
+ de.client = self
121
+ de.properties = definitions
122
+ de.post
123
+ end
140
124
  end
141
125
  end
@@ -1,56 +1,66 @@
1
1
  module FuelSDK
2
2
  module Objects
3
- module SoapRead
4
- def get
5
- client.soap_get id, properties, filter
6
- end
3
+ module Soap
4
+ module Read
5
+ attr_accessor :filter
6
+ def get _id=nil
7
+ client.soap_get _id||id, properties, filter
8
+ end
7
9
 
8
- def info
9
- client.soap_describe id
10
+ def info
11
+ client.soap_describe id
12
+ end
10
13
  end
11
- end
12
14
 
13
- module SoapCUD #create, update, delete
14
- def post
15
- client.soap_post id, properties
16
- end
15
+ module CUD #create, update, delete
16
+ def post
17
+ client.soap_post id, properties
18
+ end
17
19
 
18
- def patch
19
- client.soap_patch id, properties
20
- end
20
+ def patch
21
+ client.soap_patch id, properties
22
+ end
21
23
 
22
- def delete
23
- client.soap_delete id, properties
24
+ def delete
25
+ client.soap_delete id, properties
26
+ end
24
27
  end
25
28
  end
26
29
 
27
- module RestRead
28
- def get
29
- client.rest_get id, properties
30
+ module Rest
31
+ module Read
32
+ def get
33
+ client.rest_get id, properties
34
+ end
30
35
  end
31
- end
32
36
 
33
- module RestCUD
34
- def post
35
- client.rest_post id, properties
36
- end
37
+ module CUD
38
+ def post
39
+ client.rest_post id, properties
40
+ end
37
41
 
38
- def patch
39
- client.rest_patch id, properties
40
- end
42
+ def patch
43
+ client.rest_patch id, properties
44
+ end
41
45
 
42
- def delete
43
- client.rest_delete id, properties
46
+ def delete
47
+ client.rest_delete id, properties
48
+ end
44
49
  end
45
50
  end
46
51
 
47
52
  class Base
48
- attr_accessor :filter, :properties, :client
53
+ attr_accessor :properties, :client
49
54
  attr_reader :id
50
55
 
51
56
  alias props= properties= # backward compatibility
52
57
  alias authStub= client= # backward compatibility
53
58
 
59
+ def properties
60
+ @properties = [@properties].compact unless @properties.kind_of? Array
61
+ @properties
62
+ end
63
+
54
64
  def id
55
65
  self.class.id
56
66
  end
@@ -64,21 +74,21 @@ module FuelSDK
64
74
  end
65
75
 
66
76
  class BounceEvent < Objects::Base
67
- include Objects::SoapRead
77
+ include Objects::Soap::Read
68
78
  end
69
79
 
70
80
  class ClickEvent < Objects::Base
71
- include Objects::SoapRead
81
+ include Objects::Soap::Read
72
82
  end
73
83
 
74
84
  class ContentArea < Objects::Base
75
- include Objects::SoapRead
76
- include Objects::SoapCUD
85
+ include Objects::Soap::Read
86
+ include Objects::Soap::CUD
77
87
  end
78
88
 
79
89
  class DataFolder < Objects::Base
80
- include Objects::SoapRead
81
- include Objects::SoapCUD
90
+ include Objects::Soap::Read
91
+ include Objects::Soap::CUD
82
92
  end
83
93
 
84
94
  class Folder < DataFolder
@@ -90,16 +100,16 @@ module FuelSDK
90
100
  end
91
101
 
92
102
  class Email < Objects::Base
93
- include Objects::SoapRead
94
- include Objects::SoapCUD
103
+ include Objects::Soap::Read
104
+ include Objects::Soap::CUD
95
105
  end
96
106
 
97
107
  class List < Objects::Base
98
- include Objects::SoapRead
99
- include Objects::SoapCUD
108
+ include Objects::Soap::Read
109
+ include Objects::Soap::CUD
100
110
 
101
111
  class Subscriber < Objects::Base
102
- include Objects::SoapRead
112
+ include Objects::Soap::Read
103
113
  def id
104
114
  'ListSubscriber'
105
115
  end
@@ -107,26 +117,26 @@ module FuelSDK
107
117
  end
108
118
 
109
119
  class OpenEvent < Objects::Base
110
- include Objects::SoapRead
120
+ include Objects::Soap::Read
111
121
  end
112
122
 
113
123
  class SentEvent < Objects::Base
114
- include Objects::SoapRead
124
+ include Objects::Soap::Read
115
125
  end
116
126
 
117
127
  class Subscriber < Objects::Base
118
- include Objects::SoapRead
119
- include Objects::SoapCUD
128
+ include Objects::Soap::Read
129
+ include Objects::Soap::CUD
120
130
  end
121
131
 
122
132
  class UnsubEvent < Objects::Base
123
- include Objects::SoapRead
133
+ include Objects::Soap::Read
124
134
  end
125
135
 
126
136
  class TriggeredSend < Objects::Base
137
+ include Objects::Soap::Read
138
+ include Objects::Soap::CUD
127
139
  attr_accessor :subscribers
128
- include Objects::SoapRead
129
- include Objects::SoapCUD
130
140
  def id
131
141
  'TriggeredSendDefinition'
132
142
  end
@@ -135,9 +145,199 @@ module FuelSDK
135
145
  end
136
146
  end
137
147
 
148
+ class DataExtension < Objects::Base
149
+ include Objects::Soap::Read
150
+ include Objects::Soap::CUD
151
+ attr_accessor :fields
152
+ alias columns= fields= # backward compatibility
153
+
154
+ def post
155
+ munge_fields self.properties
156
+ super
157
+ end
158
+
159
+ def patch
160
+ munge_fields self.properties
161
+ super
162
+ end
163
+
164
+ class Column < Objects::Base
165
+ include Objects::Soap::Read
166
+ def id
167
+ 'DataExtensionField'
168
+ end
169
+ def get
170
+ if filter and filter.kind_of? Hash and \
171
+ filter.include? 'Property' and filter['Property'] == 'CustomerKey'
172
+ filter['Property'] = 'DataExtension.CustomerKey'
173
+ end
174
+ super
175
+ end
176
+ end
177
+
178
+ class Row < Objects::Base
179
+ include Objects::Soap::Read
180
+ include Objects::Soap::CUD
181
+
182
+ attr_accessor :name, :customer_key
183
+
184
+ # backward compatibility
185
+ alias Name= name=
186
+ alias CustomerKey= customer_key=
187
+
188
+ def id
189
+ 'DataExtensionObject'
190
+ end
191
+
192
+ def get
193
+ super "#{id}[#{name}]"
194
+ end
195
+
196
+ def name
197
+ unless @name
198
+ retrieve_required
199
+ end
200
+ @name
201
+ end
202
+
203
+ def customer_key
204
+ unless @customer_key
205
+ retrieve_required
206
+ end
207
+ @customer_key
208
+ end
209
+
210
+ def post
211
+ munge_properties self.properties
212
+ super
213
+ end
214
+
215
+ def patch
216
+ munge_properties self.properties
217
+ super
218
+ end
219
+
220
+ def delete
221
+ munge_keys self.properties
222
+ super
223
+ end
224
+
225
+ private
226
+ #::TODO::
227
+ # opportunity for meta programming here... but need to get this out the door
228
+ def munge_keys d
229
+ d.each do |o|
230
+
231
+ next if explicit_keys(o) && explicit_customer_key(o)
232
+
233
+ formatted = []
234
+ o['CustomerKey'] = customer_key unless explicit_customer_key o
235
+ unless explicit_properties(o)
236
+ o.each do |k, v|
237
+ next if k == 'CustomerKey'
238
+ formatted.concat FuelSDK.format_name_value_pairs k => v
239
+ o.delete k
240
+ end
241
+ o['Keys'] = {'Key' => formatted }
242
+ end
243
+ end
244
+ end
245
+
246
+ def explicit_keys h
247
+ h['Keys'] and h['Keys']['Key']
248
+ end
249
+
250
+ def munge_properties d
251
+ d.each do |o|
252
+
253
+ next if explicit_properties(o) && explicit_customer_key(o)
254
+
255
+ formatted = []
256
+ o['CustomerKey'] = customer_key unless explicit_customer_key o
257
+ unless explicit_properties(o)
258
+ o.each do |k, v|
259
+ next if k == 'CustomerKey'
260
+ formatted.concat FuelSDK.format_name_value_pairs k => v
261
+ o.delete k
262
+ end
263
+ o['Properties'] = {'Property' => formatted }
264
+ end
265
+ end
266
+ end
267
+
268
+ def explicit_properties h
269
+ h['Properties'] and h['Properties']['Property']
270
+ end
271
+
272
+ def explicit_customer_key h
273
+ h['CustomerKey']
274
+ end
275
+
276
+ def retrieve_required
277
+ # have to use instance variables so we don't recursivelly retrieve_required
278
+ if !@name && !@customer_key
279
+ raise 'Unable to process DataExtension::Row ' \
280
+ 'request due to missing CustomerKey and Name'
281
+ end
282
+ if !@name || !@customer_key
283
+ filter = {
284
+ 'Property' => @name.nil? ? 'CustomerKey' : 'Name',
285
+ 'SimpleOperator' => 'equals',
286
+ 'Value' => @customer_key || @name
287
+ }
288
+ rsp = client.soap_get 'DataExtension', ['Name', 'CustomerKey'], filter
289
+ if rsp.success? && rsp.results.count == 1
290
+ self.name = rsp.results.first[:name]
291
+ self.customer_key = rsp.results.first[:customer_key]
292
+ else
293
+ raise 'Unable to process DataExtension::Row'
294
+ end
295
+ end
296
+ end
297
+ end
298
+
299
+ private
300
+
301
+ def munge_fields d
302
+ # maybe one day will make it smart enough to zip properties and fields if count is same?
303
+ if d.kind_of? Array and d.count > 1 and (fields and !fields.empty?)
304
+ # we could map the field to all DataExtensions, but lets make user be explicit.
305
+ # if they are going to use fields attribute properties should
306
+ # be a single DataExtension Defined in a Hash
307
+ raise 'Unable to handle muliple DataExtension definitions and a field definition'
308
+ end
309
+
310
+ d.each do |de|
311
+
312
+ if (explicit_fields(de) and (de['columns'] || de['fields'] || has_fields)) or
313
+ (de['columns'] and (de['fields'] || has_fields)) or
314
+ (de['fields'] and has_fields)
315
+ raise 'Fields are defined in too many ways. Please only define once.' # ahhh what, to do...
316
+ end
317
+
318
+ # let users who chose, to define fields explicitly within the hash definition
319
+ next if explicit_fields de
320
+
321
+ de['Fields'] = {'Field' => de['columns'] || de['fields'] || fields}
322
+ # sanitize
323
+ de.delete 'columns'
324
+ de.delete 'fields'
325
+ raise 'DataExtension needs atleast one field.' unless de['Fields']['Field']
326
+ end
327
+ end
328
+
329
+ def explicit_fields h
330
+ h['Fields'] and h['Fields']['Field']
331
+ end
332
+
333
+ def has_fields
334
+ fields and !fields.empty?
335
+ end
336
+ end
337
+
138
338
  class Campaign < Objects::Base
139
- include Objects::RestRead
140
- include Objects::RestCUD
339
+ include Objects::Rest::Read
340
+ include Objects::Rest::CUD
141
341
 
142
342
  def properties
143
343
  @properties ||= {}
@@ -150,8 +350,8 @@ module FuelSDK
150
350
  end
151
351
 
152
352
  class Asset < Objects::Base
153
- include Objects::RestRead
154
- include Objects::RestCUD
353
+ include Objects::Rest::Read
354
+ include Objects::Rest::CUD
155
355
 
156
356
  def properties
157
357
  @properties ||= {}