class AboutYou::SDK::Model::BasketSet

BasketSet is a class used for adding a set of variant items into the basket

If you want to add a set of variant items into a basket, you need to create an instance of a BasketSet. The BasketSet contains BasketSetItems.

A set can be useful if you want to sell several variants as a single product. For example, if you offer a pair of shoes and additionally different styles of shoelaces the customer can choose from, you maybe want to put both - shoes and laces - together.

Constants

IMAGE_URL_REQUIRED

determines if an image url is required

Attributes

errors[RW]

array containing errors

id[RW]

The ID of this basket item. You can choose this ID by yourself to identify your item later.

item_app_id[RW]

app id of an item

items[RW]

array containing instances of AboutYou::SDK::Model::BasketSetItem

total_net[RW]

total net worth of the set

total_price[RW]

total price of the set

total_vat[RW]

total vat worth of the set

Public Class Methods

new(id, additional_data = nil) click to toggle source

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

# File lib/AboutYou/Model/Basket/basket_set.rb, line 46
def initialize(id, additional_data = nil)
  check_id(id)
  check_additional_data(additional_data, true)
  self.id = id
  self.additional_data = additional_data

  self
end

Public Instance Methods

add_item(item) click to toggle source

This method is used for adding items to a basket set

# File lib/AboutYou/Model/Basket/basket_set.rb, line 136
def add_item(item)
  if items.count == 0
    self.item_app_id = item.app_id
  elsif item_app_id != item.app_id
    fail '\InvalidArgumentException! you can not set different app ids
      for items in an item-set.'
  end

  items.push(item)
end
check_id(id) click to toggle source

This method is used for determining whether a certain id is valid for the basket set or not

  • Args :

    • id -> the id which should be checked

  • Fails :

    • if the id is not a String and its length is smaller then 2

# File lib/AboutYou/Model/Basket/basket_set.rb, line 189
def check_id(id)
  fail '\InvalidArgumentException! ID of the BasketSetItem must be a
    String that must contain minimum two characters' unless
      id.is_a?(String) && id.length > 1
end
create(item_id, sub_items, additional_data = nil) click to toggle source

This method is used for creating a basket set from an array containing instances of AboutYou::SDK::Model::BasketSetItem

# File lib/AboutYou/Model/Basket/basket_set.rb, line 112
def create(item_id, sub_items, additional_data = nil)
  set = new(item_id, additional_data)

  sub_items.each do |item_data|
    set.add_item(
      AboutYou::SDK::Model::BasketSetItem.new(
        item_data[0],
        item_data[1] ? item_data[1] : nil
      )
    )
  end

  set
end
create_from_json(json_object, factory, products) click to toggle source

This method is used for creating a basket set with a given api json response. It is best practice to use this method.

# File lib/AboutYou/Model/Basket/basket_set.rb, line 67
def create_from_json(json_object, factory, products)
  set = new(
    json_object['id'],
    if json_object.key?('additional_data')
      [json_object['additional_data']]
    else
      nil
    end
    )

  set.parse_error_result(json_object)

  json_object['set_items'].each do |index, json_term|
    item = factory.create_basket_set_item(json_term, products)
    if item.errors?
      set.errors[index] = item
    else
      set.items[index] = item
    end
  end
  if json_object.key?('total_price')
    set.totalPrice = json_object['total_price']
  end
  if json_object.key?('total_net')
    set.totalPrice = json_object['total_net']
  end
  if json_object.key?('total_vat')
    set.totalPrice = json_object['total_vat']
  end

  set
end
errors?() click to toggle source

This method checks if there are errors in the basket set

  • Returns :

    • Boolean determining whether there are errors or not

# File lib/AboutYou/Model/Basket/basket_set.rb, line 153
def errors?
  error_code || errors.count > 0
end
unique_key() click to toggle source

This method is used for creating a unique key for the basket set

  • Returns :

    • a String containing a unique key for the basket set

# File lib/AboutYou/Model/Basket/basket_set.rb, line 163
def unique_key
  key = ':'
  unless additional_data
    additional_data.sort!
    key = key + ':' + additional_data.to_json
  end

  items = []
  self.items.each do |item|
    items.push(item.unique_key)
  end
  key += items.to_json

  key
end