aboutyou-sdk 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDI2YWFmYTE1OWEzMDIxM2ZiYzgxN2IwNWExOTc1MjM5MjI3MWRkNw==
4
+ NjE0NzJjNmJjNzUxMWEyMzA4OGE4NDUxYzFjMDc1NzhmNTI1MTE0Yg==
5
5
  data.tar.gz: !binary |-
6
- OTMyZGRkNjUzOWRiZDAwMGFmMTczMTJjZWMxMDdiOTYxY2IzNThmYg==
6
+ NDdhMzIxNzM5OTdmMmMxZGI2YzBiOThkNTk5OTc2NDkyMThjMjMzNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDQ4N2MwZjIzODI4NWVmNzUyZDhhZGM3ZTI3ZWM4NWE4MzMxMzI3MTVkNzA3
10
- MjMwNDk2NmZlYzBhNjhmOGJhZjRjMDAzN2FhODhkODkwNDQyYjU3Y2QzODRi
11
- NTFlMzM2MjU4MDZiNDBjZGU2YzI1NDQ5NzhmYjI3MDVkOTI4NTI=
9
+ MjRkMWY0ODQyNWYzOTFlMTY5MjhhOWMxNmRjN2I2N2EyMTMxMmZjY2Y0NDBl
10
+ OGRmYTI0YmJiNmIwZWQzOWZjZmM3MTJhM2E0M2M1OTZkMDg1NWMyZDliNDcz
11
+ MzdkY2Y1YWEyOGZlNWIzZGIzOGQ1Y2I5OGM1MzE1OTFlNDk2N2E=
12
12
  data.tar.gz: !binary |-
13
- OWZiNzIxNWNiOWNlMTRlZjY3OTE0Y2ZlYzI2ZGE1ZmZhYTRmZjUwZDM4ZGVh
14
- NTg2YTljMzM3NGQyYTViM2FjNTBiMjkyMTFmYzQ3OTQxY2U5MjAwODU2NDM1
15
- MTYzYWViOWMzNTM2NTNmZTgzNzlhOGQwODVlNmY2YTM1MzZiYTE=
13
+ Njk4Mjg0MDJlMThjNDdiZmNiZjRhODFlMmEzZjc1NTc5MjQ4NWQwNDFlYWY3
14
+ ZDY4YTBmM2QwYzRlNmY1MWUzYTA5ZDZhNmFkNjcyODdmMGEzYWY5MmU1NGQw
15
+ MDY5ODIwZmYzMDI2M2EwNWQzZTYxNjQyNjUzYjY4ZmNhYWQ4OWU=
data/lib/aboutyou-sdk.rb CHANGED
@@ -303,7 +303,7 @@ class AY
303
303
  # we allow to pass a single ID instead of an array
304
304
  ids = Array(ids)
305
305
  result = query.fetchProductsByIds(ids, fields).executeSingle
306
- productsNotFound = result.getProductsNotFound
306
+ productsNotFound = result.productsNotFound
307
307
 
308
308
  if !productsNotFound.empty? && self.logger
309
309
  self.logger.warning('products not found: appid=' + self.appId + ' product ids=[' + productsNotFound.join(',') + ']')
@@ -561,23 +561,18 @@ module AboutYou
561
561
  # - a Hash containing pairs of category_id => category
562
562
  ###
563
563
  def createCategoriesFacets(jsonArray)
564
- counts = []
564
+ categoryManager =self.categoryManager
565
+
566
+ flattenCategories = {}
565
567
  jsonArray.each do |item|
566
- counts[item["term"]] = item["count"]
567
- end
568
+ id = item["term"]
569
+ category = categoryManager.category(id)
570
+ next unless category
568
571
 
569
- # fetch all categories from API
570
- flattenCategories = self.shopApi.fetchCategoriesByIds.categories
571
- flattenCategories.each do |id ,category|
572
- if counts[category.id]
573
- category.productCount = counts[category.id]
574
- if flattenCategories[String(category.parentId)]
575
- category.parent = flattenCategories[String(category.parentId)].addChild(category)
576
- end
577
- else
578
- flattenCategories.delete(id)
579
- end
572
+ category.productCount = item["count"]
573
+ flattenCategories[id] = category
580
574
  end
575
+
581
576
  flattenCategories
582
577
  end
583
578
 
@@ -29,11 +29,13 @@ module AboutYou
29
29
  # * *Args* :
30
30
  # - +cache+ -> The cache client set on the AY class
31
31
  # - +appId+ -> The App-Id of the App
32
+ # - +shopApi+ -> an Instance of AY
32
33
  #
33
34
  # * *Returns* :
34
35
  # - Instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
35
36
  ###
36
37
  def initialize(cache = nil, appId = '', shopApi)
38
+ self.categories = {}
37
39
  self.shopApi = shopApi
38
40
  self.cache = cache
39
41
  self.cacheKey = 'AY:SDK:' + String(appId) + ':categories'
@@ -73,15 +75,11 @@ module AboutYou
73
75
  # - Instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
74
76
  ###
75
77
  def parseJson(jsonObject, factory)
76
- unless self.categories
77
- self.categories = {}
78
-
79
- if jsonObject
80
- self.cacheCategories(jsonObject)
81
- self.parentChildIds = jsonObject["parent_child"]
82
- jsonObject["ids"].each do |id, jsonCategory|
83
- self.categories[id] = factory.createCategory(jsonCategory)
84
- end
78
+ if jsonObject && self.categories.empty?
79
+ self.cacheCategories(jsonObject) if self.cache
80
+ self.parentChildIds = jsonObject["parent_child"]
81
+ jsonObject["ids"].each do |id, jsonCategory|
82
+ self.categories[id] = factory.createCategory(jsonCategory)
85
83
  end
86
84
  end
87
85
  end
@@ -91,10 +89,10 @@ module AboutYou
91
89
  # categories or not
92
90
  #
93
91
  # * *Returns* :
94
- # - a boolean which determines whether this catManager has read-in categories or not
92
+ # - a boolean which determines whether this Category Manager has read-in categories or not
95
93
  ###
96
94
  def isEmpty
97
- self.categories == nil
95
+ self.categories.empty?
98
96
  end
99
97
 
100
98
  ###
@@ -25,8 +25,8 @@ module AboutYou
25
25
  def initialize(ids)
26
26
  self.facets = Hash.new
27
27
  self.groups = Hash.new
28
- ids.each do |facetIds|
29
- if !(facetIds.is_a? Hash)
28
+ ids.each do |key, facetIds|
29
+ unless facetIds.is_a? Array
30
30
  raise '\InvalidArgumentException! ids must be an associative array of array: [$groupId => [$facetId,...],...]'
31
31
  end
32
32
  end
@@ -67,30 +67,27 @@ module AboutYou
67
67
  end
68
68
 
69
69
  def fetch
70
-
71
- unless self.facets
72
- return
73
- end
74
- self.ids.each do |idPair|
75
-
76
- idPair.each do |groupId, facetIds|
77
- facet = self.class.facetManager.facet(groupId, facetIds[0])
70
+ unless self.facets && self.facets.empty?
71
+ return
72
+ end
73
+
74
+ self.ids.each do |groupId, facetIds|
75
+ facet = self.class.facetManager.facet(groupId, facetIds[0])
78
76
 
79
- unless facet
80
- # TODO: error handling
81
- next
82
- end
77
+ unless facet
78
+ # TODO: error handling
79
+ next
80
+ end
83
81
 
84
- if @groups[groupId]
85
- group = @groups[groupId]
86
- else
87
- group = AboutYou::SDK::Model::FacetGroup.new(groupId, facet.groupName)
88
- @groups[groupId] = group
89
- end
90
- group.addFacet(facet);
91
- self.facets[facet.uniqueKey] = facet;
92
- end
82
+ if @groups[groupId]
83
+ group = @groups[groupId]
84
+ else
85
+ group = AboutYou::SDK::Model::FacetGroup.new(groupId, facet.groupName)
86
+ @groups[groupId] = group
93
87
  end
88
+ group.addFacet(facet);
89
+ self.facets[facet.uniqueKey] = facet;
90
+ end
94
91
  end
95
92
 
96
93
  ###
@@ -217,11 +214,11 @@ module AboutYou
217
214
 
218
215
  def self.mergeFacetIds(facetIdsArray)
219
216
 
220
- ids = Array(nil);
221
- facetIdsArray.each do |facetIds|
222
- facetIds.each do |groupId,facetIds|
217
+ ids = {}
218
+ facetIdsArray.each do |facetIdsHash|
219
+ facetIdsHash.each do |groupId, facetIds|
223
220
  if (ids[Integer(groupId)])
224
- ids[Integer(groupId)] = ids[Integer(groupId)] + facetIds;
221
+ ids[Integer(groupId)] = (ids[Integer(groupId)] + facetIds).uniq
225
222
  else
226
223
  ids[Integer(groupId)] = facetIds;
227
224
  end
@@ -2,73 +2,123 @@ module AboutYou
2
2
  module SDK
3
3
  module Model
4
4
  module FacetManager
5
+ ###
6
+ # This class is responsible for handling all the logic when working with
7
+ # Facets
8
+ #
9
+ # author:: Collins GmbH & Co KG
10
+ ###
5
11
  class DefaultFacetManager
6
-
7
- DEFAULT_CACHE_DURATION = 7200;
12
+ # the duration for the cached values to live
13
+ DEFAULT_CACHE_DURATION = 7200;
8
14
 
9
- # @var Facet[][] */
10
- @facets
11
- attr_accessor :facets
15
+ # a Hash containing instances of AboutYou::SDK::Model::Facet
16
+ attr_accessor :facets
17
+ # an Instance of a class of AboutYou::SDK::CacheProvider
18
+ attr_accessor :cache
19
+ # The key for which the values are stored in the cache
20
+ attr_accessor :cacheKey
21
+ # an Instance of AY
22
+ attr_accessor :shopApi
12
23
 
13
- # @var Cache */
14
- @cache;
15
- attr_accessor :cache
16
-
17
- # @var string */
18
- attr_accessor :cacheKey
19
- attr_accessor :shopApi
24
+ ###
25
+ # the Constructor for the DefaultFacetManager class
26
+ #
27
+ # * *Args* :
28
+ # - +cache+ -> The cache client set on the AY class
29
+ # - +appId+ -> The App-Id of the App
30
+ # - +shopApi+ -> an Instance of AY
31
+ #
32
+ # * *Returns* :
33
+ # - Instance of AboutYou::SDK::Model::CategoryManager::DefaultFacetManager
34
+ ###
35
+ def initialize(cache = nil, appId = '', shopApi)
36
+ self.cache = cache
37
+ self.facets = {}
38
+ self.shopApi = shopApi
39
+ self.cacheKey = 'AY:SDK:' + String(appId) + ':facets'
20
40
 
21
- ###
22
- # @param Cache $cache
23
- # @param string $appId
24
- ###
25
- def initialize(cache = nil, appId = '', shopApi)
26
-
27
- self.cache = cache;
28
- self.shopApi = shopApi
29
- self.cacheKey = 'AY:SDK:' + String(appId) + ':facets';
41
+ self.loadCachedFacets
42
+ end
30
43
 
31
- self.loadCachedFacets
44
+ ###
45
+ # Gets the cached Facets for this app
46
+ ###
47
+ def loadCachedFacets
48
+ parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance) if self.cache && self.cache.exists(self.cacheKey)
49
+ end
50
+
51
+ ###
52
+ # This method caches the currently read-in Facets
53
+ ###
54
+ def cacheFacets(jsonObject)
55
+ self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION)
56
+ end
57
+
58
+ ###
59
+ # This method clears the cache
60
+ ###
61
+ def clearCache
62
+ self.cache.delete(self.cacheKey)
63
+ end
64
+
65
+ ###
66
+ # this method parses a json object received from the api and
67
+ # creates models from it
68
+ #
69
+ # * *Args* :
70
+ # - +jsonObject+ -> the jsonObject received from the api
71
+ # - +factory+ -> the model factory used for creating the models
72
+ #
73
+ # * *Returns* :
74
+ # - Instance of AboutYou::SDK::Model::CategoryManager::DefaultFacetManager
75
+ ###
76
+ def parseJson(jsonObject, factory, query=nil)
77
+ if self.facets.empty? && jsonObject
78
+ self.cacheFacets(jsonObject) if self.cache
79
+ self.facets = factory.createFacetsList(jsonObject, query)
32
80
  end
81
+ end
82
+
83
+ ###
84
+ # this method checks whether this facet manager has read-in
85
+ # facets or not
86
+ #
87
+ # * *Returns* :
88
+ # - a boolean which determines whether this facet Manager has read-in facets or not
89
+ ###
90
+ def isEmpty
91
+ self.facets.empty?
92
+ end
33
93
 
34
- def loadCachedFacets
35
- parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance) if self.cache && self.cache.exists(self.cacheKey)
36
- end
37
-
38
- def cacheFacets(jsonObject)
39
- self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION)
40
- end
41
-
42
- def clearCache
43
- self.cache.delete(self.cacheKey)
44
- end
45
-
46
- def parseJson(jsonObject, factory, query=nil)
47
- unless self.facets
48
- if jsonObject
49
- self.cacheFacets(jsonObject)
50
- self.facets = factory.createFacetsList(jsonObject, query)
51
- end
52
- end
53
- end
54
-
55
- def isEmpty
56
- return (self.facets.empty?)
57
- end
58
-
59
- ###
60
- # {@inheritdoc}
61
- ###
62
- def facet(groupId, id)
63
-
64
- lookupKey = AboutYou::SDK::Model::Facet.new(0, "", [], 0, "").uniqueKey(groupId, id);
65
- return (self.facets[lookupKey]) ? self.facets[lookupKey] : nil;
66
- end
67
-
68
- def facetsByGroupIds(groupIds = [])
69
- groupIds = groupIds.map { |gId| Integer(gId) }
70
- self.facets.select { |key, facet| groupIds.include?(facet.groupId) }
71
- end
94
+ ###
95
+ # This method is used for getting a facet model by a given groupId and facetId
96
+ #
97
+ # * *Args* :
98
+ # - +groupId+ -> the group id for which a facet model should be returned
99
+ # - +id+ -> the facet id for which a facet model should be returned
100
+ #
101
+ # * *Returns* :
102
+ # - either an instance of AboutYou::SDK::Model::Facet or nil if +groupId+ or +id+ is not found
103
+ ###
104
+ def facet(groupId, id)
105
+ lookupKey = AboutYou::SDK::Model::Facet.new(0, "", [], 0, "").uniqueKey(groupId, id);
106
+ self.facets[lookupKey] ? self.facets[lookupKey] : nil
107
+ end
108
+
109
+ ###
110
+ # This method is used for getting all facets for an Array of group ids
111
+ #
112
+ # * *Args* :
113
+ # - +groupIds+ -> an Array containing group ids for which facet models should be returned
114
+ #
115
+ # * *Returns* :
116
+ # - a Hash containing pairs of facetId => instance of AboutYou::SDK::Model::Facet
117
+ ###
118
+ def facetsByGroupIds(groupIds = [])
119
+ groupIds = groupIds.map { |gId| Integer(gId) }
120
+ self.facets.select { |key, facet| groupIds.include?(facet.groupId) }
121
+ end
72
122
  end
73
123
  end
74
124
  end
@@ -79,7 +79,6 @@ module AboutYou
79
79
  end
80
80
 
81
81
  def self.parseVariants(jsonObject, factory, product, attributeName = 'variants')
82
-
83
82
  variants = Array(nil);
84
83
  if (jsonObject[attributeName])
85
84
  jsonObject[attributeName].each do |jsonVariant|
@@ -124,7 +123,6 @@ module AboutYou
124
123
  if (ids === nil)
125
124
  ids = self.parseFacetIdsInBrand(jsonObject);
126
125
  end
127
-
128
126
  return (ids != nil) ? ids : Array(nil);
129
127
  end
130
128
 
@@ -139,7 +137,7 @@ module AboutYou
139
137
 
140
138
  def parseAttributesJson(attributesJsonObject)
141
139
 
142
- ids = Hash.new
140
+ ids = {}
143
141
 
144
142
  attributesJsonObject.each do |group, facetIds|
145
143
  gid = group[11..group.length] # prefix "attributes"
@@ -148,33 +146,31 @@ module AboutYou
148
146
  facetIds = Array(facetIds)
149
147
  ids[gid] = facetIds;
150
148
  end
151
-
152
- return ids;
149
+ return ids
153
150
  end
154
151
 
155
152
  def parseFacetIdsInVariants(jsonObject)
156
153
 
157
154
  if (jsonObject["variants"])
158
- ids = Array(nil);
155
+ ids = []
159
156
  jsonObject["variants"].each do |variant|
160
- ids.push(parseAttributesJson(variant["attributes"]));
157
+ ids.push(parseAttributesJson(variant["attributes"]))
161
158
  end
162
- ids = AboutYou::SDK::Model::FacetGroupSet.mergeFacetIds(ids);
163
-
164
- return ids;
159
+ ids = AboutYou::SDK::Model::FacetGroupSet.mergeFacetIds(ids)
160
+ return ids
165
161
  elsif (jsonObject["default_variant"])
166
- ids = parseAttributesJson(jsonObject["default_variant"]["attributes"]);
162
+ ids = parseAttributesJson(jsonObject["default_variant"]["attributes"])
167
163
 
168
- return ids;
164
+ return ids
169
165
  end
170
166
 
171
- return nil;
167
+ nil
172
168
  end
173
169
 
174
170
  def parseFacetIdsInBrand(jsonObject)
175
171
 
176
- if (!jsonObject["brand_id"])
177
- return nil;
172
+ unless jsonObject["brand_id"]
173
+ return nil
178
174
  end
179
175
 
180
176
  Hash['0' => jsonObject["brand_id"]];
@@ -186,7 +182,6 @@ module AboutYou
186
182
  if (self.facetIds.empty?)
187
183
  raise 'ShopApi\Exception\RuntimeException! To use this method, you must add the field ProductFields::ATTRIBUTES_MERGED to the "product search" or "products by ids"'
188
184
  end
189
-
190
185
  self.facetGroups = AboutYou::SDK::Model::FacetGroupSet.new(self.facetIds);
191
186
  end
192
187
 
@@ -195,11 +190,10 @@ module AboutYou
195
190
  ###
196
191
  def facetGroupSet()
197
192
 
198
- if (!self.facetGroups)
193
+ unless @facetGroups
199
194
  generateFacetGroupSet
200
195
  end
201
-
202
- self.facetGroups;
196
+ @facetGroups
203
197
  end
204
198
 
205
199
  ###
@@ -458,7 +452,6 @@ module AboutYou
458
452
  # @return \Collins\ShopApi\Model\Facet
459
453
  ###
460
454
  def brand()
461
-
462
455
  self.facetGroupSet().facet(AboutYou::SDK::Constants::FACET_BRAND, self.brandId);
463
456
  end
464
457
 
@@ -36,7 +36,7 @@ module AboutYou
36
36
  saleCounts = self.new(jsonObject["total"], jsonObject["other"], jsonObject["missing"]);
37
37
  saleCounts.parseTerms(jsonObject["terms"]);
38
38
 
39
- return self;
39
+ saleCounts
40
40
  end
41
41
 
42
42
  ###
@@ -42,7 +42,7 @@ module AboutYou
42
42
  ###
43
43
  # @return integer[] array of product ids
44
44
  ###
45
- def getProductsNotFound()
45
+ def productsNotFound()
46
46
 
47
47
  return self.idsNotFound;
48
48
  end
@@ -139,15 +139,14 @@ module AboutYou
139
139
 
140
140
  def parseFacetIds(jsonObject)
141
141
 
142
- ids = Array(nil)
142
+ ids = {}
143
143
  if !(jsonObject["attributes"].empty?)
144
144
  jsonObject["attributes"].each do |group, aIds|
145
145
  gid = group[11..group.length] # rm prefix "attributs_"
146
- ids.push(Hash[Integer(gid) => aIds]);
146
+ ids[Integer(gid)] = aIds
147
147
  end
148
148
  end
149
-
150
- return ids;
149
+ return ids
151
150
  end
152
151
 
153
152
 
@@ -191,7 +191,7 @@ module AboutYou
191
191
  # - Instance of AboutYou::SDK::Query
192
192
  ###
193
193
  def requireCategoryTree(fetchForced = false)
194
- if fetchForced || !self.factory.categoryManager.isEmpty
194
+ unless fetchForced || self.factory.categoryManager.isEmpty
195
195
  return self
196
196
  end
197
197
 
@@ -1,5 +1,5 @@
1
1
  require 'sinatra'
2
2
 
3
3
  get "/" do
4
- erb :'testDocExample'
4
+ erb :'docExampleProducts'
5
5
  end
@@ -1,6 +1,6 @@
1
1
  require 'sinatra'
2
2
  require 'pry_debug'
3
- require 'aboutyou-sdk'
3
+ require_relative '../lib/aboutyou-sdk.rb'
4
4
 
5
5
  class AY
6
6
  attr_accessor :result
@@ -21,12 +21,14 @@ class AY
21
21
  end
22
22
  end
23
23
 
24
- shop = AY.new(281, 'e6068c4ca020262613d06122663cb300')
24
+ shop = AY.new(281, 'e6068c4ca020262613d06122663cb300', "session_id", AboutYou::SDK::CacheProvider::Redis.new(Redis.new))
25
25
  shop.result = ""
26
26
 
27
27
  variantResult = shop.fetchVariantsByIds([7832543])
28
28
  variantResult.variants.each do |key, variant|
29
- shop.result = shop.result + String(variant.id) + '<img src="' + variant.image.url + '">' + '</br>'
29
+ variant.facetIds.each do |fId|
30
+ shop.result = shop.result + String(fId)
31
+ end
30
32
  end
31
33
  get "/" do
32
34
  return shop.result
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aboutyou-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ferenc Beutel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: Use the Ruby SDK to build apps for the ABOUT YOU platform.
70
84
  email: ferenc1993beutel@gmx.de
71
85
  executables: []