sklik-api 0.0.16 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -36,10 +36,34 @@ unless File.exists?(File.join(CONF_DIR, "access.rb"))
36
36
  end
37
37
 
38
38
  require 'rake/testtask'
39
- Rake::TestTask.new(:test) do |test|
40
- test.libs << 'lib' << 'test'
41
- test.pattern = 'test/unit/*.rb'
42
- test.verbose = true
39
+ Rake::TestTask.new(:test) do |t|
40
+ t.libs << 'lib' << 'test'
41
+ t.test_files = FileList['test/functional/**/*_test.rb', 'test/unit/**/*_test.rb','test/integration/**/*_test.rb']
42
+ t.warning = false
43
+ t.verbose = false
44
+ end
45
+
46
+ namespace :test do
47
+ Rake::TestTask.new(:unit) do |t|
48
+ t.libs << 'lib' << 'test'
49
+ t.test_files = FileList['test/unit/**/*_test.rb']
50
+ t.warning = false
51
+ t.verbose = false
52
+ end
53
+
54
+ Rake::TestTask.new(:functional) do |t|
55
+ t.libs << 'lib' << 'test'
56
+ t.test_files = FileList['test/functional/**/*_test.rb']
57
+ t.warning = false
58
+ t.verbose = false
59
+ end
60
+
61
+ Rake::TestTask.new(:integration) do |t|
62
+ t.libs << 'lib' << 'test'
63
+ t.test_files = FileList['test/integration/**/*_test.rb']
64
+ t.warning = false
65
+ t.verbose = false
66
+ end
43
67
  end
44
68
 
45
69
  require 'rcov/rcovtask'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.16
1
+ 0.1.0
@@ -9,7 +9,7 @@ class SklikApi
9
9
  :adSelection, :startDate, :endDate, :premiseId
10
10
  ]
11
11
 
12
- include Object
12
+ include SklikObject
13
13
  =begin
14
14
  Example of input hash
15
15
  {
@@ -57,14 +57,13 @@ Example of input hash
57
57
  #variable where are saved current data from system
58
58
  @campaign_data = nil
59
59
 
60
- #variable for storing errors
61
- @errors = []
60
+ @args = args
62
61
 
63
62
  #initialize adgroups
64
63
  @adgroups = []
65
64
  if args[:ad_groups] && args[:ad_groups].size > 0
66
65
  args[:ad_groups].each do |adgroup|
67
- @adgroups << SklikApi::Adgroup.new(self, adgroup)
66
+ @adgroups << SklikApi::Adgroup.new(adgroup.merge(:campaign => self))
68
67
  end
69
68
  end
70
69
 
@@ -72,27 +71,52 @@ Example of input hash
72
71
  super args
73
72
  end
74
73
 
75
- def errors
76
- @errors
74
+ def self.get id
75
+ return ArgumentError, "Please provide param (campaign id)" unless id
76
+ if campaign = super(NAME, id)
77
+ SklikApi::Campaign.new(process_sklik_data campaign)
78
+ else
79
+ nil
80
+ end
77
81
  end
78
82
 
79
83
  def self.find args = {}
80
84
  out = []
85
+
86
+ #asking fo campaign by ID
87
+ if args.is_a?(Integer)
88
+ return get args
89
+
90
+ #asking for campaign by hash with adgroup_id
91
+ elsif args.is_a?(Hash) && args[:campaign_id]
92
+ if campaign = get(args[:campaign_id])
93
+ return [campaign]
94
+ else
95
+ return []
96
+ end
97
+ end
98
+
81
99
  super(NAME, args[:customer_id]).each do |campaign|
82
- if (args[:campaign_id].nil? || (args[:campaign_id] && args[:campaign_id].to_i == campaign[:id].to_i)) && #find by campaign id
83
- (args[:status].nil? || (args[:status] && args[:status] == fix_status(campaign))) # find by status
84
- out << SklikApi::Campaign.new(
85
- :campaign_id => campaign[:id],
86
- :customer_id => args[:customer_id],
87
- :budget => campaign[:dayBudget].to_f/100.0,
88
- :name => campaign[:name],
89
- :status => fix_status(campaign)
90
- )
100
+ if (args[:status].nil? || (args[:status] && args[:status] == fix_status(campaign))) && # find by status
101
+ (args[:name].nil? || (args[:name] == campaign[:name]))
102
+ out << SklikApi::Campaign.new(process_sklik_data campaign)
91
103
  end
92
104
  end
93
105
  out
94
106
  end
95
107
 
108
+ def self.process_sklik_data campaign = {}
109
+ {
110
+ :campaign_id => campaign[:id],
111
+ :customer_id => campaign[:userId],
112
+ :budget => campaign[:dayBudget].to_f/100.0,
113
+ :name => campaign[:name],
114
+ :status => fix_status(campaign),
115
+ :excluded_search_services => campaign[:excludedSearchServices],
116
+ :network_setting=> {:content=>campaign[:context], :search=>true}
117
+ }
118
+ end
119
+
96
120
  def self.list_search_services
97
121
  connection.call("listSearchServices") do |param|
98
122
  return param[:searchServices].collect{|c| c.symbolize_keys}
@@ -111,27 +135,22 @@ Example of input hash
111
135
  end
112
136
  end
113
137
 
114
- def status_for_update
115
- if @args[:status] == :running
116
- return "active"
117
- elsif @args[:status] == :paused
118
- return "suspend"
138
+ def adgroups
139
+ if @args[:campaign_id] && get_current_status == :stopped
140
+ SklikApi.log :error, "Campaign: #{@args[:campaign_id]} - Can't get adgroups for stopped Campaign!"
141
+ []
119
142
  else
120
- return nil
143
+ Adgroup.find(campaign_id: self.args[:campaign_id])
121
144
  end
122
145
  end
123
146
 
124
- def adgroups
125
- Adgroup.find(self)
126
- end
127
-
128
147
  def to_hash
129
148
  if @campaign_data
130
149
  @campaign_data
131
150
  else
132
151
  @campaign_data = @args
133
152
  if @args[:status] != :stopped
134
- @campaign_data[:ad_groups] = Adgroup.find(self).collect{|a| a.to_hash}
153
+ @campaign_data[:ad_groups] = Adgroup.find(campaign_id: self.args[:campaign_id]).collect{|a| a.to_hash}
135
154
  else
136
155
  @campaign_data[:ad_groups] = []
137
156
  end
@@ -146,17 +165,17 @@ Example of input hash
146
165
  out << @args[:campaign_id]
147
166
 
148
167
  #prepare campaign struct
149
- args = {}
150
- args[:name] = @args[:name] if @args[:name]
151
- args[:status] = status_for_update if status_for_update
152
- args[:dayBudget] = (@args[:budget] * 100).to_i if @args[:budget]
153
- args[:context] = @args[:network_setting][:context] ||= true if @args[:network_setting]
168
+ u_args = {}
169
+ u_args[:name] = @args[:name] if @args[:name]
170
+ u_args[:status] = status_for_update if status_for_update
171
+ u_args[:dayBudget] = (@args[:budget] * 100).to_i if @args[:budget]
172
+ u_args[:context] = @args[:network_setting][:content].nil? || @args[:network_setting][:content] if @args[:network_setting]
154
173
  ADDITIONAL_FIELDS.each do |add_info|
155
174
  field_name = add_info.to_s.underscore.to_sym
156
- args[add_info] = @args[field_name] if @args[field_name]
175
+ u_args[add_info] = @args[field_name] if @args[field_name]
157
176
  end
158
177
 
159
- out << args
178
+ out << u_args
160
179
 
161
180
  out
162
181
  end
@@ -165,16 +184,16 @@ Example of input hash
165
184
  out = []
166
185
 
167
186
  #prepare campaign struct
168
- args = {}
169
- args[:name] = @args[:name]
170
- args[:dayBudget] = (@args[:budget] * 100).to_i if @args[:budget]
171
- args[:context] = @args[:network_setting][:context] ||= true if @args[:network_setting]
187
+ c_args = {}
188
+ c_args[:name] = @args[:name]
189
+ c_args[:status] = status_for_update if status_for_update
190
+ c_args[:dayBudget] = (@args[:budget] * 100).to_i if @args[:budget]
191
+ c_args[:context] = @args[:network_setting][:content].nil? || @args[:network_setting][:content] if @args[:network_setting]
172
192
  ADDITIONAL_FIELDS.each do |add_info|
173
193
  field_name = add_info.to_s.underscore.to_sym
174
- args[add_info] = @args[field_name] if @args[field_name]
194
+ c_args[add_info] = @args[field_name] if @args[field_name]
175
195
  end
176
-
177
- out << args
196
+ out << c_args
178
197
 
179
198
  #add customer id on which account campaign should be created
180
199
  out << @customer_id if @customer_id
@@ -183,10 +202,8 @@ Example of input hash
183
202
 
184
203
  def self.get_current_status args = {}
185
204
  raise ArgumentError, "Campaign_id is required" unless args[:campaign_id]
186
- campaigns = self.find(args)
187
- pp campaigns
188
- if campaigns.size == 1
189
- campaigns.first.args[:status]
205
+ if campaign = self.get(args[:campaign_id])
206
+ campaign.args[:status]
190
207
  else
191
208
  raise ArgumentError, "Campaign by #{args.inspect} couldn't be found!"
192
209
  end
@@ -204,14 +221,22 @@ Example of input hash
204
221
  if args[:ad_groups] && args[:ad_groups].size > 0
205
222
  @adgroups_update = true
206
223
  args[:ad_groups].each do |adgroup|
207
- @adgroups << SklikApi::Adgroup.new(self, adgroup)
224
+ @adgroups << SklikApi::Adgroup.new(adgroup.merge(campaign: self))
208
225
  end
209
226
  end
210
227
 
211
228
  save
212
229
  end
213
230
 
231
+ def valid?
232
+ clear_errors
233
+ log_error "name is required" unless args[:name] && args[:name].size > 0
234
+ log_error "budget must be more than 1 CZK" unless args[:budget] && args[:budget] > 1.0
235
+ !errors.any?
236
+ end
237
+
214
238
  def save
239
+ clear_errors
215
240
  if @args[:campaign_id] #do update
216
241
  #get current status of campaign
217
242
  before_status = get_current_status
@@ -241,7 +266,9 @@ Example of input hash
241
266
  #adgroups to be created
242
267
  (@new_adgroups.keys - @saved_adgroups.keys).each do |k|
243
268
  puts "creating new adgroup: #{@new_adgroups[k].args[:name]}"
244
- @new_adgroups[k].save
269
+ unless @new_adgroups[k].save
270
+ log_error({"Creation of: #{@new_adgroups[k].args[:name]}"=> @new_adgroups[k].errors})
271
+ end
245
272
  end
246
273
 
247
274
  #check status to be running
@@ -251,47 +278,65 @@ Example of input hash
251
278
  @saved_adgroups[k].restore
252
279
  end
253
280
  puts "updating adgroup: #{@saved_adgroups[k].args[:name]}"
254
- @saved_adgroups[k].update @new_adgroups[k]
281
+ unless @saved_adgroups[k].update @new_adgroups[k]
282
+ log_error({"Creation of: #{@saved_adgroups[k].args[:name]}"=> @saved_adgroups[k].errors})
283
+ end
255
284
  end
256
285
 
257
286
  end
258
287
 
259
288
  rescue Exception => e
260
- @errors << e.message
289
+ log_error e.message
261
290
  end
262
291
 
263
292
  #remove it if new status is stopped or status doesn't changed and before it was stopped
264
293
  remove if (@args[:status] == :stopped) || (@args[:status].nil? && before_status == :stopped)
265
294
 
266
- raise ArgumentError, "Problem with updating campaign datas #{@errors}" unless @errors.size == 0
267
-
268
- return true
269
295
  else #do save
270
296
  #create campaign
271
297
  begin
272
298
  create
273
299
  rescue Exception => e
274
- @errors << e.message
300
+ log_error e.message
301
+ #don't continue with creating campaign!
275
302
  return false
276
303
  end
277
304
 
278
- begin
279
- #create adgroups
280
- @adgroups.each{ |adgroup| adgroup.save }
281
-
282
- @campaign_data = @args
283
- raise ArgumentError, "Problem with creating campaign datas" unless @errors.size == 0
284
- return true
285
- rescue Exception => e
286
- @errors << e.message
287
- #update name
288
- update :name => "#{@args[:name]} FAILED ON CREATION - #{Time.now.strftime("%Y.%m.%d %H:%M:%S")}"
289
- #remove campaign
290
- remove
291
- #return false because error occured
292
- return false
305
+ #create adgroups
306
+ unless @adgroups.all?{ |adgroup| adgroup.save }
307
+ return rollback!
293
308
  end
309
+
310
+ @campaign_data = @args
311
+
312
+ #remove campaign when it was started with stopped status!
313
+ remove if @args[:status] && @args[:status].to_s.to_sym == :stopped
314
+
294
315
  end
316
+
317
+ !errors.any?
318
+ end
319
+
320
+ def log_error message
321
+ errors << message
322
+ end
323
+
324
+ def rollback!
325
+ #don't rollback if it is disabled!
326
+ return false unless SklikApi.use_rollback?
327
+
328
+ #remember errors!
329
+ old_errors = errors
330
+
331
+ SklikApi.log :info, "Campaign: #{@args[:campaign_id]} - ROLLBACK!"
332
+ update :name => "#{@args[:name]} FAILED ON CREATION - #{Time.now.strftime("%Y.%m.%d %H:%M:%S")}"
333
+ #remove adgroup
334
+ remove
335
+
336
+ #return remembered errors!
337
+ @errors = old_errors
338
+ #don't continue with creating adgroup!
339
+ return false
295
340
  end
296
341
  end
297
342
  end