Class: AboutYou::SDK::Model::FacetManager::DefaultFacetManager

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/Model/FacetManager/default_facet_manager.rb

Overview

This class is responsible for handling all the logic when working with Facets

author

Collins GmbH & Co KG

Constant Summary collapse

DEFAULT_CACHE_DURATION =

the duration for the cached values to live

7200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil, app_id = '', shop_api) ⇒ DefaultFacetManager

the Constructor for the DefaultFacetManager class

  • Args :

    • cache -> The cache client set on the AY class

    • app_id -> The App-Id of the App

    • shop_api -> an Instance of AY

  • Returns :

    • Instance of AboutYou::SDK::Model::CategoryManager::DefaultFacetManager



35
36
37
38
39
40
41
42
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 35

def initialize(cache = nil, app_id = '', shop_api)
  self.cache     = cache
  self.facets    = {}
  self.shop_api  = shop_api
  self.cache_key = 'AY:SDK:' + String(app_id) + ':facets'

  load_cached_facets
end

Instance Attribute Details

#cacheObject

an Instance of a class of AboutYou::SDK::CacheProvider



18
19
20
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 18

def cache
  @cache
end

#cache_keyObject

The key for which the values are stored in the cache



20
21
22
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 20

def cache_key
  @cache_key
end

#facetsObject

a Hash containing instances of AboutYou::SDK::Model::Facet



16
17
18
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 16

def facets
  @facets
end

#shop_apiObject

an Instance of AY



22
23
24
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 22

def shop_api
  @shop_api
end

Instance Method Details

#cache_facets(json_object) ⇒ Object

this method caches a given json_object

  • Args :

    • json_object -> the jsonObject received from the api

  • Returns :

    • True/False determining whether the setting was sucessfull or not



66
67
68
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 66

def cache_facets(json_object)
  cache.set(cache_key, json_object, DEFAULT_CACHE_DURATION)
end

#clear_cacheObject

This method clears the cache

  • Returns :

    • True/False determining whether the clearing was sucessfull or not



76
77
78
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 76

def clear_cache
  cache.delete(cache_key)
end

#empty?Boolean

this method checks whether this facet manager has read-in facets or not

  • Returns :

    • a boolean which determines whether this facet Manager has read-in facets or not

Returns:

  • (Boolean)


104
105
106
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 104

def empty?
  facets.empty?
end

#facet(group_id, id) ⇒ Object

This method is used for getting a facet model by a given group_id and facetId

  • Args :

    • group_id -> the group id for which a facet model should be returned

    • id -> the facet id for which a facet model should be returned

  • Returns :

    • either an instance of AboutYou::SDK::Model::Facet or nil if group_id or id is not found



118
119
120
121
122
123
124
125
126
127
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 118

def facet(group_id, id)
  look_up_key = AboutYou::SDK::Model::Facet.new(
    0,
    '',
    [],
    0,
    ''
  ).unique_key(group_id, id)
  facets[look_up_key] ? facets[look_up_key] : nil
end

#facets_by_group_ids(group_ids = []) ⇒ Object

This method is used for getting all facets for an Array of group ids

  • Args :

    • group_ids -> an Array containing group ids for which facet models should be returned

  • Returns :

    • a Hash containing pairs of facetId => instance of AboutYou::SDK::Model::Facet



138
139
140
141
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 138

def facets_by_group_ids(group_ids = [])
  group_ids = group_ids.map { |gId| Integer(gId) }
  facets.select { |_key, facet| group_ids.include?(facet.group_id) }
end

#load_cached_facetsObject

Gets the cached Categories for this app

  • Returns :

    • True/False determining whether the loading was sucessfull or not



50
51
52
53
54
55
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 50

def load_cached_facets
  parse_json(
    cache.get(cache_key),
    shop_api.model_factory_instance
  ) if cache && cache.exists(cache_key)
end

#parse_json(json_object, factory, query = nil) ⇒ Object

this method parses a json object received from the api and creates models from it

  • Args :

    • json_object -> the jsonObject received from the api

    • factory -> the model factory used for creating the models

  • Returns :

    • Instance of AboutYou::SDK::Model::CategoryManager::DefaultFacetManager



91
92
93
94
95
# File 'lib/AboutYou/Model/FacetManager/default_facet_manager.rb', line 91

def parse_json(json_object, factory, query = nil)
  return unless facets.empty? && json_object
  cache_facets(json_object) if cache
  self.facets = factory.create_facets_list(json_object, query)
end