class AboutYou::SDK::Model::ProductSearchResult

This class represents a product search result model

Attributes

categories[RW]

Array containing instances of AboutYou::SDK::Model::Category

facets[RW]

Array containing instances of AboutYou::SDK::Model::Facet

page_hash[RW]

String page hash

price_ranges[RW]

Array containing instances of AboutYou::SDK::Model::PriceRange

product_count[RW]

Integer count of products

products[RW]

Array of instances of AboutYou::SDK::Model::Product

raw_facets[RW]

raw facets

sale_counts[RW]

Integer count of products in sale

Public Class Methods

create_from_json(json_object, factory) click to toggle source

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

# File lib/AboutYou/Model/product_search_result.rb, line 47
def self.create_from_json(json_object, factory)
  product_search_result = new

  product_search_result.page_hash     = json_object['page_hash']
  product_search_result.product_count = json_object['product_count']
  product_search_result.raw_facets    = json_object['facets']

  json_object['products'].each do |json_product|
    product = factory.create_product(json_product)
    product_search_result.products[product.id] = product
  end

  product_search_result.parse_facets(json_object['facets'], factory)
  # free memory
  json_object['categories'] = nil
  json_object['prices'] = nil
  json_object['sale'] = nil

  product_search_result.facets = factory.create_facets_counts(
    json_object
  )

  product_search_result
end
new() click to toggle source

Constructor for the AboutYou::SDK::Model::ProductSearchResult class

# File lib/AboutYou/Model/product_search_result.rb, line 31
def initialize
  self.products = {}

  self
end

Public Instance Methods

category_tree() click to toggle source

Getter for the category tree

# File lib/AboutYou/Model/product_search_result.rb, line 133
def category_tree
  top_level_categories = []
  categories.each do |category|
    top_level_categories.push(category) if category.parent.nil?
  end

  top_level_categories
end
max_price() click to toggle source

Getter for the max price

  • Returns :

    • nil / Integer

# File lib/AboutYou/Model/product_search_result.rb, line 116
def max_price
  return nil if price_ranges.empty?

  price_ranges.reverse_each do |price_range|
    next if price_range.product_count == 0
    return price_range.max
  end

  price_ranges[-1].max
end
min_price() click to toggle source

Getter for the min price

  • Returns :

    • nil / Integer

# File lib/AboutYou/Model/product_search_result.rb, line 99
def min_price
  return nil if price_ranges.empty?

  price_ranges.each do |price_range|
    next if price_range.product_count == 0
    return price_range.min
  end

  price_ranges[0].min
end
parse_facets(json_object, factory) click to toggle source

This method is used for parsing the facets of the search result

# File lib/AboutYou/Model/product_search_result.rb, line 79
def parse_facets(json_object, factory)
  self.categories = factory.create_categories_facets(
    json_object['categories']
  ) if json_object['categories']

  self.price_ranges = factory.create_price_ranges(
    json_object['prices']
  ) if json_object['prices']

  self.sale_counts = factory.create_sale_facet(
    json_object['sale']
  ) if json_object['sale']
end