class AboutYou::SDK::Model::Product

This class represents an product model

Attributes

active_leaf_categories[RW]

active leaf categories of the product

active_root_categories[RW]

active root categories for the product

brand_id[RW]

product brand id

category_id_paths[RW]

category id paths of this product

default_image[RW]

product default image

default_variant[RW]

product default variant

description_long[RW]

long description

description_short[RW]

short description

facet_groups[RW]

facet groups of product

facet_ids[RW]

facet ids of this product

id[RW]

product id

inactive_variants[RW]

inactive styles of product

is_active[RW]

product active or not

is_sale[RW]

product in sale or not

leaf_categories[RW]

leaf categories for product

max_price[RW]

product max price

max_savings[RW]

product max savings

max_savings_percentage[RW]

product max savings percentage

max_savings_price[RW]

product max savings price

merchant_id[RW]

product merchant id

min_price[RW]

product min price

name[RW]

product name

root_categories[RW]

root categories for product

selected_variant[RW]

product selected variant

styles[RW]

styles of product

variants[RW]

variants of product

Public Class Methods

create_from_json(json_object, factory, app_id) click to toggle source

This method is used for creating an instance of this class by a json_object.

# File lib/AboutYou/Model/product.rb, line 75
def self.create_from_json(json_object, factory, app_id)
  product = new

  # these are required fields
  fail 'MalformedJsonException!' unless json_object['id'] || json_object['name']

  product.factory = factory

  product.id                     = json_object['id']
  product.name                   = json_object['name']

  product.is_sale                = json_object['sale'] ? json_object['sale'] : false
  product.description_short      = json_object['description_short'] ? json_object['description_short'] : ''
  product.description_long       = json_object['description_long'] ? json_object['description_long'] : ''
  product.is_active              = json_object['active'] ? json_object['active'] : true
  product.brand_id               = json_object['brand_id'] ? json_object['brand_id'] : nil
  product.merchant_id            = json_object['merchant_id'] ? json_object['merchant_id'] : nil

  product.min_price              = json_object['min_price'] ? json_object['min_price'] : nil
  product.max_price              = json_object['max_price'] ? json_object['max_price'] : nil
  product.max_savings_price      = json_object['max_savings'] ? json_object['max_savings'] : nil
  product.max_savings_percentage = json_object['max_savings_percentage'] ? json_object['max_savings_percentage'] : nil

  product.default_image          = json_object['default_image'] ? factory.create_image(json_object['default_image']) : nil
  product.default_variant        = json_object['default_variant'] ? factory.create_variant(json_object['default_variant'], self) : nil

  product.variants               = product.parse_variants(json_object, factory, product)
  product.inactive_variants      = product.parse_variants(json_object, factory, product, 'inactive_variants')
  product.styles                 = product.parse_styles(json_object, factory)

  key                            = 'categories.' + String(app_id)
  product.category_id_paths      = json_object[key] ? json_object[key] : []

  product.facet_ids              = product.parse_facet_ids(json_object)

  product
end

Public Instance Methods

brand() click to toggle source

Getter for the brand

# File lib/AboutYou/Model/product.rb, line 529
def brand
  facet_group_set.facet(
    AboutYou::SDK::Model::Facet::FACET_BRAND,
    brand_id
  )
end
categories(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) click to toggle source

Getter for the categories

  • Args :

    • active_only -> Boolean controlling whether only active categories should be returned or not

  • Returns :

# File lib/AboutYou/Model/product.rb, line 389
def categories(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY)
  root_categories(active_only)
end
category(active = false) click to toggle source

This method is used for getting a category

# File lib/AboutYou/Model/product.rb, line 314
def category(active = false)
  return unless category_id_paths

  leaf_categories(active)[leaf_categories(active).keys[0]]
end
category_with_longest_active_path() click to toggle source

Getter for the deepest category

# File lib/AboutYou/Model/product.rb, line 326
def category_with_longest_active_path
  return nil unless category_id_paths

  category_id_paths.sort! { |x, y| y.count <=> x.count }.each do |path|
    return factory.category_manager.category(path[-1]) if
    factory.category_manager.category(path[-1]).path_active?
  end

  nil
end
excluded_facet_groups(selected_facet_group_set) click to toggle source

Getter for the excluded groups

# File lib/AboutYou/Model/product.rb, line 495
def excluded_facet_groups(selected_facet_group_set)
  all_groups = {}

  variants.each do |variant|
    next unless facet_group_set.contains(selected_facet_group_set)

    facet_group_set = variant.facet_group_set
    facet_group_set.group_ids.each do |group_id|
      next if selected_facet_group_set.group_ids.include?(group_id)
      group = facet_group_set.group(group_id)
      fail 'RuntimeException! group for id ' + String(group_id) +
        ' not found' if group.nil?
      facets = group.facets
      next unless facets

      unless all_groups[group_id]
        all_groups[group_id] = AboutYou::SDK::Model::FacetGroup.new(
          group.id,
          group.name
        )
        all_groups[group_id].add_facets(facets)
      end
    end
  end

  all_groups
end
facet_group_set() click to toggle source

Getter for the facet group set

# File lib/AboutYou/Model/product.rb, line 299
def facet_group_set
  generate_facet_group_set unless @facet_groups

  @facet_groups
end
generate_facet_group_set() click to toggle source

This method generates a facet group set for self.facet_ids

  • Fails :

    • if self.facet_ids is empty

# File lib/AboutYou/Model/product.rb, line 285
def generate_facet_group_set
  fail 'RuntimeException! To use this method, you must add the field
    ProductFields::ATTRIBUTES_MERGED to the "product search" or
    "products by ids"' if facet_ids.empty?

  self.facet_groups = AboutYou::SDK::Model::FacetGroupSet.new(facet_ids)
end
group_facets(group_id) click to toggle source

Getter for facets for a certain group id

# File lib/AboutYou/Model/product.rb, line 415
def group_facets(group_id)
  facet_group_set.group(group_id) ? group.facets : []
end
leaf_category_ids() click to toggle source

Getter for the leaf category ids

  • Returns :

    • Array containing category_ids

# File lib/AboutYou/Model/product.rb, line 371
def leaf_category_ids
  leaf_categories = []
  category_id_paths.each do |category_id_path|
    leaf_categories.push(category_id_path[-1])
  end

  leaf_categories.uniq
end
parse_attributes_json(attributes_json_object) click to toggle source

This method is used for parsing an attribues json object

# File lib/AboutYou/Model/product.rb, line 223
def parse_attributes_json(attributes_json_object)
  ids = {}

  attributes_json_object.each do |group, facet_ids|
    gid = group[11..group.length] # rm prefix 'attributes'

    # TODO: Remove Workaround for Ticket ???
    facet_ids = Array(facet_ids)
    ids[gid] = facet_ids
  end

  ids
end
parse_category_id_paths(json_object) click to toggle source

This method is used for parsing the category id paths for this product

  • Args :

    • json_object -> the json_object received from the api

  • Returns :

    • Array of Strings

# File lib/AboutYou/Model/product.rb, line 169
def parse_category_id_paths(json_object)
  paths = []

  json_object.each do |name, category_paths|
    if name.index('categories') == 0
      paths = category_paths
      break
    end
  end

  paths
end
parse_facet_ids(json_object) click to toggle source

This method is used for parsing the facet ids of this product

# File lib/AboutYou/Model/product.rb, line 191
def parse_facet_ids(json_object)
  ids = parse_facet_ids_in_attributes_merged(json_object)
  ids = parse_facet_ids_in_variants(json_object) if ids.nil?
  ids = parse_facet_ids_in_brand(json_object) if ids.nil?

  !ids.nil? ? ids : {}
end
parse_facet_ids_in_attributes_merged(json_object) click to toggle source

This method is used for parsing the facet ids in merged attributes

# File lib/AboutYou/Model/product.rb, line 208
def parse_facet_ids_in_attributes_merged(json_object)
  return nil unless json_object['attributes_merged']

  parse_attributes_json(json_object['attributes_merged'])
end
parse_facet_ids_in_brand(json_object) click to toggle source

This method is used for parsing the facet ids brands

# File lib/AboutYou/Model/product.rb, line 273
def parse_facet_ids_in_brand(json_object)
  return nil unless json_object['brand_id']

  { '0' => [json_object['brand_id']] }
end
parse_facet_ids_in_variants(json_object) click to toggle source

This method is used for parsing the facet ids in the variants of this product

  • Args :

    • json_object -> the json_object received from the api

  • Returns :

    • Array containing facet ids

# File lib/AboutYou/Model/product.rb, line 246
def parse_facet_ids_in_variants(json_object)
  if json_object['variants']
    ids = []
    json_object['variants'].each do |variant|
      ids.push(parse_attributes_json(variant['attributes']))
    end
    ids = AboutYou::SDK::Model::FacetGroupSet.merge_facet_ids(ids)
    return ids
  elsif json_object['default_variant']
    ids = parse_attributes_json(
      json_object['default_variant']['attributes']
    )
    return ids
  end

  nil
end
parse_styles(json_object, factory) click to toggle source

This method is used for parsing the styles of the product

# File lib/AboutYou/Model/product.rb, line 150
def parse_styles(json_object, factory)
  styles = []

  json_object['styles'].each do |style|
    styles.push(factory.create_product(style))
  end if json_object.key?('styles') && !json_object['styles'].empty?

  styles
end
parse_variants(json_object, factory, product, attribute_name = 'variants') click to toggle source

This method is used for parsing the variants of the product

# File lib/AboutYou/Model/product.rb, line 125
def parse_variants(json_object, factory, product, attribute_name = 'variants')
  variants = {}

  json_object[attribute_name].each do |json_variant|
    variants[json_variant['id']] = factory.create_variant(
      json_variant,
      product
    )
  end if
  json_object.key?(attribute_name) &&
  !json_object[attribute_name].empty?

  variants
end
root_category_ids() click to toggle source

Getter for the root category ids

  • Returns :

    • Array containing category_ids

# File lib/AboutYou/Model/product.rb, line 356
def root_category_ids
  root_category = []
  category_id_paths.each do |category_id_path|
    root_category.push(category_id_path[0])
  end

  root_category.uniq
end
selectable_facet_groups(selected_facet_group_set) click to toggle source

Getter for the selectable facet groups

# File lib/AboutYou/Model/product.rb, line 449
def selectable_facet_groups(selected_facet_group_set)
  all_groups = {}
  selected_group_ids = selected_facet_group_set.group_ids

  variants.each do |variant|
    next if facet_group_set.contains(selected_facet_group_set)

    facet_group_set = variant.facet_group_set
    ids = facet_group_set.group_ids

    ids.each do |group_id|
      next if selected_group_ids.include?(group_id)

      group = facet_group_set.group(group_id)
      fail 'RuntimeException! group for id ' + group_id +
        ' not found' if group.nil?

      all_groups[group_id][group.unique_key] = group
    end
  end

  selected_group_ids.each do |group_id|
    ids = selected_facet_group_set.ids
    ids[group_id] = nil
    my_facet_group_set = AboutYou::SDK::Model::FacetGroupSet.new(ids)
    variants.each do |variant|
      facet_group_set = variant.facet_group_set
      if facet_group_set.contains(my_facet_group_set)
        group = facet_group_set.group(group_id)
        all_groups[group_id][group.unique_key] = group
      end
    end
  end

  all_groups
end
variant_by_facet(facet_group_set) click to toggle source

This method searches for a variant by a certain facet group set

# File lib/AboutYou/Model/product.rb, line 576
def variant_by_facet(facet_group_set)
  key = facet_group_set.unique_key
  variants.each do |variant|
    return variant if variant.facet_group_set.unique_key == key
  end

  nil
end
variant_by_id(variant_id) click to toggle source

This method returns a variant by a certain variant_id

# File lib/AboutYou/Model/product.rb, line 545
def variant_by_id(variant_id)
  variants[variant_id] ? variants[variant_id] : nil
end
variants_by_ean(ean) click to toggle source

This method searches for variants by a certain ean

# File lib/AboutYou/Model/product.rb, line 558
def variants_by_ean(ean)
  variants = []
  self.variants.each do |_key, variant|
    variants.push(variant) if variant.ean == ean
  end

  variants
end
variants_by_facet_id(facet_id, group_id) click to toggle source

This method searches for variants by a certain facet_id and group_id

  • Args :

    • facet_id -> facet_id used for searching

    • group_id -> group_id used for searching

  • Returns :

# File lib/AboutYou/Model/product.rb, line 595
def variants_by_facet_id(facet_id, group_id)
  variants = []

  facet = AboutYou::SDK::Facet.new(facet_id, '', '', group_id, '')
  self.variants.each do |variant|
    variants.push(variant) if variant.facet_group_set.contains(facet)
  end

  variants
end