Class: AboutYou::SDK::Model::Product

Inherits:
Object
  • Object
show all
Includes:
AbstractModel
Defined in:
lib/AboutYou/Model/product.rb

Overview

This class represents an product model

Instance Attribute Summary collapse

Attributes included from AbstractModel

#shop_api

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_leaf_categoriesObject

Returns the value of attribute active_leaf_categories.



38
39
40
# File 'lib/AboutYou/Model/product.rb', line 38

def active_leaf_categories
  @active_leaf_categories
end

#active_root_categoriesObject

Returns the value of attribute active_root_categories.



36
37
38
# File 'lib/AboutYou/Model/product.rb', line 36

def active_root_categories
  @active_root_categories
end

#brand_idObject

Returns the value of attribute brand_id.



24
25
26
# File 'lib/AboutYou/Model/product.rb', line 24

def brand_id
  @brand_id
end

#category_id_pathsObject

Returns the value of attribute category_id_paths.



26
27
28
# File 'lib/AboutYou/Model/product.rb', line 26

def category_id_paths
  @category_id_paths
end

#default_imageObject

Returns the value of attribute default_image.



28
29
30
# File 'lib/AboutYou/Model/product.rb', line 28

def default_image
  @default_image
end

#default_variantObject

Returns the value of attribute default_variant.



29
30
31
# File 'lib/AboutYou/Model/product.rb', line 29

def default_variant
  @default_variant
end

#description_longObject

Returns the value of attribute description_long.



18
19
20
# File 'lib/AboutYou/Model/product.rb', line 18

def description_long
  @description_long
end

#description_shortObject

Returns the value of attribute description_short.



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

def description_short
  @description_short
end

#facet_groups(group_id) ⇒ Object

Returns the value of attribute facet_groups.



34
35
36
# File 'lib/AboutYou/Model/product.rb', line 34

def facet_groups
  @facet_groups
end

#facet_idsObject

Returns the value of attribute facet_ids.



27
28
29
# File 'lib/AboutYou/Model/product.rb', line 27

def facet_ids
  @facet_ids
end

#factoryObject

Returns the value of attribute factory.



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

def factory
  @factory
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#inactive_variantsObject

Returns the value of attribute inactive_variants.



32
33
34
# File 'lib/AboutYou/Model/product.rb', line 32

def inactive_variants
  @inactive_variants
end

#is_activeObject

Returns the value of attribute is_active.



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

def is_active
  @is_active
end

#is_saleObject

Returns the value of attribute is_sale.



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

def is_sale
  @is_sale
end

#leaf_categories(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object

Returns the value of attribute leaf_categories.



37
38
39
# File 'lib/AboutYou/Model/product.rb', line 37

def leaf_categories
  @leaf_categories
end

#max_priceObject

Returns the value of attribute max_price.



20
21
22
# File 'lib/AboutYou/Model/product.rb', line 20

def max_price
  @max_price
end

#max_savingsObject

Returns the value of attribute max_savings.



22
23
24
# File 'lib/AboutYou/Model/product.rb', line 22

def max_savings
  @max_savings
end

#max_savings_percentageObject

Returns the value of attribute max_savings_percentage.



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

def max_savings_percentage
  @max_savings_percentage
end

#max_savings_priceObject

Returns the value of attribute max_savings_price.



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

def max_savings_price
  @max_savings_price
end

#merchant_idObject

Returns the value of attribute merchant_id.



25
26
27
# File 'lib/AboutYou/Model/product.rb', line 25

def merchant_id
  @merchant_id
end

#min_priceObject

Returns the value of attribute min_price.



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

def min_price
  @min_price
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#root_categories(active_only = Category::ACTIVE_ONLY) ⇒ Object

Returns the value of attribute root_categories.



35
36
37
# File 'lib/AboutYou/Model/product.rb', line 35

def root_categories
  @root_categories
end

#selected_variantObject

Returns the value of attribute selected_variant.



30
31
32
# File 'lib/AboutYou/Model/product.rb', line 30

def selected_variant
  @selected_variant
end

#stylesObject

Returns the value of attribute styles.



33
34
35
# File 'lib/AboutYou/Model/product.rb', line 33

def styles
  @styles
end

#variantsObject

Returns the value of attribute variants.



31
32
33
# File 'lib/AboutYou/Model/product.rb', line 31

def variants
  @variants
end

Class Method Details

.create_from_json(json_object, factory, app_id) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/AboutYou/Model/product.rb', line 40

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

Instance Method Details

#brandObject



310
311
312
313
314
315
# File 'lib/AboutYou/Model/product.rb', line 310

def brand
  facet_group_set.facet(
    AboutYou::SDK::Model::Facet::FACET_BRAND,
    brand_id
  )
end

#categories(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY) ⇒ Object



221
222
223
# File 'lib/AboutYou/Model/product.rb', line 221

def categories(active_only = AboutYou::SDK::Model::Category::ACTIVE_ONLY)
  root_categories(active_only)
end

#category(active = false) ⇒ Object



182
183
184
185
186
# File 'lib/AboutYou/Model/product.rb', line 182

def category(active = false)
  return nil unless category_id_paths

  leaf_categories(active)[leaf_categories(active).keys[0]]
end

#category_with_longest_active_pathObject



188
189
190
191
192
193
194
195
196
197
# File 'lib/AboutYou/Model/product.rb', line 188

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



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/AboutYou/Model/product.rb', line 282

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_setObject



176
177
178
179
180
# File 'lib/AboutYou/Model/product.rb', line 176

def facet_group_set
  generate_facet_group_set unless @facet_groups

  @facet_groups
end

#generate_facet_group_setObject



168
169
170
171
172
173
174
# File 'lib/AboutYou/Model/product.rb', line 168

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



229
230
231
# File 'lib/AboutYou/Model/product.rb', line 229

def group_facets(group_id)
  facet_group_set.group(group_id) ? group.facets : []
end

#leaf_category_idsObject



212
213
214
215
216
217
218
219
# File 'lib/AboutYou/Model/product.rb', line 212

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



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/AboutYou/Model/product.rb', line 130

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



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/AboutYou/Model/product.rb', line 103

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



116
117
118
119
120
121
122
# File 'lib/AboutYou/Model/product.rb', line 116

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



124
125
126
127
128
# File 'lib/AboutYou/Model/product.rb', line 124

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



162
163
164
165
166
# File 'lib/AboutYou/Model/product.rb', line 162

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/AboutYou/Model/product.rb', line 144

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



93
94
95
96
97
98
99
100
101
# File 'lib/AboutYou/Model/product.rb', line 93

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/AboutYou/Model/product.rb', line 78

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_idsObject



203
204
205
206
207
208
209
210
# File 'lib/AboutYou/Model/product.rb', line 203

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/AboutYou/Model/product.rb', line 245

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



331
332
333
334
335
336
337
338
# File 'lib/AboutYou/Model/product.rb', line 331

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



317
318
319
# File 'lib/AboutYou/Model/product.rb', line 317

def variant_by_id(variant_id)
  variants[variant_id] ? variants[variant_id] : nil
end

#variants_by_ean(ean) ⇒ Object



321
322
323
324
325
326
327
328
329
# File 'lib/AboutYou/Model/product.rb', line 321

def variants_by_ean(ean)
  variants = []

  variants.each do |_key, variant|
    variants.push(variant) if variant.ean == ean
  end

  variants
end

#variants_by_facet_id(facet_id, group_id) ⇒ Object



340
341
342
343
344
345
346
347
348
349
# File 'lib/AboutYou/Model/product.rb', line 340

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