Class: AboutYou::SDK::Model::BasketItem

Inherits:
Object
  • Object
show all
Includes:
AbstractBasketItem, BasketVariantItem, ResultError
Defined in:
lib/AboutYou/Model/Basket/basket_item.rb

Overview

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.

Constant Summary

Constants included from AbstractBasketItem

AbstractBasketItem::IMAGE_URL_REQUIRED

Instance Attribute Summary collapse

Attributes included from BasketVariantItem

#app_id, #json_object, #product, #variant, #variant_id

Attributes included from AbstractBasketItem

#additional_data, #is_changed

Attributes included from ResultError

#error_code, #error_ident, #error_message

Instance Method Summary collapse

Methods included from BasketVariantItem

#check_app_id, #check_variant_id, #errors?, #old_price, #tax, #total_net, #total_price, #total_vat, #unique_key

Methods included from AbstractBasketItem

#addition_data=, #check_additional_data, #description

Methods included from ResultError

#parse_error_result

Constructor Details

#initialize(id, variant_id, additional_data = nil, app_id = nil) ⇒ BasketItem

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 :

    • an instance of AboutYou::SDK::Model::BasketItem



31
32
33
34
35
36
37
# 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

Instance Attribute Details

#idObject

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



17
18
19
# File 'lib/AboutYou/Model/Basket/basket_item.rb', line 17

def id
  @id
end

Instance Method Details

#check_id(id) ⇒ Object

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



87
88
89
90
91
# 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) ⇒ Object

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

  • Args :

    • json_object -> the api response key

    • products -> array of products

  • Returns :

    • an instance of AboutYou::SDK::Model::BasketItem



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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