class AboutYou::SDK::Model::Basket

This Class represents a basket model

Attributes

deleted_items[RW]

Array containing the deleted items in the basket

errors[RW]

errors of the basket

items[RW]

array of items in the basket

products[RW]

Array of products

total_amount[RW]

Integer containing the total amount of the basket

total_net[RW]

Integer containing the total net woth of the basket

total_price[RW]

Integer containing the total price of the basket

total_vat[RW]

Integer containing the total vat woth of the basket

unique_variant_count[RW]

Integer count of unique variants

updated_items[RW]

Array containing the updated items in the basket

Public Class Methods

create_from_json(json_object, factory) click to toggle source

This method is used for creating an instance of this class by a json_object.

# File lib/AboutYou/Model/basket.rb, line 39
def self.create_from_json(json_object, factory)
  basket = new
  basket.errors = {}
  basket.deleted_items = {}
  basket.updated_items = {}
  basket.total_price  = json_object['total_price']
  basket.total_net    = json_object['total_net']
  basket.total_vat    = json_object['total_vat']
  basket.parse_items(json_object, factory)
  basket.total_amount = items.count

  basket
end

Public Instance Methods

check_additional_data(add_data = nil, _img_url_required = false) click to toggle source

checks if certain additional_data is valid to set for a basket or not

  • Args :

    • add_data -> the desired data to check

    • _img_url_required -> unused operator

  • Fails :

    • if add_data doesnt have a description

    • if add_data doesnt have valid internal infos

# File lib/AboutYou/Model/basket.rb, line 297
def check_additional_data(add_data = nil, _img_url_required = false)
  fail 'InvalidArgumentException! description is required
    in additional data' if add_data && !add_data.key?('description')
  fail 'InvalidArgumentException! internal_infos must be an array' if
  add_data.key?('internal_infos') &&
  !add_data['internal_infos'].is_a?(Array)
end
collected_items() click to toggle source

This methods creates a Hash containing pairs of unique_item_key => instance of AboutYou::SDK::Model::BasketItem It only contains 1 entry per unique_item_key and will increase the amount if more items are given

# File lib/AboutYou/Model/basket.rb, line 86
def collected_items
  items = self.items
  items_merged = {}
  items.each do |item|
    key = item.unique_key
    if items_merged.key?(key)
      amount = items_merged[key]['amount'] + 1
      items_merged[key] = {
        'item' => item,
        'price' => item.total_price * amount,
        'amount' => amount
      }
    else
      items_merged[key] = {
        'item' => item,
        'price' => item.total_price,
        'amount' => 1
      }
    end
  end

  items_merged
end
delete_all_items() click to toggle source

This method is used for deleting all items from the basket

# File lib/AboutYou/Model/basket.rb, line 210
def delete_all_items
  items = self.items

  return self unless items.empty?
  ids = []
  items.each do |item|
    ids.push = item.id
  end
  delete_items(ids)

  self
end
delete_item(item_id) click to toggle source

This method is used for deleting an item from the basket

# File lib/AboutYou/Model/basket.rb, line 181
def delete_item(item_id)
  deleted_items[item_id] = item_id

  self
end
delete_items(item_ids) click to toggle source

This method is used for deleting items from the basket

# File lib/AboutYou/Model/basket.rb, line 196
def delete_items(item_ids)
  item_ids.each do |item_id|
    deleted_items[item_id] = item_id
  end

  self
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.rb, line 59
def errors?
  errors.count > 0
end
item(item_id) click to toggle source

This method gets an item from the basket for a given item id

# File lib/AboutYou/Model/basket.rb, line 72
def item(item_id)
  return items[item_id] if items.key?(item_id)
end
order_lines_array() click to toggle source

This method builds the order lines for the update query

# File lib/AboutYou/Model/basket.rb, line 117
def order_lines_array
  order_lines = []

  deleted_items.uniq.each do |itemId|
    order_lines.push('delete' => itemId)
  end

  updatedItems.each do |item|
    order_lines.push(item)
  end

  order_lines
end
parse_items(json_object, factory) click to toggle source

This method is used for parsing the items in the basket

# File lib/AboutYou/Model/basket.rb, line 141
def parse_items(json_object, factory)
  products = {}

  json_object['products'].each do |key, json_product|
    products[key] = factory.create_product(json_product)
  end if json_object['products']

  self.products = products
  vids = []

  json_object['order_lines'].each do |key, json_item|
    if json_item['set_items']
      item = factory.create_basket_set(json_item, products)
    else
      vids.push(json_item['variantId'])
      item = factory.create_basket_item(json_item, products)
    end

    if item.errors?
      errors[key] = item
    else
      items[item.id] = item
    end
  end if json_object['order_lines']

  vids = vids.uniq
  self.uniqueVariantCount = vids.count

  self
end
update_item(basket_item) click to toggle source

This method is used for updating an item in the basket

# File lib/AboutYou/Model/basket.rb, line 232
def update_item(basket_item)
  item = {
    'id' => basket_item.id,
    'variant_id' => basket_item.variant_id,
    'app_id' => basket_item.app_id
  }
  add_data = basket_item.additional_data
  if add_data
    check_additional_data(add_data)
    item['additional_data'] = add_data
  end
  updated_items[basket_item.id] = item

  self
end
update_item_set(basket_set) click to toggle source

This method is used for updating an item set in the basket

# File lib/AboutYou/Model/basket.rb, line 257
def update_item_set(basket_set)
  items = basket_set.items

  fail 'InvalidArgumentException! BasketSet
    needs at least one item' if items.empty?

  item_set = []
  items.each do |sub_item|
    item = {
      'variant_id' => sub_item.variant_id,
      'app_id' => sub_item.app_id
    }
    add_data = sub_item.additional_data
    if add_data
      check_additional_data(add_data)
      item['additional_data'] = add_data
    end
    item_set.push = item
  end

  updated_items[basket_set.id] = {
    'id' => basket_set.id,
    'additional_data' => basket_set.additional_data,
    'set_items' => item_set
  }

  self
end