Class: AboutYou::SDK::Model::Basket
- Inherits:
-
Object
- Object
- AboutYou::SDK::Model::Basket
- Defined in:
- lib/AboutYou/Model/basket.rb
Overview
This Class represents a basket model
Instance Attribute Summary collapse
-
#deleted_items ⇒ Object
Array containing the deleted items in the basket.
-
#errors ⇒ Object
errors of the basket.
-
#items ⇒ Object
array of items in the basket.
-
#products ⇒ Object
Array of products.
-
#total_amount ⇒ Object
Integer containing the total amount of the basket.
-
#total_net ⇒ Object
Integer containing the total net woth of the basket.
-
#total_price ⇒ Object
Integer containing the total price of the basket.
-
#total_vat ⇒ Object
Integer containing the total vat woth of the basket.
-
#unique_variant_count ⇒ Object
Integer count of unique variants.
-
#updated_items ⇒ Object
Array containing the updated items in the basket.
Class Method Summary collapse
-
.create_from_json(json_object, factory) ⇒ Object
This method is used for creating an instance of this class by a json_object.
Instance Method Summary collapse
-
#check_additional_data(add_data = nil, _img_url_required = false) ⇒ Object
checks if certain additional_data is valid to set for a basket or not.
-
#collected_items ⇒ Object
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.
-
#delete_all_items ⇒ Object
This method is used for deleting all items from the basket.
-
#delete_item(item_id) ⇒ Object
This method is used for deleting an item from the basket.
-
#delete_items(item_ids) ⇒ Object
This method is used for deleting items from the basket.
-
#errors? ⇒ Boolean
This method checks if there are errors in the basket set.
-
#item(item_id) ⇒ Object
This method gets an item from the basket for a given item id.
-
#order_lines_array ⇒ Object
This method builds the order lines for the update query.
-
#parse_items(json_object, factory) ⇒ Object
This method is used for parsing the items in the basket.
-
#update_item(basket_item) ⇒ Object
This method is used for updating an item in the basket.
-
#update_item_set(basket_set) ⇒ Object
This method is used for updating an item set in the basket.
Instance Attribute Details
#deleted_items ⇒ Object
Array containing the deleted items in the basket
25 26 27 |
# File 'lib/AboutYou/Model/basket.rb', line 25 def deleted_items @deleted_items end |
#errors ⇒ Object
errors of the basket
11 12 13 |
# File 'lib/AboutYou/Model/basket.rb', line 11 def errors @errors end |
#items ⇒ Object
array of items in the basket
9 10 11 |
# File 'lib/AboutYou/Model/basket.rb', line 9 def items @items end |
#products ⇒ Object
Array of products
15 16 17 |
# File 'lib/AboutYou/Model/basket.rb', line 15 def products @products end |
#total_amount ⇒ Object
Integer containing the total amount of the basket
23 24 25 |
# File 'lib/AboutYou/Model/basket.rb', line 23 def total_amount @total_amount end |
#total_net ⇒ Object
Integer containing the total net woth of the basket
19 20 21 |
# File 'lib/AboutYou/Model/basket.rb', line 19 def total_net @total_net end |
#total_price ⇒ Object
Integer containing the total price of the basket
17 18 19 |
# File 'lib/AboutYou/Model/basket.rb', line 17 def total_price @total_price end |
#total_vat ⇒ Object
Integer containing the total vat woth of the basket
21 22 23 |
# File 'lib/AboutYou/Model/basket.rb', line 21 def total_vat @total_vat end |
#unique_variant_count ⇒ Object
Integer count of unique variants
13 14 15 |
# File 'lib/AboutYou/Model/basket.rb', line 13 def unique_variant_count @unique_variant_count end |
#updated_items ⇒ Object
Array containing the updated items in the basket
27 28 29 |
# File 'lib/AboutYou/Model/basket.rb', line 27 def updated_items @updated_items end |
Class Method Details
.create_from_json(json_object, factory) ⇒ 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
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# 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 |
Instance Method Details
#check_additional_data(add_data = nil, _img_url_required = false) ⇒ Object
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
-
297 298 299 300 301 302 303 |
# 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 ⇒ Object
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
-
Returns :
-
Hash containing pairs of unique_item_key => instance of AboutYou::SDK::Model::BasketItem
-
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 ⇒ Object
This method is used for deleting all items from the basket
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
210 211 212 213 214 215 216 217 218 219 220 221 |
# 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) ⇒ Object
This method is used for deleting an item from the basket
-
Args :
-
item_id
-> the item_id for which the item should be deleted
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
181 182 183 184 185 |
# 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) ⇒ Object
This method is used for deleting items from the basket
-
Args :
-
item_ids
-> Array containing item ids for which items should be deleted
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
196 197 198 199 200 201 202 |
# 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? ⇒ Boolean
This method checks if there are errors in the basket set
-
Returns :
-
Boolean determining whether there are errors or not
-
59 60 61 |
# File 'lib/AboutYou/Model/basket.rb', line 59 def errors? errors.count > 0 end |
#item(item_id) ⇒ Object
This method gets an item from the basket for a given item id
-
Args :
-
item_id
-> the id used for searching in the basket
-
-
Returns :
-
instance of AboutYou::SDK::Model::BasketItem or null if not found
-
72 73 74 |
# 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 ⇒ Object
This method builds the order lines for the update query
-
Returns :
-
Array containing either Hashes with pairs of ‘delete’ => item_id or instances of AboutYou::SDK::Model::BasketItem
-
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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) ⇒ Object
This method is used for parsing the items in the basket
-
Args :
-
json_object
-> the json object received from the api -
factory
-> Instance of AboutYou::SDK::Factory::DefaultModelFactory
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# 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) ⇒ Object
This method is used for updating an item in the basket
-
Args :
-
basket_item
-> instance of AboutYou::SDK::Model::BasketItem
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# 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) ⇒ Object
This method is used for updating an item set in the basket
-
Args :
-
basket_item
-> instance of AboutYou::SDK::Model::BasketSet
-
-
Returns :
-
Instance of AboutYou::SDK::Model::Basket
-
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# 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 |