aboutyou-sdk 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,440 +1,422 @@
1
- require 'json'
2
-
3
1
  module AboutYou
4
2
  module SDK
5
3
  module QueryBuilder
6
4
 
7
- ###
8
- # @param string $searchword The prefix search word to search for.
9
- # @param int $limit Maximum number of results.
10
- # @param array $types Array of types to search for (Constants::TYPE_...).
11
- #
12
- # @return $this
13
- #
14
- # @throws \InvalidArgumentException
15
- ###
16
- def fetchAutocomplete(
17
- searchword,
18
- limit = nil,
19
- types = nil)
20
-
21
- if (!(searchword.is_a? String))
22
- raise 'InvalidArgumentException! searchword must be a string'
23
- end
24
-
25
- # strtolower is a workaround of ticket SAPI-532
26
- options = Hash[
27
- 'searchword' => searchword.downcase
28
- ]
29
-
30
- if (limit != nil)
31
- if (!(limit.is_a? Integer) && !limit[/\d/])
32
- raise 'InvalidArgumentException! limit must be an integer'
33
- end
34
- options['limit'] = Integer(limit);
35
- end
36
-
37
- if (!types.empty?)
38
- options['types'] = types;
39
- end
40
-
41
- self.query.push(Hash['autocompletion' => options])
42
-
43
- return self;
44
- end
45
-
46
- ###
47
- # @param string $sessionId Free to choose ID of the current website visitor.
48
- #
49
- # @return $this
50
- ###
51
- def fetchBasket(sessionId)
52
-
53
- checkSessionId(sessionId);
54
-
55
- self.query.push(Hash['basket' => Hash['session_id' => sessionId]])
56
-
57
- return self;
58
- end
59
-
60
- ###
61
- # @param string $sessionId Free to choose ID of the current website visitor.
62
- # @param int $productVariantId ID of product variant.
63
- # @param int $amount Amount of items to add.
64
- #
65
- #@return $this
66
- ###
67
- def addItemsToBasket(sessionId, items)
68
-
69
- checkSessionId(sessionId);
70
-
71
- orderLines = Array();
72
-
73
- items.each do ||item|
74
- orderLine = Hash['id' => item.getId(),'variant_id' => item.getVariantId()]
75
-
76
- if(item.getAdditionalData())
77
- orderLine['additional_data'] = item.getAdditionalData();
78
- end
79
-
80
- orderLines.push(orderLine)
81
- end
82
-
83
- self.query.push( Hash['basket' => Hash['session_id' => sessionId,'order_lines' => orderLines]])
84
-
85
- return self;
86
- end
87
-
88
- ###
89
- # @param string $sessionId Free to choose ID of the current website visitor.
90
- # @param Model\BasketItemSet[] $itemSets
91
- # @param int $amount Amount of items to add.
92
- #
93
- # @return $this
94
- ###
95
- def addItemSetsToBasket(sessionId, itemSets)
96
-
97
- checkSessionId(sessionId);
98
-
99
- orderLines = Array();
100
-
101
- itemSets.each do |itemSet|
102
- orderLine = Hash[
103
- 'id' => itemSet.getId(),
104
- 'set_items' => Array()
105
- ]
106
-
107
- if(itemSet.getAdditionalData())
108
- orderLine['additional_data'] = itemSet.getAdditionalData();
109
- end
110
-
111
-
112
- itemSet.getItems().each do |item|
113
- entry = Hash['variant_id' => item.getVariantId()]
114
-
115
- if(item.getAdditionalData())
116
- entry['additional_data'] = item.getAdditionalData();
117
- end
118
-
119
- orderLine['set_items'].push(entry)
120
- end
121
-
122
- orderLines.push(orderLine)
123
- end
124
-
125
- self.query.push(Hash['basket' => Hash['session_id' => sessionId,'order_lines' => orderLines]])
126
-
127
- return self;
128
- end
129
-
130
- ###
131
- # @param string $sessionId Free to choose ID of the current website visitor.
132
- # @param int $productVariantId ID of product variant.
133
- # @param string $basketItemId ID of single item or set in the basket
134
- #
135
- # @return $this
136
- ###
137
- def addToBasket(sessionId, productVariantId, basketItemId)
138
-
139
- checkSessionId(sessionId);
140
-
141
- self.query.push(Hash['basket' => Hash[
142
- 'session_id' => sessionId,
143
- 'order_lines' => Array(
144
- Hash[
145
- 'id' => basketItemId,
146
- 'variant_id' => Integer(productVariantId)
147
- ]
148
- )
149
- ]
150
- ]
151
- )
152
-
153
- return self;
154
- end
155
-
156
- ###
157
- # @param string $sessionId Free to choose ID of the current website visitor.
158
- # @param string[] $itemIds array of basket item ids to delete, this can be sets or single items
159
- #
160
- # @return $this
161
- ###
162
- def removeFromBasket(sessionId, itemIds)
163
-
164
- checkSessionId(sessionId);
165
-
166
- orderLines = Array();
167
-
168
- itemIds.each do |id|
169
- orderLines.push(Hash['delete' => id])
170
- end
171
-
172
- self.query.push(Hash[
173
- 'basket' => Hash[
174
- 'session_id' => sessionId,
175
- 'order_lines' => orderLines
176
- ]
177
- ]
178
- )
179
-
180
- return self;
181
- end
182
-
183
- ###
184
- # @param string $sessionId
185
- # @param Basket $basket
186
- #
187
- # @return $this
188
- ###
189
- def updateBasket(sessionId, basket)
190
-
191
- checkSessionId(sessionId);
192
-
193
- basketQuery = Hash['session_id' => sessionId]
194
-
195
- orderLines = basket.getOrderLinesArray();
196
- if (!orderLines.empty?)
197
- basketQuery['order_lines'] = orderLines;
198
- end
199
-
200
- self.query.push(Hash['basket' => basketQuery])
201
-
202
- return self;
203
- end
204
-
205
- ###
206
- # @param int[]|string[] $ids either a single category ID as integer or an array of IDs
207
- #
208
- # @return $this
209
- ###
210
- def fetchCategoriesByIds(ids = nil)
5
+ ###
6
+ # @param string $searchword The prefix search word to search for.
7
+ # @param int $limit Maximum number of results.
8
+ # @param array $types Array of types to search for (Constants::TYPE_...).
9
+ #
10
+ # @return $this
11
+ #
12
+ # @throws \InvalidArgumentException
13
+ ###
14
+ def fetchAutocomplete(searchword, limit = nil, types = nil)
15
+
16
+ unless searchword.is_a? String
17
+ raise 'InvalidArgumentException! searchword must be a string'
18
+ end
19
+
20
+ # downcase is a workaround of ticket SAPI-532
21
+ options = {'searchword' => searchword.downcase}
22
+
23
+ if limit != nil
24
+ if !(limit.is_a? Integer) && !limit[/\d/]
25
+ raise 'InvalidArgumentException! limit must be an integer'
26
+ end
27
+ options['limit'] = Integer(limit)
28
+ end
29
+
30
+ unless types.empty?
31
+ options['types'] = types
32
+ end
33
+
34
+ self.query.push({'autocompletion' => options})
35
+ self
36
+ end
37
+
38
+ ###
39
+ # @param string $sessionId Free to choose ID of the current website visitor.
40
+ #
41
+ # @return $this
42
+ ###
43
+ def fetchBasket(sessionId)
44
+
45
+ checkSessionId(sessionId)
46
+
47
+ self.query.push({'basket' => {'session_id' => sessionId}})
48
+ self
49
+ end
50
+
51
+ ###
52
+ # @param string $sessionId Free to choose ID of the current website visitor.
53
+ # @param int $productVariantId ID of product variant.
54
+ # @param int $amount Amount of items to add.
55
+ #
56
+ #@return $this
57
+ ###
58
+ def addItemsToBasket(sessionId, items)
59
+
60
+ checkSessionId(sessionId)
61
+
62
+ orderLines = []
63
+
64
+ items.each do |item|
65
+ orderLine = {
66
+ 'id' => item.id,
67
+ 'variant_id' => item.variantId
68
+ }
69
+
70
+ if item.additionalData
71
+ orderLine['additional_data'] = item.additionalData
72
+ end
73
+
74
+ orderLines.push(orderLine)
75
+ end
76
+
77
+ self.query.push({'basket' => {
78
+ 'session_id' => sessionId,
79
+ 'order_lines' => orderLines
80
+ }})
81
+ self
82
+ end
83
+
84
+ ###
85
+ # @param string $sessionId Free to choose ID of the current website visitor.
86
+ # @param Model\BasketItemSet[] $itemSets
87
+ # @param int $amount Amount of items to add.
88
+ #
89
+ # @return $this
90
+ ###
91
+ def addItemSetsToBasket(sessionId, itemSets)
92
+
93
+ checkSessionId(sessionId)
94
+
95
+ orderLines = []
96
+
97
+ itemSets.each do |itemSet|
98
+ orderLine = {
99
+ 'id' => itemSet.id,
100
+ 'set_items' => []
101
+ }
102
+
103
+ if itemSet.additionalData
104
+ orderLine['additional_data'] = itemSet.additionalData
105
+ end
106
+
107
+ itemSet.items.each do |item|
108
+ entry = {'variant_id' => item.variantId}
109
+ if item.additionalData
110
+ entry['additional_data'] = item.additionalData
111
+ end
112
+ orderLine['set_items'].push(entry)
113
+ end
114
+
115
+ orderLines.push(orderLine)
116
+ end
117
+
118
+ self.query.push({'basket' => {
119
+ 'session_id' => sessionId,
120
+ 'order_lines' => orderLines
121
+ }})
122
+ self
123
+ end
124
+
125
+ ###
126
+ # @param string $sessionId Free to choose ID of the current website visitor.
127
+ # @param int $productVariantId ID of product variant.
128
+ # @param string $basketItemId ID of single item or set in the basket
129
+ #
130
+ # @return $this
131
+ ###
132
+ def addToBasket(sessionId, productVariantId, basketItemId)
133
+
134
+ checkSessionId(sessionId);
135
+
136
+ self.query.push({'basket' => {
137
+ 'session_id' => sessionId,
138
+ 'order_lines' => [{
139
+ 'id' => basketItemId,
140
+ 'variant_id' => Integer(productVariantId)
141
+ }]
142
+ }})
143
+ self
144
+ end
145
+
146
+ ###
147
+ # @param string $sessionId Free to choose ID of the current website visitor.
148
+ # @param string[] $itemIds array of basket item ids to delete, this can be sets or single items
149
+ #
150
+ # @return $this
151
+ ###
152
+ def removeFromBasket(sessionId, itemIds)
153
+
154
+ checkSessionId(sessionId)
155
+
156
+ orderLines = []
157
+
158
+ itemIds.each do |id|
159
+ orderLines.push({'delete' => id})
160
+ end
161
+
162
+ self.query.push({
163
+ 'basket' => {
164
+ 'session_id' => sessionId,
165
+ 'order_lines' => orderLines
166
+ }
167
+ })
168
+ self
169
+ end
170
+
171
+ ###
172
+ # @param string $sessionId
173
+ # @param Basket $basket
174
+ #
175
+ # @return $this
176
+ ###
177
+ def updateBasket(sessionId, basket)
178
+
179
+ checkSessionId(sessionId)
180
+
181
+ basketQuery = {'session_id' => sessionId}
182
+
183
+ orderLines = basket.orderLinesArray
184
+ unless orderLines.empty?
185
+ basketQuery['order_lines'] = orderLines
186
+ end
187
+
188
+ self.query.push({'basket' => basketQuery})
189
+ self
190
+ end
191
+
192
+ ###
193
+ # @param int[]|string[] $ids either a single category ID as integer or an array of IDs
194
+ #
195
+ # @return $this
196
+ ###
197
+ def fetchCategoriesByIds(ids = nil)
211
198
 
212
- if (ids == nil)
213
- self.query.push('category' => nil)
214
- else
215
- # we allow to pass a single ID instead of an array
216
- ids = Array(ids)
217
-
218
- ids.each do |id|
219
- id = Integer(id)
220
- if (id < 1)
221
- raise 'InvalidArgumentException! A single category ID must be greater than 0'
222
- end
223
- end
224
-
225
- ids = ids.map{|id| Integer(id)}
226
-
227
- self.query.push(Hash[
228
- 'category' => Hash[
229
- 'ids' => ids
230
- ]
231
- ]
232
- )
233
- end
234
- return self
235
- end
236
-
237
- ###
238
- # @param int $maxDepth -1 <= $maxDepth <= 10,
239
- #
240
- # @return $this
241
- ###
242
- def fetchCategoryTree()
243
-
244
- self.query.push(Hash['category_tree' => {"version"=>"2"}])
245
-
246
- return self
247
- end
248
-
249
- ###
250
- # @param string[]|int[] $ids
251
- # @param array $fields
252
- #
253
- # @return $this
254
- ###
255
- def fetchProductsByIds(ids,fields = Array(nil))
256
- # we allow to pass a single ID instead of an array
257
-
258
- ids = Array(ids)
259
- ids = ids.map{|id| Integer(id)}
260
-
261
- self.query.push(Hash['products' => Hash['ids' => ids,'fields' => AboutYou::SDK::Criteria::ProductFields.filterFields(fields)]])
262
-
263
- return self;
264
- end
265
-
266
- ###
267
- # @param string[]|int[] $ids
268
- #
269
- # @return $this
270
- ###
271
- def fetchLiveVariantByIds(ids)
272
- # we allow to pass a single ID instead of an array
273
- ids = Array(ids)
274
-
275
- ids = ids.map{|id| Integer(id)}
276
-
277
- self.query.push(Hash['live_variant' => Hash['ids' => ids]])
278
-
279
- return self;
280
- end
281
-
282
- ###
283
- # @param string[] $eans
284
- # @param array $fields
285
- #
286
- # @return $this
287
- ###
288
- def fetchProductsByEans(eans,fields = Array())
289
- self.query.push(Hash[
290
- 'products_eans' => Hash[
291
- 'eans' => eans,
292
- 'fields' => AboutYou::SDK::Criteria::ProductFields.filterFields(fields),
293
- 'version' => '2'
294
- ]
295
- ]
296
- )
297
-
298
- return self;
299
- end
300
-
301
- ###
302
- # @param string|int $orderId
303
- #
304
- # @return $this
305
- ###
306
- def fetchOrder(orderId)
307
-
308
- self.query.push(Hash['get_order' => Hash['order_id' =>orderId]])
309
-
310
- return self;
311
- end
312
-
313
- ###
314
- # @param string $sessionId
315
- # @param string $successUrl
316
- # @param string $cancelUrl
317
- # @param string $errorUrl
318
- #
319
- # @return $this
320
- ###
321
- def initiateOrder(sessionId, successUrl, cancelUrl, errorUrl)
322
-
323
- checkSessionId(sessionId);
324
-
325
- args = Hash['session_id' => sessionId,'success_url' => successUrl]
326
- if (cancelUrl)
327
- args['cancel_url'] = cancelUrl;
328
- end
329
- if (errorUrl)
330
- args['error_url'] = errorUrl;
331
- end
332
- self.query.push(Hash['initiate_order' => args])
333
-
334
- return self;
335
- end
336
-
337
- ###
338
- # @param ProductSearchCriteria $criteria
339
- #
340
- # @return $this
341
- ###
342
- def fetchProductSearch(criteria)
343
-
344
- checkSessionId(criteria.sessionId());
345
- self.query.push(Hash['product_search' =>criteria.toArray])
346
- return self;
347
- end
348
-
349
- ###
350
- # @param array $groupIds
351
- #
352
- # @return $this
353
- #
354
- # @throws \InvalidArgumentException
355
- ###
356
- def fetchFacets(groupIds = Array(nil))
357
-
358
- if(groupIds)
359
- groupids = groupIds.map{|groupId| Integer(groupId)}
360
- end
361
-
362
- self.query.push(Hash['facets' => Hash['group_ids' => groupids]])
363
- return self;
364
- end
365
-
366
- ###
367
- # @param array $params
368
- #
369
- # @return $this
370
- #
371
- # @throws \InvalidArgumentException
372
- ###
373
- def fetchFacet(params)
374
-
375
- if (params.empty?)
376
- raise 'InvalidArgumentException! no params given'
377
- end
378
-
379
- self.query.push(Hash['facet' => params])
380
-
381
- return self
382
- end
383
-
384
- ###
385
- # @return $this
386
- ###
387
- def fetchFacetTypes()
388
-
389
- self.query.push(Hash['facet_types' => nil]);
390
-
391
- return self;
392
- end
393
-
394
- ###
395
- # @param string $searchword The search string to search for.
396
- #
397
- # @return $this
398
- ###
399
- def fetchSuggest(searchword)
400
-
401
- self.query.push(Hash['suggest' => Hash['searchword' => searchword]])
402
-
403
- return self
404
- end
405
-
406
- ###
407
- # @return $this
408
- ###
409
- def fetchChildApps()
410
-
411
- self.query.push(Hash['child_apps' => nil])
412
-
413
- return self;
414
- end
415
-
416
- ###
417
- # @return string
418
- ###
419
- def queryString()
420
-
421
- return self.query.to_json
422
- end
423
-
424
- ###
425
- # @param $sessionId
426
- #
427
- # @throws \InvalidArgumentException
428
- ###
429
- def checkSessionId(sessionId)
430
-
431
- if (!(sessionId.is_a? String))
432
- raise 'InvalidArgumentException! The session id must be a string'
433
- end
434
- if !(sessionId.length > 4)
435
- raise 'InvalidArgumentException! The session id must have at least 5 characters'
436
- end
437
- end
199
+ if ids == nil
200
+ self.query.push('category' => nil)
201
+ else
202
+ # we allow to pass a single ID instead of an array
203
+ ids = Array(ids)
204
+
205
+ ids.each do |id|
206
+ id = Integer(id)
207
+ if id < 1
208
+ raise 'InvalidArgumentException! A single category ID must be greater than 0'
209
+ end
210
+ end
211
+
212
+ ids = ids.map{|id| Integer(id)}
213
+
214
+ self.query.push({
215
+ 'category' => {
216
+ 'ids' => ids}
217
+ })
218
+ end
219
+ self
220
+ end
221
+
222
+ ###
223
+ # @param int $maxDepth -1 <= $maxDepth <= 10,
224
+ #
225
+ # @return $this
226
+ ###
227
+ def fetchCategoryTree()
228
+
229
+ self.query.push({'category_tree' => {"version"=>"2"}})
230
+ self
231
+ end
232
+
233
+ ###
234
+ # @param string[]|int[] $ids
235
+ # @param array $fields
236
+ #
237
+ # @return $this
238
+ ###
239
+ def fetchProductsByIds(ids,fields = [])
240
+ # we allow to pass a single ID instead of an array
241
+
242
+ ids = Array(ids).map{|id| Integer(id)}
243
+
244
+ self.query.push({'products' => {
245
+ 'ids' => ids,
246
+ 'fields' => AboutYou::SDK::Criteria::ProductFields.filterFields(fields)
247
+ }
248
+ })
249
+ self
250
+ end
251
+
252
+ ###
253
+ # @param string[]|int[] $ids
254
+ #
255
+ # @return $this
256
+ ###
257
+ def fetchLiveVariantByIds(ids)
258
+ # we allow to pass a single ID instead of an array
259
+ ids = Array(ids)
260
+
261
+ ids = ids.map{|id| Integer(id)}
262
+
263
+ self.query.push({'live_variant' => {'ids' => ids}})
264
+ self
265
+ end
266
+
267
+ ###
268
+ # @param string[] $eans
269
+ # @param array $fields
270
+ #
271
+ # @return $this
272
+ ###
273
+ def fetchProductsByEans(eans,fields = [])
274
+
275
+ self.query.push({
276
+ 'products_eans' => {
277
+ 'eans' => eans,
278
+ 'fields' => AboutYou::SDK::Criteria::ProductFields.filterFields(fields),
279
+ 'version' => '2'
280
+ }
281
+ })
282
+ self
283
+ end
284
+
285
+ ###
286
+ # @param string|int $orderId
287
+ #
288
+ # @return $this
289
+ ###
290
+ def fetchOrder(orderId)
291
+
292
+ self.query.push({'get_order' => {'order_id' => orderId}})
293
+ self
294
+ end
295
+
296
+ ###
297
+ # @param string $sessionId
298
+ # @param string $successUrl
299
+ # @param string $cancelUrl
300
+ # @param string $errorUrl
301
+ #
302
+ # @return $this
303
+ ###
304
+ def initiateOrder(sessionId, successUrl, cancelUrl, errorUrl)
305
+
306
+ checkSessionId(sessionId)
307
+
308
+ args = {
309
+ 'session_id' => sessionId,
310
+ 'success_url' => successUrl
311
+ }
312
+ if cancelUrl
313
+ args['cancel_url'] = cancelUrl
314
+ end
315
+ if errorUrl
316
+ args['error_url'] = errorUrl
317
+ end
318
+ self.query.push({'initiate_order' => args})
319
+ self
320
+ end
321
+
322
+ ###
323
+ # @param ProductSearchCriteria $criteria
324
+ #
325
+ # @return $this
326
+ ###
327
+ def fetchProductSearch(criteria)
328
+
329
+ checkSessionId(criteria.sessionId)
330
+
331
+ self.query.push({'product_search' => criteria.toArray})
332
+ self
333
+ end
334
+
335
+ ###
336
+ # @param array $groupIds
337
+ #
338
+ # @return $this
339
+ #
340
+ # @throws \InvalidArgumentException
341
+ ###
342
+ def fetchFacets(groupIds = [])
343
+
344
+ if groupIds
345
+ groupids = groupIds.map{|groupId| Integer(groupId)}
346
+ end
347
+
348
+ self.query.push({'facets' => {'group_ids' => groupids}})
349
+ self
350
+ end
351
+
352
+ ###
353
+ # @param array $params
354
+ #
355
+ # @return $this
356
+ #
357
+ # @throws \InvalidArgumentException
358
+ ###
359
+ def fetchFacet(params)
360
+
361
+ if params.empty?
362
+ raise 'InvalidArgumentException! no params given'
363
+ end
364
+
365
+ self.query.push({'facet' => params})
366
+ self
367
+ end
368
+
369
+ ###
370
+ # @return $this
371
+ ###
372
+ def fetchFacetTypes
373
+
374
+ self.query.push({'facet_types' => nil})
375
+ self
376
+ end
377
+
378
+ ###
379
+ # @param string $searchword The search string to search for.
380
+ #
381
+ # @return $this
382
+ ###
383
+ def fetchSuggest(searchword)
384
+
385
+ self.query.push({'suggest' => {'searchword' => searchword}})
386
+ self
387
+ end
388
+
389
+ ###
390
+ # @return $this
391
+ ###
392
+ def fetchChildApps
393
+
394
+ self.query.push({'child_apps' => nil})
395
+ self
396
+ end
397
+
398
+ ###
399
+ # @return string
400
+ ###
401
+ def queryString
402
+
403
+ self.query.to_json
404
+ end
405
+
406
+ ###
407
+ # @param $sessionId
408
+ #
409
+ # @throws \InvalidArgumentException
410
+ ###
411
+ def checkSessionId(sessionId)
412
+
413
+ unless sessionId.is_a? String
414
+ raise 'InvalidArgumentException! The session id must be a string'
415
+ end
416
+ unless sessionId.length > 4
417
+ raise 'InvalidArgumentException! The session id must have at least 5 characters'
418
+ end
419
+ end
438
420
  end
439
421
  end
440
422
  end