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

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

  • Returns :

    • an instance of AboutYou::SDK::Model::ProductSearchResult



31
32
33
34
35
# File 'lib/AboutYou/Model/product_search_result.rb', line 31

def initialize
  self.products = {}

  self
end

Instance Attribute Details

#categoriesObject

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



21
22
23
# File 'lib/AboutYou/Model/product_search_result.rb', line 21

def categories
  @categories
end

#facetsObject

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



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

def facets
  @facets
end

#page_hashObject

String page hash



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

def page_hash
  @page_hash
end

#price_rangesObject

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



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

def price_ranges
  @price_ranges
end

#product_countObject

Integer count of products



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

def product_count
  @product_count
end

#productsObject

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



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

def products
  @products
end

#raw_facetsObject

raw facets



23
24
25
# File 'lib/AboutYou/Model/product_search_result.rb', line 23

def raw_facets
  @raw_facets
end

#sale_countsObject

Integer count of products in sale



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

def sale_counts
  @sale_counts
end

Class Method Details

.create_from_json(json_object, factory) ⇒ Object

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

  • Args :

    • json_object -> the json_object received from the api

    • factory -> instance of AboutYou::SDK::Factory::DefaultModelFactory

  • Returns :

    • Instance of AboutYou::SDK::Model::ProductSearchResult



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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

Instance Method Details

#category_treeObject

Getter for the category tree

  • Returns :

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



133
134
135
136
137
138
139
140
# 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_priceObject

Getter for the max price

  • Returns :

    • nil / Integer



116
117
118
119
120
121
122
123
124
125
# 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_priceObject

Getter for the min price

  • Returns :

    • nil / Integer



99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

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

  • Args :

    • json_object -> the json_object received from the api

    • factory -> instance of AboutYou::SDK::Factory::DefaultModelFactory



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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