Class: AboutYou::SDK::Model::Variant

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

Overview

This class represents a variant model

Instance Attribute Summary collapse

Attributes included from AbstractModel

#shop_api

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#about_numberObject

Integer about number id



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

def about_number
  @about_number
end

#additional_infoObject

additional information



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

def additional_info
  @additional_info
end

#colorObject

String color



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

def color
  @color
end

#created_dateObject

Integer creation date



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

def created_date
  @created_date
end

#eanObject

String ean



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

def ean
  @ean
end

#facet_groupsObject

Array of AboutYou::SDK::Model::FacetGroup



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

def facet_groups
  @facet_groups
end

#facet_idsObject

Array of Integer facet_ids



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

def facet_ids
  @facet_ids
end

#factoryObject

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



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

def factory
  @factory
end

#first_active_dateObject

Integer first active data



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

def first_active_date
  @first_active_date
end

#first_sale_dateObject

integer first Sale data



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

def first_sale_date
  @first_sale_date
end

#idObject

Integer id



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

def id
  @id
end

#imagesObject

Array of AboutYou::SDK::Model::Image



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

def images
  @images
end

#is_defaultObject

Boolean is variant default for product or not



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

def is_default
  @is_default
end

#lengthObject

Integer length



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

def length
  @length
end

#old_priceObject

Integer old price



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

def old_price
  @old_price
end

#priceObject

Integer price



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

def price
  @price
end

#productObject

instance of AboutYou::SDK::Model::Product



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

def product
  @product
end

#quantityObject

integer quantity



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

def quantity
  @quantity
end

#retail_priceObject

Integer retail price



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

def retail_price
  @retail_price
end

#season_codeObject

string season_code



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

def season_code
  @season_code
end

#selected_imageObject

instance of AboutYou::SDK::Model::Image



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

def selected_image
  @selected_image
end

#updated_dateObject

Integer update Date



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

def updated_date
  @updated_date
end

Class Method Details

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

    • product -> Product of this variant

  • Fails :

    • if json_object has no id

  • Returns :

    • Instance of AboutYou::SDK::Model::Variant



68
69
70
71
72
73
74
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
# File 'lib/AboutYou/Model/variant.rb', line 68

def self.create_from_json(json_object, factory, product)
  fail 'MalformedJsonException' unless json_object['id']

  variant                   = new
  variant.factory           = factory
  variant.product           = product

  variant.images            = variant.parse_images(json_object['images'])
  # free memory
  json_object['images']     = nil

  variant.id                = json_object['id']
  variant.about_number      = (json_object['about_number']) ? json_object['about_number'] : nil
  variant.ean               = (json_object['ean']) ? json_object['ean'] : nil
  variant.is_default        = (json_object['default']) ? json_object['default'] : nil
  variant.price             = (json_object['price']) ? json_object['price'] : nil
  variant.old_price         = (json_object['old_price']) ? json_object['old_price'] : nil
  variant.retail_price      = (json_object['retail_price']) ? json_object['retail_price'] : nil
  variant.additional_info   = (json_object['additional_info']) ? json_object['additional_info'] : nil
  variant.quantity          = (json_object['quantity']) ? json_object['quantity'] : 0
  variant.first_active_date = (json_object['first_active_date']) ? json_object['first_active_date'] : nil
  variant.first_sale_date   = (json_object['first_sale_date']) ? json_object['first_sale_date'] : nil
  variant.created_date      = (json_object['created_date']) ? json_object['created_date'] : nil
  variant.updated_date      = (json_object['updated_date']) ? json_object['updated_date'] : nil

  variant.facet_ids         = variant.parse_facet_ids(json_object)
  variant.facet_groups      = variant.generate_facet_group_set(json_object)
  variant.color             = variant.facet_group(AboutYou::SDK::Model::Facet::FACET_COLOR)
  variant.length            = variant.facet_group(AboutYou::SDK::Model::Facet::FACET_LENGTH)
  variant.season_code       = variant.facet_group(AboutYou::SDK::Model::Facet::FACET_SEASON_CODE)

  variant
end

Instance Method Details

#facet_group(group_id) ⇒ Object

This method searches by a certain facet group id

  • Args :

    • group_id -> The group_id used for searching

  • Returns :

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



212
213
214
# File 'lib/AboutYou/Model/variant.rb', line 212

def facet_group(group_id)
  facet_groups.group(group_id)
end

#generate_facet_group_set(json_object) ⇒ Object

This method generated a facet group set for a certain json_object

  • Args :

    • json_object -> The json_object received from the api



198
199
200
201
# File 'lib/AboutYou/Model/variant.rb', line 198

def generate_facet_group_set(json_object)
  ids = parse_facet_ids(json_object)
  self.facet_groups = AboutYou::SDK::Model::FacetGroupSet.new(ids)
end

#imageObject

Getter for selected / default image

  • Returns :

    • nil / AboutYou::SDK::Model::Image



162
163
164
165
166
167
168
169
170
# File 'lib/AboutYou/Model/variant.rb', line 162

def image
  if selected_image
    return selected_image
  elsif images[0]
    return images[0]
  end

  nil
end

#image_by_hash(hash) ⇒ Object

This method searches for an image by a certain hash

  • Args :

    • hash -> the hash used for searching

  • Returns :

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



129
130
131
132
133
134
135
# File 'lib/AboutYou/Model/variant.rb', line 129

def image_by_hash(hash)
  images.each do |image|
    return image if image.hash == hash
  end

  nil
end

#parse_facet_ids(json_object) ⇒ Object

This method parses the facet ids of a certain json_object

  • Args :

    • json_object -> The json_object received from the api

  • Returns :

    • {} / Hash containing pairs of group_id => attribute_ids



181
182
183
184
185
186
187
188
189
190
# File 'lib/AboutYou/Model/variant.rb', line 181

def parse_facet_ids(json_object)
  ids = {}

  json_object['attributes'].each do |group, attribute_ids|
    group_id = group[11..group.length] # rm prefix 'attributs_'
    ids[Integer(group_id)] = attribute_ids
  end unless json_object['attributes'].empty?

  ids
end

#parse_images(image_json) ⇒ Object

This method is used for parsing images

  • Args :

    • image_json -> the image json_object received from the api

  • Returns :

    • / Array of instances of AboutYou::SDK::Model::Image



111
112
113
114
115
116
117
118
# File 'lib/AboutYou/Model/variant.rb', line 111

def parse_images(image_json)
  image_array = []
  image_json.each do |image|
    image_array.push(factory.create_image(image))
  end if image_json

  image_array
end

#quantity_per_packObject

Getter for the quantity per pack

  • Returns :

    • Integer quantity



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/AboutYou/Model/variant.rb', line 261

def quantity_per_pack
  facet_group = facet_group(
    AboutYou::SDK::Model::Facet::FACET_QUANTITY_PER_PACK
  )
  return 1 unless facet_group

  facets = facet_group.facets
  return 1 unless facets

  facets[0].value
end

#select_image(hash = nil) ⇒ Object

You can select an image with this method by a certain hash

  • Args :

    • hash -> the hash used for selecting [optional]

  • Returns :

    • Boolean determining whether the selection was successful or not



146
147
148
149
150
151
152
153
154
# File 'lib/AboutYou/Model/variant.rb', line 146

def select_image(hash = nil)
  if hash
    self.selected_image = image_by_hash(hash)
    true
  else
    self.selected_image = nil
    false
  end
end

#sizeObject

Getter for the size facet group

  • Returns :

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



222
223
224
225
226
# File 'lib/AboutYou/Model/variant.rb', line 222

def size
  # @todo: Instance level caching
  group_id = size_group_id
  facet_group(group_id) unless group_id.empty?
end

#size_group_idObject

This method is used for getting the size group id

  • Returns :

    • nil / group_id



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/AboutYou/Model/variant.rb', line 234

def size_group_id
  keys = {}
  groups = facet_groups.groups
  size_run = facet_group(AboutYou::SDK::Model::Facet::FACET_SIZE_RUN)

  groups.each do |group|
    keys[group.name] = group.group_id
  end if groups.is_a?(Array)

  size_run.facets.each do |facet|
    group_name = facet.value
    return keys[group_name] if keys.key?(group_name)
  end unless size_run.empty?

  return keys['size'] if keys.key?('size')

  return keys['size_run'] if keys.key?('size_run')

  nil
end