Class: Tesco::Groceries::Basket
- Inherits:
-
Hash
- Object
- Hash
- Tesco::Groceries::Basket
- Defined in:
- lib/tesco.rb
Overview
Represents a shopping basket.
Instance Attribute Summary collapse
-
#basket_id ⇒ Object
readonly
Returns the value of attribute basket_id.
-
#clubcard_points ⇒ Object
readonly
These are calculated on the server, so we need to sync before returning them.
-
#customer_id ⇒ Object
readonly
Returns the value of attribute customer_id.
-
#multi_buy_savings ⇒ Object
readonly
These are calculated on the server, so we need to sync before returning them.
-
#price ⇒ Object
readonly
These are calculated on the server, so we need to sync before returning them.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
Class Method Summary collapse
-
.flush ⇒ Object
This class keeps track of all the baskets for each user that’s been logged in, to save on API calls.
-
.new(api) ⇒ Object
You can initialize your own basket with Basket.new(tesco_api_instance), but I’d recommend using #Tesco#basket.
Instance Method Summary collapse
-
#<(products) ⇒ Object
(also: #add)
Adds the given product(s) to the basket.
-
#>(products) ⇒ Object
(also: #remove)
Removes the given product(s) completely from the basket.
-
#authtest ⇒ Object
tests to make sure you are authenticated as this basket’s owner.
-
#clean ⇒ Object
Empties the basket completely — this may take a while for large baskets.
-
#note(product, note) ⇒ Object
Change the note for the shopper on a product in your basket.
-
#sync ⇒ Object
Makes sure this object reflects the basket on Tesco online.
Instance Attribute Details
#basket_id ⇒ Object (readonly)
Returns the value of attribute basket_id.
241 242 243 |
# File 'lib/tesco.rb', line 241 def basket_id @basket_id end |
#clubcard_points ⇒ Object (readonly)
These are calculated on the server, so we need to sync before returning them
242 243 244 |
# File 'lib/tesco.rb', line 242 def clubcard_points @clubcard_points end |
#customer_id ⇒ Object (readonly)
Returns the value of attribute customer_id.
241 242 243 |
# File 'lib/tesco.rb', line 241 def customer_id @customer_id end |
#multi_buy_savings ⇒ Object (readonly)
These are calculated on the server, so we need to sync before returning them
242 243 244 |
# File 'lib/tesco.rb', line 242 def multi_buy_savings @multi_buy_savings end |
#price ⇒ Object (readonly)
These are calculated on the server, so we need to sync before returning them
242 243 244 |
# File 'lib/tesco.rb', line 242 def price @price end |
#quantity ⇒ Object (readonly)
Returns the value of attribute quantity.
241 242 243 |
# File 'lib/tesco.rb', line 241 def quantity @quantity end |
Class Method Details
.flush ⇒ Object
This class keeps track of all the baskets for each user that’s been logged in, to save on API calls. If you need to remove this information from memory this method will destroy the class variable that holds it, without affecting anything on the Tesco servers.
265 266 267 |
# File 'lib/tesco.rb', line 265 def self.flush @@basket.clean end |
.new(api) ⇒ Object
You can initialize your own basket with Basket.new(tesco_api_instance), but I’d recommend using #Tesco#basket.
Because this object will sync with the Tesco server there can only ever be one instance. It will keep track of different users’ baskets. (Wipe this memory with #Basket#flush)
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/tesco.rb', line 249 def self.new(api) raise ArgumentError, "The argument needs to be a Tesco instance" if not api.is_a? Tesco::Groceries begin return @@basket[api.customer_id] rescue (@@basket ||= {})[api.customer_id] = self.allocate @@basket[api.customer_id].instance_variable_set(:@api,api) @@basket[api.customer_id].instance_variable_set(:@customer_id,api.customer_id) @@basket[api.customer_id].sync @@basket[api.customer_id] end end |
Instance Method Details
#<(products) ⇒ Object Also known as: add
Adds the given product(s) to the basket. It increments that item’s quantity if it’s already present in the basket. Leave a note for the shopper against these items with note.
295 296 297 298 299 300 301 |
# File 'lib/tesco.rb', line 295 def <(products) authtest [products].flatten.each do |product| raise ArgumentError, "That is not a Tesco Product object" if !product.is_a?(Tesco::Groceries::Product) (self[product] ||= BasketItem.new(self,{'ProductId' => product.product_id})).add(1) end end |
#>(products) ⇒ Object Also known as: remove
Removes the given product(s) completely from the basket.
305 306 307 308 309 310 311 |
# File 'lib/tesco.rb', line 305 def >(products) authtest [products].flatten.each do |product| raise ArgumentError, "That is not a Tesco Product object" if !product.is_a?(Tesco::Groceries::Product) delete(product).remove # Removes the product from the basket, then deletes it from the API end end |
#authtest ⇒ Object
tests to make sure you are authenticated as this basket’s owner
323 324 325 |
# File 'lib/tesco.rb', line 323 def authtest raise NotAuthenticatedError, "Please reauthenticate as this basket's owner before attempting to modify it" if @customer_id != @api.customer_id end |
#clean ⇒ Object
Empties the basket completely — this may take a while for large baskets
315 316 317 318 319 320 |
# File 'lib/tesco.rb', line 315 def clean authtest self.each_pair do |product,basket_item| delete(product).remove # Removes the product from the basket, then deletes it from the API end end |
#note(product, note) ⇒ Object
Change the note for the shopper on a product in your basket
287 288 289 290 291 292 |
# File 'lib/tesco.rb', line 287 def note(product,note) # TODO: should work with multiple products authtest raise IndexError, "That item is not in the basket" if not self[product] @api.api_request(:changebasket,:productid => product.product_id,:changequantity => 0,:noteforshopper => note) self[product].instance_variable_set(:@note,note) end |
#sync ⇒ Object
Makes sure this object reflects the basket on Tesco online.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/tesco.rb', line 270 def sync authtest res = @api.api_request(:listbasket) @basket_id = res['BasketId'].to_i @price = res['BasketGuidePrice'].to_f @multi_buy_savings = res['BasketGuideMultiBuySavings'].to_f @clubcard_points = res['BasketTotalClubcardPoints'].to_i @quantity = res['BasketQuantity'] # TODO: Is this just length? res['BasketLines'].each do |more| self[Product.new(@api,more['ProductId'],more)] = BasketItem.new(self,more) end return true end |