aboutyou-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +15 -0
  2. data/lib/aboutyou-sdk.rb +545 -0
  3. data/lib/aboutyou-sdk/Client.rb +125 -0
  4. data/lib/aboutyou-sdk/Constants.rb +48 -0
  5. data/lib/aboutyou-sdk/Criteria/ProductFields.rb +54 -0
  6. data/lib/aboutyou-sdk/Criteria/ProductSearchCriteria.rb +436 -0
  7. data/lib/aboutyou-sdk/Factory/DefaultModelFactory.rb +454 -0
  8. data/lib/aboutyou-sdk/Model/AbstractModel.rb +26 -0
  9. data/lib/aboutyou-sdk/Model/App.rb +35 -0
  10. data/lib/aboutyou-sdk/Model/Autocomplete.rb +92 -0
  11. data/lib/aboutyou-sdk/Model/Basket.rb +262 -0
  12. data/lib/aboutyou-sdk/Model/Basket/AbstractBasketItem.rb +76 -0
  13. data/lib/aboutyou-sdk/Model/Basket/BasketItem.rb +100 -0
  14. data/lib/aboutyou-sdk/Model/Basket/BasketSet.rb +192 -0
  15. data/lib/aboutyou-sdk/Model/Basket/BasketSetItem.rb +46 -0
  16. data/lib/aboutyou-sdk/Model/Basket/BasketVariantItem.rb +137 -0
  17. data/lib/aboutyou-sdk/Model/CategoriesResult.rb +15 -0
  18. data/lib/aboutyou-sdk/Model/Category.rb +126 -0
  19. data/lib/aboutyou-sdk/Model/CategoryManager/DefaultCategoryManager.rb +172 -0
  20. data/lib/aboutyou-sdk/Model/CategoryTree.rb +35 -0
  21. data/lib/aboutyou-sdk/Model/Facet.rb +105 -0
  22. data/lib/aboutyou-sdk/Model/FacetGroup.rb +110 -0
  23. data/lib/aboutyou-sdk/Model/FacetGroupSet.rb +247 -0
  24. data/lib/aboutyou-sdk/Model/FacetManager/DefaultFacetManager.rb +79 -0
  25. data/lib/aboutyou-sdk/Model/Image.rb +134 -0
  26. data/lib/aboutyou-sdk/Model/ImageSize.rb +21 -0
  27. data/lib/aboutyou-sdk/Model/InitiateOrder.rb +50 -0
  28. data/lib/aboutyou-sdk/Model/Order.rb +17 -0
  29. data/lib/aboutyou-sdk/Model/Product.rb +543 -0
  30. data/lib/aboutyou-sdk/Model/ProductSearchResult.rb +125 -0
  31. data/lib/aboutyou-sdk/Model/ProductSearchResult/FacetCount.rb +26 -0
  32. data/lib/aboutyou-sdk/Model/ProductSearchResult/FacetCounts.rb +38 -0
  33. data/lib/aboutyou-sdk/Model/ProductSearchResult/PriceRange.rb +49 -0
  34. data/lib/aboutyou-sdk/Model/ProductSearchResult/SaleCounts.rb +58 -0
  35. data/lib/aboutyou-sdk/Model/ProductSearchResult/TermsCount.rb +27 -0
  36. data/lib/aboutyou-sdk/Model/ProductsEansResult.rb +43 -0
  37. data/lib/aboutyou-sdk/Model/ProductsResult.rb +52 -0
  38. data/lib/aboutyou-sdk/Model/ResultError.rb +24 -0
  39. data/lib/aboutyou-sdk/Model/Variant.rb +255 -0
  40. data/lib/aboutyou-sdk/Model/VariantsResult.rb +102 -0
  41. data/lib/aboutyou-sdk/Query.rb +261 -0
  42. data/lib/aboutyou-sdk/QueryBuilder.rb +440 -0
  43. data/tests/Sinatra-test/Main.rb +5 -0
  44. data/tests/testApiClient.rb +35 -0
  45. data/tests/testAutocomplete.rb +40 -0
  46. data/tests/testCatFilter.rb +35 -0
  47. data/tests/testCatTree.rb +37 -0
  48. data/tests/testFacets.rb +36 -0
  49. data/tests/testProdCatLongestPath.rb +34 -0
  50. data/tests/testProductSearchResult.rb +35 -0
  51. data/tests/testProductsByIds.rb +34 -0
  52. data/tests/testVariantsByIds.rb +34 -0
  53. metadata +122 -0
@@ -0,0 +1,454 @@
1
+ module AboutYou
2
+ module SDK
3
+ module Factory
4
+ class DefaultModelFactory
5
+
6
+ attr_accessor :shopApi
7
+ attr_accessor :categoryManager
8
+ attr_accessor :facetManager
9
+
10
+ ###
11
+ # @param ShopApi $shopApi
12
+ # @param FacetManagerInterface $facetManager
13
+ # @param EventDispatcher $eventDispatcher
14
+ ###
15
+ def initialize(shopApi = nil,facetManager,categoryManager)
16
+
17
+ self.shopApi=(shopApi);
18
+ self.categoryManager = categoryManager;
19
+ self.facetManager=facetManager;
20
+ AboutYou::SDK::Model::FacetGroupSet.facetManager=(self.facetManager);
21
+ end
22
+
23
+ def baseImageUrl=(baseUrl)
24
+
25
+ AboutYou::SDK::Model::Image.baseUrl=baseUrl;
26
+ end
27
+
28
+
29
+ ###
30
+ # {@inheritdoc}
31
+ #
32
+ # @return ShopApi\Model\Autocomplete
33
+ ###
34
+ def createAutocomplete(jsonObject, query)
35
+
36
+ return AboutYou::SDK::Model::Autocomplete.createFromJson(jsonObject, self);
37
+ end
38
+
39
+ ###
40
+ # {@inheritdoc}
41
+ #
42
+ # @return ShopApi\Model\Basket
43
+ ###
44
+ def createBasket(jsonObject)
45
+
46
+ return AboutYou::SDK::Model::Basket.createFromJson(jsonObject, self);
47
+ end
48
+
49
+ ###
50
+ # {@inheritdoc}
51
+ #
52
+ # @return ShopApi\Model\Basket\BasketItem
53
+ ###
54
+ def createBasketItem(jsonObject, products)
55
+
56
+ return AboutYou::SDK::Model::Basket::BasketItem.createFromJson(jsonObject, products);
57
+ end
58
+
59
+ ###
60
+ # {@inheritdoc}
61
+ #
62
+ # @return ShopApi\Model\Basket\BasketSet
63
+ ###
64
+ def createBasketSet(jsonObject, products)
65
+
66
+ return AboutYou::SDK::Model::BasketSet::createFromJson(jsonObject, self, products);
67
+ end
68
+
69
+ ###
70
+ # {@inheritdoc}
71
+ #
72
+ # @return ShopApi\Model\Basket\BasketSetItem
73
+ ###
74
+ def createBasketSetItem(jsonObject, products)
75
+
76
+ return AboutYou::SDK::Model::BasketSetItem::createFromJson(jsonObject, products);
77
+ end
78
+
79
+ ###
80
+ # {@inheritdoc}
81
+ #
82
+ # @return ShopApi\Model\Category
83
+ ###
84
+ def createCategory(jsonObject, parent = nil)
85
+
86
+ return AboutYou::SDK::Model::Category.createFromJson(jsonObject, self.categoryManager);
87
+ end
88
+
89
+ ###
90
+ # {@inheritdoc}
91
+ #
92
+ #
93
+ # @return ShopApi\Model\CategoryTree
94
+ ###
95
+ def createCategoryTree(jsonArray, query)
96
+
97
+ self.initializeCategoryManager(jsonArray);
98
+
99
+ return AboutYou::SDK::Model::CategoryTree.new(self.categoryManager());
100
+ end
101
+
102
+ ###
103
+ # {@inheritdoc}
104
+ #
105
+ # @return ShopApi\Model\Facet
106
+ ###
107
+ def createFacet(jsonObject)
108
+
109
+ return AboutYou::SDK::Model::Facet.createFromJson(jsonObject);
110
+ end
111
+
112
+ ###
113
+ # {@inheritdoc}
114
+ #
115
+ # @return ShopApi\Model\Facet[]
116
+ ###
117
+ def createFacetList(jsonArray)
118
+
119
+ facets = Hash.new
120
+ jsonArray.each do |jsonFacet|
121
+ facet = self.createFacet(jsonFacet);
122
+ key = facet.uniqueKey();
123
+ facets[key] = facet;
124
+ end
125
+
126
+ return facets;
127
+ end
128
+
129
+ ###
130
+ # {@inheritdoc}
131
+ #
132
+ # @return ShopApi\Model\Facet[]
133
+ ###
134
+ def createFacetsList(jsonObject, query)
135
+
136
+ return self.createFacetList(jsonObject["facet"]);
137
+ end
138
+
139
+
140
+ ###
141
+ # {@inheritdoc}
142
+ ###
143
+ def createFacetsCounts(jsonObject)
144
+ facetsCounts = Hash.new
145
+
146
+ jsonObject["facets"].each do |groupId, jsonResultFacet|
147
+ if !(groupId[/\d/])
148
+ next
149
+ end
150
+ facetCounts = self.termFacets(groupId, jsonResultFacet["terms"])
151
+
152
+ facetsCounts[groupId] = AboutYou::SDK::Model::FacetCounts.createFromJson(
153
+ groupId,
154
+ jsonResultFacet,
155
+ facetCounts
156
+ );
157
+ end
158
+
159
+ return facetsCounts;
160
+ end
161
+
162
+ def termFacets(groupId, jsonTerms)
163
+
164
+ facetManager = self.facetManager;
165
+
166
+ facetCounts = Array(nil);
167
+ jsonTerms.each do |jsonTerm|
168
+ id = Integer(jsonTerm["term"]);
169
+ facet = facetManager.facet(groupId, id);
170
+ if (facet == nil)
171
+ next;
172
+ end
173
+ # TODO: Handle error, write test
174
+
175
+ facetCounts.push(AboutYou::SDK::Model::FacetCount.new(facet, jsonTerm["count"]))
176
+ end
177
+
178
+ return facetCounts;
179
+ end
180
+
181
+ ###
182
+ # {@inheritdoc}
183
+ #
184
+ # @return integer[]
185
+ ###
186
+ def createFacetTypes(jsonArray)
187
+
188
+ return jsonArray;
189
+ end
190
+
191
+ ###
192
+ # {@inheritdoc}
193
+ #
194
+ # @return ShopApi\Model\Image
195
+ ###
196
+ def createImage(jsonObject)
197
+
198
+ AboutYou::SDK::Model::Image.createFromJson(jsonObject);
199
+ end
200
+
201
+ ###
202
+ # {@inheritdoc}
203
+ #
204
+ # @return ShopApi\Model\Product
205
+ ###
206
+ def createProduct(jsonObject)
207
+
208
+ AboutYou::SDK::Model::Product.createFromJson(jsonObject, self, self.shopApi.appId());
209
+ end
210
+
211
+ ###
212
+ # {@inheritdoc}
213
+ #
214
+ # @return ShopApi\Model\VariantsResult
215
+ ###
216
+ def createVariantsResult(jsonObject, query)
217
+
218
+ variants = Hash.new
219
+ errors = Array(nil);
220
+ productIds = Array(nil);
221
+ productSearchResult = false;
222
+
223
+ jsonObject.each do |id, data|
224
+ if (data["error_code"])
225
+ errors.push(id)
226
+ else
227
+ variants[data["id"]] = data["product_id"]
228
+
229
+ productIds.push(data["product_id"])
230
+ end
231
+ end
232
+
233
+ if (productIds.count > 0)
234
+ productIds = productIds.uniq;
235
+ # search products for valid variants
236
+ productSearchResult = self.shopApi.
237
+ fetchProductsByIds(
238
+ productIds,[
239
+ AboutYou::SDK::Criteria::ProductFields::ATTRIBUTES_MERGED,
240
+ AboutYou::SDK::Criteria::ProductFields::BRAND,
241
+ AboutYou::SDK::Criteria::ProductFields::CATEGORIES,
242
+ AboutYou::SDK::Criteria::ProductFields::DEFAULT_IMAGE,
243
+ AboutYou::SDK::Criteria::ProductFields::DEFAULT_VARIANT,
244
+ AboutYou::SDK::Criteria::ProductFields::DESCRIPTION_LONG,
245
+ AboutYou::SDK::Criteria::ProductFields::DESCRIPTION_SHORT,
246
+ AboutYou::SDK::Criteria::ProductFields::IS_ACTIVE,
247
+ AboutYou::SDK::Criteria::ProductFields::IS_SALE,
248
+ AboutYou::SDK::Criteria::ProductFields::MAX_PRICE,
249
+ AboutYou::SDK::Criteria::ProductFields::MIN_PRICE,
250
+ AboutYou::SDK::Criteria::ProductFields::VARIANTS
251
+ ]
252
+ )
253
+ end
254
+
255
+ return AboutYou::SDK::Model::VariantsResult.create(variants, errors, productSearchResult);
256
+ end
257
+
258
+ ###
259
+ # {@inheritdoc}
260
+ #
261
+ # @return ShopApi\Model\Product
262
+ ###
263
+ def createSingleProduct(jsonObject)
264
+
265
+ return self.createProduct(jsonObject);
266
+ end
267
+
268
+ ###
269
+ # {@inheritdoc}
270
+ #
271
+ # @return ShopApi\Model\ProductsResult
272
+ ###
273
+ def createProductsResult(jsonObject, query)
274
+ AboutYou::SDK::Model::ProductsResult.createFromJson(jsonObject, self);
275
+ end
276
+
277
+ ###
278
+ # {@inheritdoc}
279
+ #
280
+ # @return ShopApi\Model\ProductsEansResult
281
+ ###
282
+ def createProductsEansResult(jsonObject)
283
+
284
+ return AboutYou::SDK::Model::ProductsEansResult.createFromJson(jsonObject, self)
285
+ end
286
+
287
+ ###
288
+ # {@inheritdoc}
289
+ #
290
+ # @return ShopApi\Model\ProductSearchResult
291
+ ###
292
+ def createProductSearchResult(jsonObject, query)
293
+
294
+ return AboutYou::SDK::Model::ProductSearchResult.createFromJson(jsonObject, self);
295
+ end
296
+
297
+ ###
298
+ # {@inheritdoc}
299
+ #
300
+ # @return ShopApi\Model\CategoriesResult
301
+ ###
302
+ def createCategoriesResult(jsonObject, queryParams)
303
+ return AboutYou::SDK::Model::CategoriesResult.createFromJson(jsonObject, queryParams["ids"], self)
304
+ end
305
+
306
+ ###
307
+ # {@inheritdoc}
308
+ #
309
+ # @return string[]
310
+ ###
311
+ def createSuggest(jsonArray)
312
+
313
+ return jsonArray;
314
+ end
315
+
316
+ ###
317
+ # {@inheritdoc}
318
+ #
319
+ # @return ShopApi\Model\Variant
320
+ ###
321
+ def createVariant(jsonObject, product)
322
+
323
+ AboutYou::SDK::Model::Variant.createFromJson(jsonObject, self, product);
324
+ end
325
+
326
+ ###
327
+ # {@inheritdoc}
328
+ #
329
+ # @return ShopApi\Model\Order
330
+ ###
331
+ def createOrder(jsonObject)
332
+
333
+ basket = self.createBasket(jsonObject["basket"]);
334
+
335
+ return AboutYou::SDK::Model::Basket.new(jsonObject["order_id"], basket);
336
+ end
337
+
338
+ ###
339
+ # {@inheritdoc}
340
+ #
341
+ # @return ShopApi\Model\InitiateOrder
342
+ ###
343
+ def initiateOrder(jsonObject)
344
+
345
+ return AboutYou::SDK::Model::InitiateOrder.createFromJson(jsonObject);
346
+ end
347
+
348
+ ###
349
+ # {@inheritdoc}
350
+ #
351
+ # @return ShopApi\Model\App[]
352
+ ###
353
+ def createChildApps(jsonObject)
354
+
355
+ apps = Hash.new
356
+ jsonObject["child_apps"].each do |jsonApp|
357
+ app = self.createApp(jsonApp);
358
+ key = app.id();
359
+ apps[key] = app;
360
+ end
361
+
362
+ return apps;
363
+ end
364
+
365
+ ###
366
+ # {@inheritdoc}
367
+ #
368
+ # @return ShopApi\Model\App
369
+ ###
370
+ def createApp(jsonObject)
371
+
372
+ return AboutYou::SDK::Model::App.createFromJson(jsonObject);
373
+ end
374
+
375
+
376
+
377
+ def initializeCategoryManager(jsonObject)
378
+
379
+ return self.categoryManager().parseJson(jsonObject, self);
380
+ end
381
+
382
+ def updateFacetManager(jsonObject, query)
383
+
384
+ facets = self.createFacetsList(jsonObject, query);
385
+ self.facetManager.facets=(facets);
386
+ end
387
+
388
+ ###
389
+ # {@inheritdoc}
390
+ #
391
+ # @return ShopApi\Model\ProductSearchResult\PriceRange[]
392
+ ###
393
+ def createPriceRanges(jsonObject)
394
+
395
+ priceRanges = Array(nil);
396
+ jsonObject["ranges"].each do |range|
397
+ priceRanges.push(AboutYou::SDK::Model::PriceRange.createFromJson(range))
398
+ end
399
+
400
+ return priceRanges;
401
+ end
402
+
403
+ ###
404
+ # {@inheritdoc}
405
+ #
406
+ # @return ShopApi\Model\ProductSearchResult\SaleCounts
407
+ ###
408
+ def createSaleFacet(jsonObject)
409
+
410
+ return AboutYou::SDK::Model::SaleCounts.createFromJson(jsonObject);
411
+ end
412
+
413
+ ###
414
+ # {@inheritdoc}
415
+ ###
416
+ def createCategoriesFacets(jsonArray)
417
+ counts = Array(nil);
418
+ jsonArray.each do |item|
419
+ categoryId = item["term"];
420
+ counts[categoryId] = item["count"];
421
+ end
422
+
423
+ # fetch all categories from API
424
+ flattenCategories = self.shopApi().fetchCategoriesByIds().categories
425
+ flattenCategories.each do |id ,category|
426
+ if (counts[category.id])
427
+ category.productCount = counts[category.id]
428
+ if (flattenCategories[String(category.parentId)])
429
+ parent = flattenCategories[String(category.parentId)];
430
+ parent.addChild(category);
431
+ category.parent=parent
432
+ end
433
+ else
434
+ flattenCategories.delete(id)
435
+ end
436
+ end
437
+ return flattenCategories;
438
+ end
439
+ def preHandleError(json, resultKey, isMultiRequest)
440
+
441
+ if (resultKey == 'basket' && json["order_lines"])
442
+ return false;
443
+ end
444
+
445
+ if (isMultiRequest)
446
+ return 'ShopApi\Model\ResultError(json)';
447
+ end
448
+
449
+ raise 'ResultErrorException(json)';
450
+ end
451
+ end
452
+ end
453
+ end
454
+ end