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

cache[RW]

the cache client

cache_key[RW]

the cache key for the app

categories[RW]

the read-in categories from the category manager

parent_child_ids[RW]

the parent child ids for the categories

shop_api[RW]

instance of AY

Public Class Methods

new(cache = nil, app_id = '', shop_api) click to toggle source

the Constructor for the AY class

# 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

cache_categories(json_object) click to toggle source

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
categories_by_name( name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY ) click to toggle source

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 :

# 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) click to toggle source

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 :

# 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) click to toggle source

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 :

# 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() click to toggle source

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
empty?() click to toggle source

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
first_category_by_name( name, active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY ) click to toggle source

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 :

# 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() click to toggle source

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
parse_json(json_object, factory) click to toggle source

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

# 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) click to toggle source

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 :

# 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