aboutyou-sdk 0.0.13 → 0.0.14

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
- NDQzMDNhYThmZmVhMjk4MTQzZDMxZDBhMWE1MWQzZWYxY2EwYjgzYQ==
4
+ ZDBmNjhmNzliNGI3N2JjOTk4MDgxODc0NDBjNTYwZjI5N2RlNGIzZA==
5
5
  data.tar.gz: !binary |-
6
- MjJiMWEyZTg1ZTFkY2NhMjg4ZjY0NWY5M2Q0OWM1NGM1YjAyZmM0Yg==
6
+ YWZkODI5NmM1ODhhMDFiOWJkNTBmODI3ZjczYTIzYjRjZDJiMzc2OQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmU3ZDljM2UwYmE0ODgzZjNkZTk3MGRmOTFhNWEyYmVmMGVmYzAwMDQ2ZmYz
10
- ZjZkZmFlZjE1MTk0MTk3ZmU2NWZhMmEzOGY0ZmY3OTA0NmRmN2U4ZGZhMDY3
11
- ZWY3OWM5MjY4ODA1ZDU3YjA3ODg3NTk4NDNiOTk5Y2RiYmI0ZWM=
9
+ MDJhYjY1MGRhYjc3ZjEyOWM4YTNiZDlhMGY0YjZiOGM0Y2UxNWM5YjI1ZTQx
10
+ NzY4Y2JiMzljMDBlYzliNTVhYjY4ODNmN2ExYjQyMTlmMDBhY2M3NmUzYzBj
11
+ ZDg4YzdlOGZhNzA5Yjg0ZGZmZjdmOGE2YzQyNDc1MWM0MTE3NjI=
12
12
  data.tar.gz: !binary |-
13
- NGI0OWFkYTFiOGM4ZDM0MzhiODMxOGRhMzQ5NjIxMzdiMDdmZjQ4NmFkNDE2
14
- MmZhMDU4YmYxZmVkMmE4ODI2ZGQwMzgxZGYxYjJjYmI3MjI3OWUzYjAzN2Nk
15
- ZDdiYzQzN2E5ZDk5ZThlYjdjYWExYjMyZTA0ZGI5Y2JlMzFlYTY=
13
+ NDAxMWRlNjQyNDdkZWU4ZjU1M2I1OTQ3ZjExMTViNmEwMjFlYzA3NzcyZjg0
14
+ MDI2OTg2MmMzMmQzMTRkMmI1Y2U3NmY5ZTQxNjEyZGU5YTUzZjRjYzNiZTJi
15
+ NTAyMmYyNDUwMGZmMWE0NWM1N2E4ZTE2NDk2YTA4YmM0MTdlYzM=
@@ -1,6 +1,9 @@
1
1
  require 'httparty'
2
2
  require 'json'
3
3
  require 'require_all'
4
+ require 'dalli'
5
+ require 'redis'
6
+
4
7
 
5
8
  require_rel 'aboutyou-sdk'
6
9
 
@@ -60,11 +63,17 @@ class AY
60
63
  appPassword,
61
64
  sessionId = 'SESSION_ID',
62
65
  cache = nil,
63
- cacheServer= 'localhost:11211',
66
+ cacheServer= nil,
64
67
  apiEndPoint = AboutYou::SDK::Constants::API_ENVIRONMENT_LIVE,
65
68
  resultFactory = nil,
66
69
  logger = nil
67
- )
70
+ )
71
+
72
+ if cacheServer == nil
73
+ cacheServer = 'localhost:11211' if cache == 'memcache'
74
+ cacheServer = {:host => "localhost", :port => 6379} if cache == 'redis'
75
+ end
76
+
68
77
  self.sessionId = sessionId
69
78
  self.appId = appId
70
79
  self.appPassword = appPassword
@@ -74,21 +83,26 @@ class AY
74
83
  apiEndPoint,
75
84
  logger
76
85
  )
86
+
87
+ self.cache = Dalli::Client.new(cacheServer, {:value_max_bytes => 5242880}) if cache == 'memcache'
88
+ self.cache = Redis.new(cacheServer) if cache == 'redis'
77
89
 
90
+ if resultFactory
91
+ self.modelFactoryInstance = resultFactory
92
+ else
93
+ initDefaultFactory(self.cache)
94
+ end
95
+
78
96
  if apiEndPoint == AboutYou::SDK::Constants::API_ENVIRONMENT_STAGE
79
97
  self.baseImageUrl = (IMAGE_URL_STAGE)
80
98
  self.environment = AboutYou::SDK::Constants::API_ENVIRONMENT_STAGE
81
99
  elsif apiEndPoint == AboutYou::SDK::Constants::API_ENVIRONMENT_SANDBOX
82
100
  self.baseImageUrl = (IMAGE_URL_SANDBOX)
83
101
  self.environment = AboutYou::SDK::Constants::API_ENVIRONMENT_SANDBOX
84
- else
102
+ elsif apiEndPoint == AboutYou::SDK::Constants::API_ENVIRONMENT_LIVE
85
103
  self.baseImageUrl = (IMAGE_URL_LIVE)
104
+ self.environment = AboutYou::SDK::Constants::API_ENVIRONMENT_LIVE
86
105
  end
87
- if cache == 'memcache'
88
- require 'dalli'
89
- self.cache = Dalli::Client.new(cacheServer, {:value_max_bytes => 5242880})
90
- end
91
- resultFactory ? self.modelFactoryInstance = resultFactory : initDefaultFactory(self.cache)
92
106
  end
93
107
 
94
108
  ###
@@ -561,7 +575,7 @@ class AY
561
575
  # * *Args* :
562
576
  # - +baseImageUrl+ -> nil will reset to the default url, false to get relative urls, otherwise the url prefix
563
577
  ###
564
- def baseImageUrl=(baseImageUrl = nil)
578
+ def baseImageUrl= (baseImageUrl = nil)
565
579
 
566
580
  if !baseImageUrl
567
581
  @baseImageUrl = IMAGE_URL_LIVE
@@ -571,7 +585,7 @@ class AY
571
585
  @baseImageUrl = ''
572
586
  end
573
587
 
574
- self.modelFactory.baseImageUrl=(self.baseImageUrl)
588
+ self.modelFactory.baseImageUrl = self.baseImageUrl
575
589
  end
576
590
 
577
591
 
@@ -582,13 +596,12 @@ class AY
582
596
  # - +cache+ -> an Instance of the used cache [optional]
583
597
  ###
584
598
  def initDefaultFactory(cache = nil)
585
- resultFactory = AboutYou::SDK::Factory::DefaultModelFactory.new(self,
586
- AboutYou::SDK::Model::FacetManager::DefaultFacetManager.new(cache, self.appId, self),
587
- AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager.new(cache, self.appId, self)
599
+ self.modelFactoryInstance = AboutYou::SDK::Factory::DefaultModelFactory.new(self)
600
+
601
+ self.modelFactoryInstance.initializeManagers(
602
+ AboutYou::SDK::Model::FacetManager::DefaultFacetManager.new(cache, self.appId, self),
603
+ AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager.new(cache, self.appId, self)
588
604
  )
589
- resultFactory.baseImageUrl=(self.baseImageUrl)
590
-
591
- self.modelFactoryInstance=(resultFactory)
605
+ self.modelFactoryInstance.baseImageUrl=(self.baseImageUrl)
592
606
  end
593
-
594
607
  end
@@ -27,8 +27,11 @@ module AboutYou
27
27
  # * *Returns* :
28
28
  # - an instance of AboutYou::SDK::Factory::DefaultModelFactory
29
29
  ###
30
- def initialize(shopApi = nil, facetManager, categoryManager)
30
+ def initialize(shopApi = nil)
31
31
  self.shopApi = shopApi
32
+ end
33
+
34
+ def initializeManagers(facetManager, categoryManager)
32
35
  self.categoryManager = categoryManager
33
36
  self.facetManager = facetManager
34
37
  AboutYou::SDK::Model::FacetGroupSet.facetManager = self.facetManager
@@ -20,6 +20,7 @@ module AboutYou
20
20
  attr_accessor :cache
21
21
  # the cache key for the app
22
22
  attr_accessor :cacheKey
23
+ # instance of AY
23
24
  attr_accessor :shopApi
24
25
 
25
26
  ###
@@ -43,17 +44,18 @@ module AboutYou
43
44
  # Gets the cached Categories for this app
44
45
  ###
45
46
  def loadCachedCategories
46
- if self.cache
47
- parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance)
48
- end
47
+ parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance) if self.cache && self.cache.get(self.cacheKey)
49
48
  end
50
49
 
51
50
  ###
52
51
  # This method caches the currently read-in categories
53
52
  ###
54
53
  def cacheCategories(jsonObject)
55
- if self.cache
56
- self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION)
54
+ self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION) if self.cache.instance_of?(Dalli::Client)
55
+ if self.cache.instance_of?(Redis)
56
+ jsonObject = jsonObject.to_json unless jsonObject.is_a? String
57
+ self.cache.set(self.cacheKey, jsonObject)
58
+ self.cache.expire(self.cacheKey, DEFAULT_CACHE_DURATION)
57
59
  end
58
60
  end
59
61
 
@@ -61,9 +63,8 @@ module AboutYou
61
63
  # This method clears the cache
62
64
  ###
63
65
  def clearCache
64
- if self.cache
65
- self.cache.delete(self.cacheKey)
66
- end
66
+ self.cache.delete(self.cacheKey) if self.cache.instance_of?(Dalli::Client)
67
+ self.cache.del(self.cacheKey) if self.cache.instance_of?(Redis)
67
68
  end
68
69
 
69
70
  ###
@@ -78,12 +79,16 @@ module AboutYou
78
79
  # - Instance of AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
79
80
  ###
80
81
  def parseJson(jsonObject, factory)
81
- self.categories = {}
82
- if jsonObject
83
- self.cacheFacets(jsonObject)
84
- self.parentChildIds = jsonObject["parent_child"]
85
- jsonObject["ids"].each do |id, jsonCategory|
86
- self.categories[id] = factory.createCategory(jsonCategory)
82
+ unless self.categories
83
+ self.categories = {}
84
+ jsonObject = JSON.parse(jsonObject) if jsonObject.is_a? String
85
+
86
+ if jsonObject
87
+ self.cacheCategories(jsonObject)
88
+ self.parentChildIds = jsonObject["parent_child"]
89
+ jsonObject["ids"].each do |id, jsonCategory|
90
+ self.categories[id] = factory.createCategory(jsonCategory)
91
+ end
87
92
  end
88
93
  end
89
94
  end
@@ -27,41 +27,40 @@ module AboutYou
27
27
  self.cache = cache;
28
28
  self.shopApi = shopApi
29
29
  self.cacheKey = 'AY:SDK:' + String(appId) + ':facets';
30
- self.facets = Hash.new
31
30
 
32
- self.loadCachedFacets();
31
+ self.loadCachedFacets
33
32
  end
34
33
 
35
- def loadCachedFacets()
36
-
37
- if (self.cache)
38
- parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance)
39
- end
34
+ def loadCachedFacets
35
+ parseJson(self.cache.get(self.cacheKey), self.shopApi.modelFactoryInstance) if self.cache && self.cache.get(self.cacheKey)
40
36
  end
41
37
 
42
38
  def cacheFacets(jsonObject)
43
-
44
- if (self.cache)
45
- self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION);
46
- end
39
+ self.cache.set(self.cacheKey, jsonObject, DEFAULT_CACHE_DURATION) if self.cache.instance_of?(Dalli::Client)
40
+ if self.cache.instance_of?(Redis)
41
+ jsonObject = jsonObject.to_json unless jsonObject.is_a?(String)
42
+ self.cache.set(self.cacheKey, jsonObject)
43
+ self.cache.expire(self.cacheKey, DEFAULT_CACHE_DURATION)
44
+ end
47
45
  end
48
46
 
49
- def clearCache()
50
-
51
- if (self.cache)
52
- self.cache.delete(self.cacheKey);
53
- end
47
+ def clearCache
48
+ self.cache.delete(self.cacheKey) if self.cache.instance_of?(Dalli::Client)
49
+ self.cache.del(self.cacheKey) if self.cache.instance_of?(Redis)
54
50
  end
55
51
 
56
52
  def parseJson(jsonObject, factory, query=nil)
57
- if jsonObject
58
- self.cacheFacets(jsonObject)
59
- self.facets = factory.createFacetsList(jsonObject, query)
53
+ unless self.facets
54
+ jsonObject = JSON.parse(jsonObject) if jsonObject.is_a?(String)
55
+ if jsonObject
56
+ self.cacheFacets(jsonObject)
57
+ self.facets = factory.createFacetsList(jsonObject, query)
58
+ end
60
59
  end
61
60
  end
62
61
 
63
- def isEmpty()
64
- return (self.facets.empty?);
62
+ def isEmpty
63
+ return (self.facets.empty?)
65
64
  end
66
65
 
67
66
  ###
@@ -124,14 +124,12 @@ module AboutYou
124
124
  def fetchProductsByIds(ids,fields = [])
125
125
 
126
126
  super(ids, fields)
127
-
128
127
  if AboutYou::SDK::Criteria::ProductFields.requiresCategories(fields)
129
128
  self.requireCategoryTree
130
129
  end
131
130
  if AboutYou::SDK::Criteria::ProductFields.requiresFacets(fields)
132
131
  self.requireFacets
133
132
  end
134
-
135
133
  return self
136
134
  end
137
135
 
@@ -193,8 +191,7 @@ module AboutYou
193
191
  # - Instance of AboutYou::SDK::Query
194
192
  ###
195
193
  def requireCategoryTree(fetchForced = false)
196
-
197
- unless fetchForced || self.factory.categoryManager.isEmpty
194
+ if fetchForced || !self.factory.categoryManager.isEmpty
198
195
  return self
199
196
  end
200
197
 
@@ -4,11 +4,7 @@ require_relative'../lib/aboutyou-sdk.rb'
4
4
 
5
5
  get "/doCache" do
6
6
  shop = AY.new(281, 'e6068c4ca020262613d06122663cb300', 'sessionId', 'memcache')
7
- shop.fetchProductsByIds(
8
- [661132, 660971, 661367, 661361],
9
- [AboutYou::SDK::Criteria::ProductFields::CATEGORIES,
10
- AboutYou::SDK::Criteria::ProductFields::VARIANTS]
11
- )
7
+ shop.fetchProductsByIds([661132, 660971, 661367, 661361], [AboutYou::SDK::Criteria::ProductFields::CATEGORIES])
12
8
  end
13
9
 
14
10
  get "/getCache" do
@@ -0,0 +1,31 @@
1
+ require 'sinatra'
2
+ require "pry_debug"
3
+ require_relative'../lib/aboutyou-sdk.rb'
4
+
5
+ get "/doCache" do
6
+ shop = AY.new(281, 'e6068c4ca020262613d06122663cb300', 'sessionId', 'redis')
7
+ shop.fetchProductsByIds([661132, 660971, 661367, 661361], [AboutYou::SDK::Criteria::ProductFields::CATEGORIES])
8
+ end
9
+
10
+ get "/getCache" do
11
+ result = ""
12
+ shop = AY.new(281, 'e6068c4ca020262613d06122663cb300', 'sessionId', 'redis')
13
+
14
+ shop.fetchCategoryTree.categories.each do |key, cate|
15
+ result = result + cate.name + ' '
16
+ end
17
+
18
+ return result
19
+ end
20
+
21
+ get "/noCache" do
22
+ result = ""
23
+ shop = AY.new(281, 'e6068c4ca020262613d06122663cb300', 'sessionId', 'redis')
24
+ shop.cache.delete('AY:SDK:281:categories')
25
+
26
+ shop.fetchCategoryTree.categories.each do |key, cate|
27
+ result = result + cate.name + ' '
28
+ end
29
+
30
+ return result
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aboutyou-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ferenc Beutel
@@ -128,6 +128,7 @@ files:
128
128
  - tests/testProductSearchResult.rb
129
129
  - tests/testProductsByEans.rb
130
130
  - tests/testProductsByIds.rb
131
+ - tests/testRedisCache.rb
131
132
  - tests/testSuggest.rb
132
133
  - tests/testVariantsByIds.rb
133
134
  homepage: https://developer.aboutyou.de