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

active leaf categories of the product



62
63
64
# File 'lib/AboutYou/Model/product.rb', line 62

def active_leaf_categories
  @active_leaf_categories
end

#active_root_categoriesObject

active root categories for the product



58
59
60
# File 'lib/AboutYou/Model/product.rb', line 58

def active_root_categories
  @active_root_categories
end

#brand_idObject

product brand id



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

def brand_id
  @brand_id
end

#category_id_pathsObject

category id paths of this product



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

def category_id_paths
  @category_id_paths
end

#default_imageObject

product default image



42
43
44
# File 'lib/AboutYou/Model/product.rb', line 42

def default_image
  @default_image
end

#default_variantObject

product default variant



44
45
46
# File 'lib/AboutYou/Model/product.rb', line 44

def default_variant
  @default_variant
end

#description_longObject

long description



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

def description_long
  @description_long
end

#description_shortObject

short description



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

def description_short
  @description_short
end

#facet_groups(group_id) ⇒ Object

Getter for all facetGroups for a certain group id

  • Args :

    • group_id -> group_id used for searching

  • Returns :

    • Hash containing pairs of unique_group_key => group_id



54
55
56
# File 'lib/AboutYou/Model/product.rb', line 54

def facet_groups
  @facet_groups
end

#facet_idsObject

facet ids of this product



40
41
42
# File 'lib/AboutYou/Model/product.rb', line 40

def facet_ids
  @facet_ids
end

#factoryObject

instance of AboutYou::SDK::Factory::DefaultModelFactory



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

def factory
  @factory
end

#idObject

product id



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

def id
  @id
end

#inactive_variantsObject

inactive styles of product



50
51
52
# File 'lib/AboutYou/Model/product.rb', line 50

def inactive_variants
  @inactive_variants
end

#is_activeObject

product active or not



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

def is_active
  @is_active
end

#is_saleObject

product in sale or not



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

def is_sale
  @is_sale
end

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

Getter for the leaf categories

  • Args :

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

  • Returns :

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



60
61
62
# File 'lib/AboutYou/Model/product.rb', line 60

def leaf_categories
  @leaf_categories
end

#max_priceObject

product max price



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

def max_price
  @max_price
end

#max_savingsObject

product max savings



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

def max_savings
  @max_savings
end

#max_savings_percentageObject

product max savings percentage



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

def max_savings_percentage
  @max_savings_percentage
end

#max_savings_priceObject

product max savings price



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

def max_savings_price
  @max_savings_price
end

#merchant_idObject

product merchant id



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

def merchant_id
  @merchant_id
end

#min_priceObject

product min price



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

def min_price
  @min_price
end

#nameObject

product name



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

def name
  @name
end

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

Getter for the root categories

  • Args :

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

  • Returns :

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



56
57
58
# File 'lib/AboutYou/Model/product.rb', line 56

def root_categories
  @root_categories
end

#selected_variantObject

product selected variant



46
47
48
# File 'lib/AboutYou/Model/product.rb', line 46

def selected_variant
  @selected_variant
end

#stylesObject

styles of product



52
53
54
# File 'lib/AboutYou/Model/product.rb', line 52

def styles
  @styles
end

#variantsObject

variants of product



48
49
50
# File 'lib/AboutYou/Model/product.rb', line 48

def variants
  @variants
end

Class Method Details

.create_from_json(json_object, factory, app_id) ⇒ 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

    • app_id -> app id of the product

  • Returns :

    • Instance of AboutYou::SDK::Model::Product



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

Instance Method Details

#brandObject

Getter for the brand

  • Returns :

    • Instance of AboutYou::SDK::Model::Facet



529
530
531
532
533
534
# 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) ⇒ Object

Getter for the categories

  • Args :

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

  • Returns :

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



389
390
391
# 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) ⇒ Object

This method is used for getting a category

  • Args :

    • active -> Boolean determining whether the category has to be active or not

  • Returns :

    • nil / instance of AboutYou::SDK::Model::Category



314
315
316
317
318
# 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_pathObject

Getter for the deepest category

  • Returns :

    • nil / instance of AboutYou::SDK::Model::Category



326
327
328
329
330
331
332
333
334
335
# 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) ⇒ Object

Getter for the excluded groups

  • Args :

    • selected_facet_group_set -> selected instance of AboutYou::SDK::Model::FacetGroupSet

  • Returns :

    • Hash containing pairs of unique_group_key => instance of AboutYou::SDK::Model::FacetGroup



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# 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_setObject

Getter for the facet group set

  • Returns :

    • instance of AboutYou::SDK::Model::FacetGroupSet



299
300
301
302
303
# 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_setObject

This method generates a facet group set for self.facet_ids

  • Fails :

    • if self.facet_ids is empty



285
286
287
288
289
290
291
# 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) ⇒ Object

Getter for facets for a certain group id

  • Args :

    • group_id -> group_id used for searching

  • Returns :

    • / instance of AboutYou::SDK::Model::FacetGroup



415
416
417
# File 'lib/AboutYou/Model/product.rb', line 415

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

#leaf_category_idsObject

Getter for the leaf category ids

  • Returns :

    • Array containing category_ids



371
372
373
374
375
376
377
378
# 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) ⇒ Object

This method is used for parsing an attribues json object

  • Args :

    • attributes_json_object -> the attributes_json_object received from the api

  • Returns :

    • nil / a Hash containing pairs of facet_ids => AboutYou::SDK::Model::Facet



223
224
225
226
227
228
229
230
231
232
233
234
235
# 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) ⇒ Object

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



169
170
171
172
173
174
175
176
177
178
179
180
# 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) ⇒ Object

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

  • Args :

    • json_object -> the json_object received from the api

  • Returns :

    • a Hash containing pairs of facet_ids => AboutYou::SDK::Model::Facet



191
192
193
194
195
196
197
# 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) ⇒ Object

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

  • Args :

    • json_object -> the json_object received from the api

  • Returns :

    • nil / a Hash containing pairs of facet_ids => AboutYou::SDK::Model::Facet



208
209
210
211
212
# 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) ⇒ Object

This method is used for parsing the facet ids brands

  • Args :

    • json_object -> the json_object received from the api

  • Returns :

    • nil / a Hash containing pairs of facet_ids => AboutYou::SDK::Model::Facet



273
274
275
276
277
# 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) ⇒ Object

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



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# 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) ⇒ Object

This method is used for parsing the styles of the product

  • Args :

    • json_object -> the json_object received from the api

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

  • Returns :

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



150
151
152
153
154
155
156
157
158
# 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') ⇒ Object

This method is used for parsing the variants of the product

  • Args :

    • json_object -> the json_object received from the api

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

    • product -> self

    • attribute_name -> attribute name for parsing [optional]

  • Returns :

    • Hash containing pairs of variant_id => instance of AboutYou::SDK::Model::Variant



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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_idsObject

Getter for the root category ids

  • Returns :

    • Array containing category_ids



356
357
358
359
360
361
362
363
# 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) ⇒ Object

Getter for the selectable facet groups

  • Args :

    • selected_facet_group_set -> selected instance of AboutYou::SDK::Model::FacetGroupSet

  • Returns :

    • Hash containing pairs of unique_group_key => instance of AboutYou::SDK::Model::FacetGroup



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# 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) ⇒ Object

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

  • Args :

    • facet_group_set -> facet_group_set used for searching

  • Returns :

    • nil / instance of AboutYou::SDK::Model::Variant



577
578
579
580
581
582
583
584
# File 'lib/AboutYou/Model/product.rb', line 577

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

This method returns a variant by a certain variant_id

  • Args :

    • variant_id -> variant id used for searching

  • Returns :

    • nil / instance of AboutYou::SDK::Model::Variant



545
546
547
# 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) ⇒ Object

This method searches for variants by a certain ean

  • Args :

    • ean -> ean used for searching

  • Returns :

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



558
559
560
561
562
563
564
565
566
# File 'lib/AboutYou/Model/product.rb', line 558

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

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 :

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



596
597
598
599
600
601
602
603
604
605
# File 'lib/AboutYou/Model/product.rb', line 596

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