Class: AboutYou::SDK::Model::ProductSearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/AboutYou/Model/product_search_result.rb

Overview

This class represents a product search result model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProductSearchResult

Returns a new instance of ProductSearchResult.



19
20
21
# File 'lib/AboutYou/Model/product_search_result.rb', line 19

def initialize
  self.products = {}
end

Instance Attribute Details

#categoriesObject

Returns the value of attribute categories.



16
17
18
# File 'lib/AboutYou/Model/product_search_result.rb', line 16

def categories
  @categories
end

#facetsObject

Returns the value of attribute facets.



15
16
17
# File 'lib/AboutYou/Model/product_search_result.rb', line 15

def facets
  @facets
end

#page_hashObject

Returns the value of attribute page_hash.



11
12
13
# File 'lib/AboutYou/Model/product_search_result.rb', line 11

def page_hash
  @page_hash
end

#price_rangesObject

Returns the value of attribute price_ranges.



14
15
16
# File 'lib/AboutYou/Model/product_search_result.rb', line 14

def price_ranges
  @price_ranges
end

#product_countObject

Returns the value of attribute product_count.



12
13
14
# File 'lib/AboutYou/Model/product_search_result.rb', line 12

def product_count
  @product_count
end

#productsObject

Returns the value of attribute products.



10
11
12
# File 'lib/AboutYou/Model/product_search_result.rb', line 10

def products
  @products
end

#raw_facetsObject

Returns the value of attribute raw_facets.



17
18
19
# File 'lib/AboutYou/Model/product_search_result.rb', line 17

def raw_facets
  @raw_facets
end

#sale_countsObject

Returns the value of attribute sale_counts.



13
14
15
# File 'lib/AboutYou/Model/product_search_result.rb', line 13

def sale_counts
  @sale_counts
end

Class Method Details

.create_from_json(json_object, factory) ⇒ Object

Returns static.

Parameters:

  • \stdClass

    $json_object

  • ModelFactoryInterface

    $factory

Returns:

  • static



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/AboutYou/Model/product_search_result.rb', line 29

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

Instance Method Details

#category_treeObject

Returns Category[].

Returns:

  • Category[]



103
104
105
106
107
108
109
110
# File 'lib/AboutYou/Model/product_search_result.rb', line 103

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_priceObject

Returns the max price in euro cent, if the price range was not requested/selected

Returns:

  • integer|null



89
90
91
92
93
94
95
96
97
98
# File 'lib/AboutYou/Model/product_search_result.rb', line 89

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_priceObject

Returns the min price in euro cent or null, if the price range was not requested/selected

  • @return integer|null



73
74
75
76
77
78
79
80
81
82
# File 'lib/AboutYou/Model/product_search_result.rb', line 73

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) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/AboutYou/Model/product_search_result.rb', line 54

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