class AboutYou::SDK::Model::BasketItem

BasketItem is a class used for adding a variant item into the basket

If you want to add a variant into a basket, you need to create an instance of a BasketItem. The BasketItem represents a variant by it’s variant_id. It can contain $additional_data that will be transmitted to the merchant untouched.

Attributes

id[RW]

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

Public Class Methods

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

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

  • Args :

    • id -> the basket item id

    • variant_id -> the id of the variant

    • additional_data -> additional data of the basket item [optional]

    • app_id -> the app id [optional]

  • Returns :

# File lib/AboutYou/Model/Basket/basket_item.rb, line 31
def initialize(id, variant_id, additional_data = nil, app_id = nil)
  check_id(id)
  self.id = id
  super(variant_id, additional_data, app_id)

  self
end

Public Instance Methods

check_id(id) click to toggle source

This method checks whether an basket item id is valid or not

  • Args :

    • id -> the id which should be checked

  • Fails :

    • if the id is not a String and has less then 2 characters

# File lib/AboutYou/Model/Basket/basket_item.rb, line 87
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 < 2
end
create_from_json(json_object, products) click to toggle source

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

# File lib/AboutYou/Model/Basket/basket_item.rb, line 50
def create_from_json(json_object, products)
  item = new(
      json_object['id'],
      json_object['variant_id'],
      if json_object.key?('additional_data')
        [json_object['additional_data']]
      else
        nil
      end,
      json_object.key?('app_id') ? json_object['app_id'] : nil
  )

  item.parse_error_result(json_object)
  item.json_object = json_object

  unless json_object['product_id'].empty?
    if products.key?(json_object['product_id'])
      item.product = products[json_object['product_id']]
    else
      fail 'UnexpectedResultException! Product with ID ' +
        String(json_object['product_id']) +
        ' expected but wasnt received with the basket'
    end
  end

  item
end