aboutyou-sdk 0.0.24 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +8 -8
  2. data/lib/{aboutyou-sdk/CacheProvider/Dalli.rb → AboutYou/CacheProvider/dalli.rb} +18 -19
  3. data/lib/{aboutyou-sdk/CacheProvider/Redis.rb → AboutYou/CacheProvider/redis.rb} +19 -20
  4. data/lib/{aboutyou-sdk/Criteria/ProductFields.rb → AboutYou/Criteria/product_fields.rb} +25 -27
  5. data/lib/{aboutyou-sdk/Criteria/ProductSearchCriteria.rb → AboutYou/Criteria/product_search_criteria.rb} +128 -123
  6. data/lib/{aboutyou-sdk/Factory/DefaultModelFactory.rb → AboutYou/Factory/default_model_factory.rb} +267 -224
  7. data/lib/AboutYou/Model/Basket/abstract_basket_item.rb +57 -0
  8. data/lib/AboutYou/Model/Basket/basket_item.rb +95 -0
  9. data/lib/AboutYou/Model/Basket/basket_set.rb +199 -0
  10. data/lib/AboutYou/Model/Basket/basket_set_item.rb +54 -0
  11. data/lib/AboutYou/Model/Basket/basket_variant_item.rb +123 -0
  12. data/lib/{aboutyou-sdk/Model/CategoryManager/DefaultCategoryManager.rb → AboutYou/Model/CategoryManager/default_category_manager.rb} +80 -65
  13. data/lib/{aboutyou-sdk/Model/FacetManager/DefaultFacetManager.rb → AboutYou/Model/FacetManager/default_facet_manager.rb} +58 -50
  14. data/lib/{aboutyou-sdk/Model/ProductSearchResult/FacetCount.rb → AboutYou/Model/ProductSearchResult/facet_count.rb} +2 -3
  15. data/lib/AboutYou/Model/ProductSearchResult/facet_counts.rb +67 -0
  16. data/lib/{aboutyou-sdk/Model/ProductSearchResult/PriceRange.rb → AboutYou/Model/ProductSearchResult/price_range.rb} +17 -18
  17. data/lib/AboutYou/Model/ProductSearchResult/sale_counts.rb +79 -0
  18. data/lib/AboutYou/Model/ProductSearchResult/terms_count.rb +40 -0
  19. data/lib/{aboutyou-sdk/Model/AbstractModel.rb → AboutYou/Model/abstract_model.rb} +2 -3
  20. data/lib/{aboutyou-sdk/Model/App.rb → AboutYou/Model/app.rb} +15 -15
  21. data/lib/{aboutyou-sdk/Model/Autocomplete.rb → AboutYou/Model/autocomplete.rb} +25 -32
  22. data/lib/AboutYou/Model/basket.rb +235 -0
  23. data/lib/AboutYou/Model/categories_result.rb +24 -0
  24. data/lib/AboutYou/Model/category.rb +102 -0
  25. data/lib/AboutYou/Model/category_tree.rb +37 -0
  26. data/lib/AboutYou/Model/facet.rb +86 -0
  27. data/lib/AboutYou/Model/facet_group.rb +96 -0
  28. data/lib/AboutYou/Model/facet_group_set.rb +209 -0
  29. data/lib/AboutYou/Model/image.rb +102 -0
  30. data/lib/{aboutyou-sdk/Model/ImageSize.rb → AboutYou/Model/image_size.rb} +9 -5
  31. data/lib/AboutYou/Model/initiate_order.rb +53 -0
  32. data/lib/AboutYou/Model/order.rb +22 -0
  33. data/lib/AboutYou/Model/product.rb +353 -0
  34. data/lib/AboutYou/Model/product_search_result.rb +114 -0
  35. data/lib/AboutYou/Model/products_ean_result.rb +43 -0
  36. data/lib/AboutYou/Model/products_result.rb +56 -0
  37. data/lib/AboutYou/Model/result_error.rb +28 -0
  38. data/lib/AboutYou/Model/variant.rb +214 -0
  39. data/lib/AboutYou/Model/variants_result.rb +91 -0
  40. data/lib/AboutYou/client.rb +128 -0
  41. data/lib/{aboutyou-sdk/Constants.rb → AboutYou/constants.rb} +3 -5
  42. data/lib/AboutYou/query.rb +343 -0
  43. data/lib/AboutYou/query_builder.rb +530 -0
  44. data/lib/ay.rb +566 -0
  45. data/tests/Sinatra-test/Main.rb +3 -0
  46. data/tests/testAutocomplete.rb +5 -6
  47. data/tests/testCatFilter.rb +8 -9
  48. data/tests/testCatTree.rb +3 -3
  49. data/tests/testCategoryCaching.rb +4 -5
  50. data/tests/{testFacetCaching.rb → testDalliCache.rb} +4 -5
  51. data/tests/testFacetTypes.rb +5 -6
  52. data/tests/testFacets.rb +7 -8
  53. data/tests/testFetchFacet.rb +5 -6
  54. data/tests/testJavaScript.rb +3 -4
  55. data/tests/testProdCatLongestPath.rb +7 -8
  56. data/tests/testProductSearchResult.rb +6 -7
  57. data/tests/testProductsByEans.rb +6 -7
  58. data/tests/testProductsByIds.rb +6 -7
  59. data/tests/testRedisCache.rb +4 -5
  60. data/tests/testSuggest.rb +5 -6
  61. data/tests/testVariantsByIds.rb +7 -8
  62. metadata +46 -46
  63. data/lib/aboutyou-sdk.rb +0 -596
  64. data/lib/aboutyou-sdk/Client.rb +0 -134
  65. data/lib/aboutyou-sdk/Model/Basket.rb +0 -268
  66. data/lib/aboutyou-sdk/Model/Basket/AbstractBasketItem.rb +0 -76
  67. data/lib/aboutyou-sdk/Model/Basket/BasketItem.rb +0 -100
  68. data/lib/aboutyou-sdk/Model/Basket/BasketSet.rb +0 -192
  69. data/lib/aboutyou-sdk/Model/Basket/BasketSetItem.rb +0 -46
  70. data/lib/aboutyou-sdk/Model/Basket/BasketVariantItem.rb +0 -137
  71. data/lib/aboutyou-sdk/Model/CategoriesResult.rb +0 -15
  72. data/lib/aboutyou-sdk/Model/Category.rb +0 -126
  73. data/lib/aboutyou-sdk/Model/CategoryTree.rb +0 -35
  74. data/lib/aboutyou-sdk/Model/Facet.rb +0 -105
  75. data/lib/aboutyou-sdk/Model/FacetGroup.rb +0 -110
  76. data/lib/aboutyou-sdk/Model/FacetGroupSet.rb +0 -242
  77. data/lib/aboutyou-sdk/Model/Image.rb +0 -134
  78. data/lib/aboutyou-sdk/Model/InitiateOrder.rb +0 -50
  79. data/lib/aboutyou-sdk/Model/Order.rb +0 -17
  80. data/lib/aboutyou-sdk/Model/Product.rb +0 -335
  81. data/lib/aboutyou-sdk/Model/ProductSearchResult.rb +0 -125
  82. data/lib/aboutyou-sdk/Model/ProductSearchResult/FacetCounts.rb +0 -59
  83. data/lib/aboutyou-sdk/Model/ProductSearchResult/SaleCounts.rb +0 -71
  84. data/lib/aboutyou-sdk/Model/ProductSearchResult/TermsCount.rb +0 -36
  85. data/lib/aboutyou-sdk/Model/ProductsEansResult.rb +0 -47
  86. data/lib/aboutyou-sdk/Model/ProductsResult.rb +0 -52
  87. data/lib/aboutyou-sdk/Model/ResultError.rb +0 -24
  88. data/lib/aboutyou-sdk/Model/Variant.rb +0 -255
  89. data/lib/aboutyou-sdk/Model/VariantsResult.rb +0 -102
  90. data/lib/aboutyou-sdk/Query.rb +0 -350
  91. data/lib/aboutyou-sdk/QueryBuilder.rb +0 -499
@@ -0,0 +1,530 @@
1
+ module AboutYou
2
+ module SDK
3
+ ###
4
+ # the QueryBuilder is a module supporting the query with mroe complex logic
5
+ # concerning the building of the queries for the api-call
6
+ #
7
+ # Author:: Collins GmbH & Co KG
8
+ ###
9
+ module QueryBuilder
10
+ ###
11
+ # builds the autocompletion-query
12
+ #
13
+ # * *Args* :
14
+ # - +searchword+ -> a String containing the word to search completitions for
15
+ # - +limit+ -> Maximum number of results [optional]
16
+ # - +types+ -> Array of types to search for [optional]
17
+ #
18
+ # * *Returns* :
19
+ # - Instance of AboutYou::SDK::Query
20
+ ###
21
+ def fetch_autocomplete(searchword, limit = nil, types = nil)
22
+ unless searchword.is_a? String
23
+ fail 'InvalidArgumentException! searchword must be a string'
24
+ end
25
+
26
+ # downcase is a workaround of ticket SAPI-532
27
+ options = { 'searchword' => searchword.downcase }
28
+
29
+ unless limit.nil?
30
+ fail 'InvalidArgumentException! limit must be an integer' unless
31
+ limit.is_a?(Integer) || limit[/\d/]
32
+ options['limit'] = Integer(limit)
33
+ end
34
+
35
+ options['types'] = types unless types.empty?
36
+
37
+ query.push(
38
+ 'autocompletion' => options
39
+ )
40
+
41
+ self
42
+ end
43
+
44
+ ###
45
+ # builds the basket-query
46
+ #
47
+ # * *Args* :
48
+ # - +session_id+ -> a String containing the session id of an user
49
+ #
50
+ # * *Returns* :
51
+ # - Instance of AboutYou::SDK::Query
52
+ ###
53
+ def fetch_basket(session_id)
54
+ check_session_id(session_id)
55
+
56
+ query.push(
57
+ 'basket' => {
58
+ 'session_id' => session_id
59
+ }
60
+ )
61
+
62
+ self
63
+ end
64
+
65
+ ###
66
+ # builds the basket-query
67
+ #
68
+ # * *Args* :
69
+ # - +session_id+ -> a String containing the session id of an user
70
+ # - +items+ -> an Array containing all of the items which should be added
71
+ #
72
+ # * *Returns* :
73
+ # - Instance of AboutYou::SDK::Query
74
+ ###
75
+ def add_items_to_basket(session_id, items)
76
+ check_session_id(session_id)
77
+
78
+ order_lines = []
79
+
80
+ items.each do |item|
81
+ order_line = {
82
+ 'id' => item.id,
83
+ 'variant_id' => item.variant_id
84
+ }
85
+
86
+ if item.additional_data
87
+ order_line['additional_data'] = item.additional_data
88
+ end
89
+
90
+ order_lines.push(order_line)
91
+ end
92
+
93
+ query.push(
94
+ 'basket' => {
95
+ 'session_id' => session_id,
96
+ 'order_lines' => order_lines
97
+ }
98
+ )
99
+
100
+ self
101
+ end
102
+
103
+ ###
104
+ # builds the basket-query
105
+ #
106
+ # * *Args* :
107
+ # - +session_id+ -> a String containing the session id of an user
108
+ # - +itemSets+ -> an instance of AboutYou::SDK::Model::BasketSet
109
+ #
110
+ # * *Returns* :
111
+ # - Instance of AboutYou::SDK::Query
112
+ ###
113
+ def add_itemsets_to_basket(session_id, itemsets)
114
+ check_session_id(session_id)
115
+
116
+ order_lines = []
117
+
118
+ itemsets.each do |item_set|
119
+ order_line = {
120
+ 'id' => item_set.id,
121
+ 'set_items' => []
122
+ }
123
+
124
+ if item_set.additional_data
125
+ order_line['additional_data'] = item_set.additional_data
126
+ end
127
+
128
+ item_set.items.each do |item|
129
+ entry = {
130
+ 'variant_id' => item.variant_id
131
+ }
132
+ if item.additional_data
133
+ entry['additional_data'] = item.additional_data
134
+ end
135
+ order_line['set_items'].push(entry)
136
+ end
137
+
138
+ order_lines.push(order_line)
139
+ end
140
+
141
+ query.push(
142
+ 'basket' => {
143
+ 'session_id' => session_id,
144
+ 'order_lines' => order_lines
145
+ }
146
+ )
147
+
148
+ self
149
+ end
150
+
151
+ ###
152
+ # builds the basket-query
153
+ #
154
+ # * *Args* :
155
+ # - +session_id+ -> a String containing the session id of an user
156
+ # - +productVariantId+ -> ID of product variant
157
+ # - +basketItemId+ -> ID of single item or set in the basket
158
+ #
159
+ # * *Returns* :
160
+ # - Instance of AboutYou::SDK::Query
161
+ ###
162
+ def add_to_basket(session_id, product_variant_id, basket_item_id)
163
+ check_session_id(session_id)
164
+
165
+ query.push(
166
+ 'basket' => {
167
+ 'session_id' => session_id,
168
+ 'order_lines' => [{
169
+ 'id' => basket_item_id,
170
+ 'variant_id' => Integer(product_variant_id)
171
+ }]
172
+ }
173
+ )
174
+
175
+ self
176
+ end
177
+
178
+ ###
179
+ # builds the basket-query
180
+ #
181
+ # * *Args* :
182
+ # - +session_id+ -> a String containing the session id of an user
183
+ # - +itemIds+ -> array of basket item ids to delete, this can be sets or single items
184
+ #
185
+ # * *Returns* :
186
+ # - Instance of AboutYou::SDK::Query
187
+ ###
188
+ def remove_from_basket(session_id, item_ids)
189
+ check_session_id(session_id)
190
+
191
+ order_lines = []
192
+
193
+ item_ids.each do |id|
194
+ order_lines.push(
195
+ 'delete' => id
196
+ )
197
+ end
198
+
199
+ query.push(
200
+ 'basket' => {
201
+ 'session_id' => session_id,
202
+ 'order_lines' => order_lines
203
+ }
204
+ )
205
+
206
+ self
207
+ end
208
+
209
+ ###
210
+ # builds the basket-query
211
+ #
212
+ # * *Args* :
213
+ # - +session_id+ -> a String containing the session id of an user
214
+ # - +basket+ -> an instance of AboutYou::SDK::Model::Basket
215
+ #
216
+ # * *Returns* :
217
+ # - Instance of AboutYou::SDK::Query
218
+ ###
219
+ def update_basket(session_id, basket)
220
+ check_session_id(session_id)
221
+
222
+ order_lines = basket.order_lines_array
223
+ basket_query = { 'session_id' => session_id }
224
+ basket_query['order_lines'] = order_lines unless order_lines.empty?
225
+
226
+ query.push(
227
+ 'basket' => basket_query
228
+ )
229
+
230
+ self
231
+ end
232
+
233
+ ###
234
+ # builds the category-query
235
+ #
236
+ # * *Args* :
237
+ # - +ids+ -> either a single category ID as integer or an array of IDs
238
+ #
239
+ # * *Returns* :
240
+ # - Instance of AboutYou::SDK::Query
241
+ ###
242
+ def fetch_categories_by_ids(ids = nil)
243
+ if ids.nil?
244
+ query.push(
245
+ 'category' => nil
246
+ )
247
+ else
248
+ # we allow to pass a single ID instead of an array
249
+ ids = Array(ids)
250
+
251
+ ids.each do |id|
252
+ id = Integer(id)
253
+ if id < 1
254
+ fail 'InvalidArgumentException! A single category ID must be greater than 0'
255
+ end
256
+ end
257
+
258
+ ids = ids.map { |id| Integer(id) }
259
+
260
+ query.push(
261
+ 'category' => {
262
+ 'ids' => ids
263
+ }
264
+ )
265
+ end
266
+
267
+ self
268
+ end
269
+
270
+ ###
271
+ # builds the category_tree-query
272
+ #
273
+ # * *Returns* :
274
+ # - Instance of AboutYou::SDK::Query
275
+ ###
276
+ def fetch_category_tree
277
+ query.push(
278
+ 'category_tree' => {
279
+ 'version' => '2'
280
+ }
281
+ )
282
+
283
+ self
284
+ end
285
+
286
+ ###
287
+ # builds the products-query
288
+ #
289
+ # * *Args* :
290
+ # - +ids+ -> Either a single id or an Array of ids which should be fetched
291
+ # - +fields+ -> Additional product fields which should be fetched for each product [optional]
292
+ #
293
+ # * *Returns* :
294
+ # - Instance of AboutYou::SDK::Query
295
+ ###
296
+ def fetch_products_by_ids(ids, fields = [])
297
+ # we allow to pass a single ID instead of an array
298
+ ids = Array(ids).map { |id| Integer(id) }
299
+
300
+ query.push(
301
+ 'products' => {
302
+ 'ids' => ids,
303
+ 'fields' => AboutYou::SDK::Criteria::ProductFields.filter_fields(fields)
304
+ }
305
+ )
306
+ self
307
+ end
308
+
309
+ ###
310
+ # builds the live_variant-query
311
+ #
312
+ # * *Args* :
313
+ # - +ids+ -> Either a single id or an Array of ids which should be fetched
314
+ #
315
+ # * *Returns* :
316
+ # - Instance of AboutYou::SDK::Query
317
+ ###
318
+ def fetch_live_variant_by_ids(ids)
319
+ # we allow to pass a single ID instead of an array
320
+ ids = Array(ids)
321
+ ids = ids.map { |id| Integer(id) }
322
+
323
+ query.push(
324
+ 'live_variant' => {
325
+ 'ids' => ids
326
+ }
327
+ )
328
+
329
+ self
330
+ end
331
+
332
+ ###
333
+ # builds the products_eans-query
334
+ #
335
+ # * *Args* :
336
+ # - +eans+ -> Either a single ean or an Array of eans which should be fetched
337
+ # - +fields+ -> Additional product fields which should be fetched for each product [optional]
338
+ #
339
+ # * *Returns* :
340
+ # - Instance of AboutYou::SDK::Query
341
+ ###
342
+ def fetch_products_by_eans(eans, fields = [])
343
+ query.push(
344
+ 'products_eans' => {
345
+ 'eans' => eans,
346
+ 'fields' => AboutYou::SDK::Criteria::ProductFields.filter_fields(fields),
347
+ 'version' => '2'
348
+ }
349
+ )
350
+
351
+ self
352
+ end
353
+
354
+ ###
355
+ # builds the get_order-query
356
+ #
357
+ # * *Args* :
358
+ # - +orderId+ -> id of the order which should be fetched
359
+ #
360
+ # * *Returns* :
361
+ # - Instance of AboutYou::SDK::Query
362
+ ###
363
+ def fetch_order(order_id)
364
+ query.push(
365
+ 'get_order' => {
366
+ 'order_id' => order_id
367
+ }
368
+ )
369
+
370
+ self
371
+ end
372
+
373
+ ###
374
+ # builds the initiate_order-query
375
+ #
376
+ # * *Args* :
377
+ # - +session_id+ -> A String containing the sessionId of the User
378
+ # - +successUrl+ -> callback URL if the order was OK
379
+ # - +cancelUrl+ -> callback URL if the order was canceled [optional]
380
+ # - +errorUrl+ -> callback URL if the order had any exceptions [optional]
381
+ #
382
+ # * *Returns* :
383
+ # - Instance of AboutYou::SDK::Query
384
+ ###
385
+ def initiate_order(session_id, success_url, cancel_url, error_url)
386
+ check_session_id(session_id)
387
+
388
+ args = {
389
+ 'session_id' => session_id,
390
+ 'success_url' => success_url
391
+ }
392
+ args['cancel_url'] = cancel_url if cancel_url
393
+ args['error_url'] = error_url if error_url
394
+
395
+ query.push(
396
+ 'initiate_order' => args
397
+ )
398
+
399
+ self
400
+ end
401
+
402
+ ###
403
+ # builds the product_search-query
404
+ #
405
+ # * *Args* :
406
+ # - +criteria+ -> Hash containing one or multiple search terms
407
+ #
408
+ # * *Returns* :
409
+ # - Instance of AboutYou::SDK::Query
410
+ ###
411
+ def fetch_product_search(criteria)
412
+ check_session_id(criteria.session_id)
413
+
414
+ query.push(
415
+ 'product_search' => criteria.to_array
416
+ )
417
+
418
+ self
419
+ end
420
+
421
+ ###
422
+ # builds the facets-query
423
+ #
424
+ # * *Args* :
425
+ # - +group_ids+ -> Array of group ids [optional]
426
+ #
427
+ # * *Returns* :
428
+ # - Instance of AboutYou::SDK::Query
429
+ ###
430
+ def fetch_facets(group_ids = [])
431
+ group_ids = group_ids.map { |groupId| Integer(groupId) } if group_ids
432
+
433
+ query.push(
434
+ 'facets' => {
435
+ 'group_ids' => group_ids
436
+ }
437
+ )
438
+
439
+ self
440
+ end
441
+
442
+ ###
443
+ # builds the facet-query
444
+ #
445
+ # * *Args* :
446
+ # - +params+ -> Hash containing 2 keys: "id", "group_id"
447
+ #
448
+ # * *Returns* :
449
+ # - Instance of AboutYou::SDK::Query
450
+ ###
451
+ def fetch_facet(params)
452
+ fail 'InvalidArgumentException! no params given' if params.empty?
453
+
454
+ query.push(
455
+ 'facet' => params
456
+ )
457
+
458
+ self
459
+ end
460
+
461
+ ###
462
+ # builds the facet_types-query
463
+ #
464
+ # * *Returns* :
465
+ # - Instance of AboutYou::SDK::Query
466
+ ###
467
+ def fetch_facet_types
468
+ query.push(
469
+ 'facet_types' => nil
470
+ )
471
+
472
+ self
473
+ end
474
+
475
+ ###
476
+ # builds the suggest-query
477
+ #
478
+ # * *Args* :
479
+ # - +searchword+ -> the searchs tring to search for
480
+ #
481
+ # * *Returns* :
482
+ # - Instance of AboutYou::SDK::Query
483
+ ###
484
+ def fetch_suggest(searchword)
485
+ query.push(
486
+ 'suggest' => {
487
+ 'searchword' => searchword
488
+ }
489
+ )
490
+
491
+ self
492
+ end
493
+
494
+ ###
495
+ # builds the child_apps-query
496
+ #
497
+ # * *Returns* :
498
+ # - Instance of AboutYou::SDK::Query
499
+ ###
500
+ def fetch_child_apps
501
+ query.push(
502
+ 'child_apps' => nil
503
+ )
504
+
505
+ self
506
+ end
507
+
508
+ ###
509
+ # turns all queries built in here into a json string
510
+ #
511
+ # * *Returns* :
512
+ # - Json string containing all queries built in here
513
+ ###
514
+ def query_string
515
+ query.to_json
516
+ end
517
+
518
+ ###
519
+ # checks whether the session id is valid or not
520
+ #
521
+ # * *Args* :
522
+ # - +session_id+ -> the session id of a user
523
+ ###
524
+ def check_session_id(sessionId)
525
+ fail 'InvalidArgumentException! The session id must be a string' unless sessionId.is_a? String
526
+ fail 'InvalidArgumentException! The session id must have at least 5 characters' unless sessionId.length > 4
527
+ end
528
+ end
529
+ end
530
+ end