Class: AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
- Inherits:
-
Object
- Object
- AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
- Defined in:
- lib/AboutYou/Model/CategoryManager/default_category_manager.rb
Overview
This class is responsible for handling all the logic when working with categories
- author
-
Collins GmbH & Co KG
Constant Summary collapse
- DEFAULT_CACHE_DURATION =
the duration for the cached values to live
7200
Instance Attribute Summary collapse
-
#cache ⇒ Object
the cache client.
-
#cache_key ⇒ Object
the cache key for the app.
-
#categories(ids = nil, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method is used for getting either all categories when
id
is nil or a Hash of all Category Models found for an Array of Category Ids. -
#parent_child_ids ⇒ Object
the parent child ids for the categories.
-
#shop_api ⇒ Object
instance of AY.
Instance Method Summary collapse
-
#cache_categories(json_object) ⇒ Object
this method caches a given json_object.
-
#categories_by_name(name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the subcategories for a given category id.
-
#category(id) ⇒ Object
This method is used for getting a category model by a given id.
-
#category_tree(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method returns the root categories for the read-in categories.
-
#clear_cache ⇒ Object
This method clears the cache.
-
#empty? ⇒ Boolean
this method checks whether this category manager has read-in categories or not.
-
#first_category_by_name(name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the first category which has a certain name.
-
#initialize(cache = nil, app_id = '', shop_api) ⇒ DefaultCategoryManager
constructor
the Constructor for the AY class.
-
#load_cached_categories ⇒ Object
Gets the cached Categories for this app.
-
#parse_json(json_object, factory) ⇒ Object
this method parses a json object received from the api and creates models from it.
-
#subcategories(id, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the subcategories for a given category id.
Constructor Details
#initialize(cache = nil, app_id = '', shop_api) ⇒ DefaultCategoryManager
the Constructor for the AY 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::DefaultCategoryManager
-
37 38 39 40 41 42 43 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 37 def initialize(cache = nil, app_id = '', shop_api) self.categories = {} self.shop_api = shop_api self.cache = cache self.cache_key = 'AY:SDK:' + String(app_id) + ':categories' load_cached_categories end |
Instance Attribute Details
#cache ⇒ Object
the cache client
20 21 22 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 20 def cache @cache end |
#cache_key ⇒ Object
the cache key for the app
22 23 24 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 22 def cache_key @cache_key end |
#categories(ids = nil, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method is used for getting either all categories when id
is nil or a Hash of all Category Models found for an Array of Category Ids
-
Args :
-
ids
-> an Array of category Ids -
activeOnly
-> determines whether the result should contain only active categories or not
-
-
Returns :
-
a Hash containing pairs of Category_ID => an Instance of AboutYou::SDK::Model::Category
-
16 17 18 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 16 def categories @categories end |
#parent_child_ids ⇒ Object
the parent child ids for the categories
18 19 20 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 18 def parent_child_ids @parent_child_ids end |
#shop_api ⇒ Object
instance of AY
24 25 26 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 24 def shop_api @shop_api end |
Instance Method Details
#cache_categories(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
-
67 68 69 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 67 def cache_categories(json_object) cache.set(cache_key, json_object, DEFAULT_CACHE_DURATION) end |
#categories_by_name(name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the subcategories for a given category id
-
Args :
-
id
-> an Array of category Ids -
activeOnly
-> determines whether the result should contain only active categories or not
-
-
Returns :
-
a Hash containing pairs of Category_ID => an Instance of AboutYou::SDK::Model::Category
-
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 221 def categories_by_name( name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY ) result_categories = [] categories.each do |category| result_categories.push(category) if category.name == name && (active_only == AboutYou::SDK::Model::Category::ALL || category.is_active) end result_categories end |
#category(id) ⇒ Object
This method is used for getting a category model by a given id
-
Args :
-
id
-> the id for which a category model should be returned
-
-
Returns :
-
either an instance of AboutYou::SDK::Model::Category or nil if
id
is not found
-
134 135 136 137 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 134 def category(id) return nil unless @categories[String(id)] @categories[String(id)] end |
#category_tree(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method returns the root categories for the read-in categories
-
Args :
-
activeOnly
-> determines whether the result should contain only active categories or not
-
-
Returns :
-
a Hash containing pairs of Category_Id => instance of AboutYou::SDK::Model::Category
-
121 122 123 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 121 def category_tree(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) subcategories(0, active_only) end |
#clear_cache ⇒ Object
This method clears the cache
-
Returns :
-
True/False determining whether the clearing was sucessfull or not
-
77 78 79 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 77 def clear_cache cache.delete(cache_key) end |
#empty? ⇒ Boolean
this method checks whether this category manager has read-in categories or not
-
Returns :
-
a boolean which determines whether this Category Manager has read-in categories or not
-
108 109 110 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 108 def empty? categories.empty? end |
#first_category_by_name(name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the first category which has a certain name
-
Args :
-
name
-> the name which should be found -
activeOnly
-> determines whether the result should contain only active categories or not
-
-
Returns :
-
either an instance of AboutYou::SDK::Model::Category or nil if
name
not found
-
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 197 def first_category_by_name( name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY ) categories.each do |category| return category if category.name == name && (active_only == AboutYou::SDK::Model::Category::ACTIVE_ONLY || category.is_active) end nil end |
#load_cached_categories ⇒ Object
Gets the cached Categories for this app
-
Returns :
-
True/False determining whether the loading was sucessfull or not
-
51 52 53 54 55 56 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 51 def load_cached_categories parse_json( cache.get(cache_key), shop_api.model_factory_instance ) if cache && cache.exists(cache_key) end |
#parse_json(json_object, factory) ⇒ 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::DefaultCategoryManager
-
92 93 94 95 96 97 98 99 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 92 def parse_json(json_object, factory) return if !json_object && categories.empty? cache_categories(json_object) if cache self.parent_child_ids = json_object['parent_child'] json_object['ids'].each do |id, json_category| categories[id] = factory.create_category(json_category) end end |
#subcategories(id, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object
This method gets the subcategories for a given category id
-
Args :
-
id
-> an Array of category Ids -
activeOnly
-> determines whether the result should contain only active categories or not
-
-
Returns :
-
a Hash containing pairs of Category_ID => an Instance of AboutYou::SDK::Model::Category
-
182 183 184 185 |
# File 'lib/AboutYou/Model/CategoryManager/default_category_manager.rb', line 182 def subcategories(id, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) return [] unless parent_child_ids && parent_child_ids.key?(String(id)) categories(parent_child_ids[String(id)], active_only) end |