class AboutYou::SDK::Model::CategoryManager::DefaultCategoryManager
This class is responsible for handling all the logic when working with categories
- author
-
Collins GmbH & Co KG
Constants
- DEFAULT_CACHE_DURATION
the duration for the cached values to live
Attributes
the cache client
the cache key for the app
the read-in categories from the category manager
the parent child ids for the categories
instance of AY
Public Class Methods
the Constructor for the AY
class
-
Args :
-
Returns :
# 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
Public Instance Methods
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
-
# 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
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
-
# 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
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 ifid
is not found
-
# File lib/AboutYou/Model/CategoryManager/default_category_manager.rb, line 134 def category(id) return nil unless @categories[String(id)] @categories[String(id)] end
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
-
# 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
This method clears the cache
-
Returns :
-
True/False determining whether the clearing was sucessfull or not
-
# File lib/AboutYou/Model/CategoryManager/default_category_manager.rb, line 77 def clear_cache cache.delete(cache_key) end
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
-
# File lib/AboutYou/Model/CategoryManager/default_category_manager.rb, line 108 def empty? categories.empty? end
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 ifname
not found
-
# 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
Gets the cached Categories for this app
-
Returns :
-
True/False determining whether the loading was sucessfull or not
-
# 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
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 :
# 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
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
-
# 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