Class: Amazon::AWS::ShoppingCart::Cart

Inherits:
Amazon::AWS::Search::Request show all
Includes:
Enumerable
Defined in:
lib/ruby-paa/aws/shoppingcart.rb

Constant Summary

Constants inherited from Amazon::AWS::Search::Request

Amazon::AWS::Search::Request::DIGEST, Amazon::AWS::Search::Request::DIGEST_SUPPORT

Instance Attribute Summary collapse

Attributes inherited from Amazon::AWS::Search::Request

#cache, #config, #conn, #encoding, #locale, #query, #user_agent

Instance Method Summary collapse

Methods inherited from Amazon::AWS::Search::Request

#reconnect, #search, #sign

Constructor Details

#initialize(key_id = nil, associate = nil, locale = nil, user_agent = USER_AGENT) ⇒ Cart

Create a new instance of a remote shopping-cart. See Amazon::AWS::Search::Request.new for details of the parameters.

Example:

cart = Cart.new


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 45

def initialize(key_id=nil, associate=nil, locale=nil,
	       user_agent=USER_AGENT)

  @cart_items = []
  @saved_for_later_items = []

  # Note the *false* as the fourth parameter to _super_, because we
  # never want to cache shopping-cart transactions.
  #
         super( key_id, associate, locale, false, user_agent )
end

Instance Attribute Details

#cart_idObject (readonly)

cart_id is an alphanumeric token that uniquely identifies a remote shopping-cart. hmac is a Hash Message Authentication Code. This is an encrypted alphanumeric token used to authenticate requests. purchase_url is the URL to follow in order to complete the purchase of the items in the shopping-cart. cart_items is an Array of items in the active area of the cart and saved_for_later_items is an Array of items in the Save For Later area of the cart.



32
33
34
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 32

def cart_id
  @cart_id
end

#cart_itemsObject (readonly) Also known as: items

cart_id is an alphanumeric token that uniquely identifies a remote shopping-cart. hmac is a Hash Message Authentication Code. This is an encrypted alphanumeric token used to authenticate requests. purchase_url is the URL to follow in order to complete the purchase of the items in the shopping-cart. cart_items is an Array of items in the active area of the cart and saved_for_later_items is an Array of items in the Save For Later area of the cart.



32
33
34
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 32

def cart_items
  @cart_items
end

#hmacObject (readonly)

cart_id is an alphanumeric token that uniquely identifies a remote shopping-cart. hmac is a Hash Message Authentication Code. This is an encrypted alphanumeric token used to authenticate requests. purchase_url is the URL to follow in order to complete the purchase of the items in the shopping-cart. cart_items is an Array of items in the active area of the cart and saved_for_later_items is an Array of items in the Save For Later area of the cart.



32
33
34
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 32

def hmac
  @hmac
end

#purchase_urlObject (readonly)

cart_id is an alphanumeric token that uniquely identifies a remote shopping-cart. hmac is a Hash Message Authentication Code. This is an encrypted alphanumeric token used to authenticate requests. purchase_url is the URL to follow in order to complete the purchase of the items in the shopping-cart. cart_items is an Array of items in the active area of the cart and saved_for_later_items is an Array of items in the Save For Later area of the cart.



32
33
34
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 32

def purchase_url
  @purchase_url
end

#saved_for_later_itemsObject (readonly) Also known as: saved_items, saved

cart_id is an alphanumeric token that uniquely identifies a remote shopping-cart. hmac is a Hash Message Authentication Code. This is an encrypted alphanumeric token used to authenticate requests. purchase_url is the URL to follow in order to complete the purchase of the items in the shopping-cart. cart_items is an Array of items in the active area of the cart and saved_for_later_items is an Array of items in the Save For Later area of the cart.



32
33
34
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 32

def saved_for_later_items
  @saved_for_later_items
end

Instance Method Details

#active?(item_id_type, item_id) ⇒ Boolean

Returns whether or not an item is present in the active area of the cart.

item_id_type is the name of the attribute that uniquely identifies an item, such as ASIN or CartItemId. item_id is the value of the item_id_type for the item whose presence in the cart is being determined.

If the item is present in the cart, its CartItemId is returned as a String. Otherwise, false is returned.

Example:

cart.active?( :ASIN, 'B00151HZA6' )

Returns:

  • (Boolean)


206
207
208
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 206

def active?(item_id_type, item_id)
  in_area?( @cart_items, item_id_type, item_id )
end

#cart_add(id_type, item_id, quantity = 1, *more_items) ⇒ Object Also known as: add

Add one or more new items to the remote shopping-cart. This can not be used to update quantities of items already in the cart. For that, you must use Cart#cart_modify instead.

id_type is a String, either ASIN or OfferListingId. item_id is the actual ASIN or offer listing ID in question, and quantity is the quantity of the item to add to the cart.

more_items is an optional list of Hash objects describing additional items to add to the cart.

Example:

cart.cart_add( :ASIN, 'B0014C2BL4', 3,

{ ‘B00006BCKL’ => 2 }, { ‘B000VVE2UW’ => 1 } )

or:

cart.cart_add( :ASIN, 'B0014C2BL4', 3,

{ ‘B00006BCKL’ => 2, ‘B000VVE2UW’ => 1 } )



128
129
130
131
132
133
134
135
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 128

def cart_add(id_type, item_id, quantity=1, *more_items)
  ca = CartAdd.new( id_type, item_id, quantity, *more_items )
  ca.response_group = @rg
  ca.params.merge!( { 'CartId' => @cart_id, 'HMAC' => @hmac } )

  cart = search( ca ).cart_add_response.cart
  @cart_items = cart.cart_items.cart_item
end

#cart_clearObject Also known as: clear

Remove all items from the shopping-cart.

Example:

cart.cart_clear


374
375
376
377
378
379
380
381
382
383
384
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 374

def cart_clear
  cc = CartClear.new
  cc.response_group = @rg
  cc.params.merge!( { 'CartId' => @cart_id, 'HMAC' => @hmac } )

  cart = search( cc ).cart_clear_response.cart

  @cart_items = []
  @saved_for_later_items = []
	
end

#cart_create(id_type, item_id, quantity = 1, merge_cart = false, *more_items) ⇒ Object Also known as: create

Prepare the remote shopping-cart for use and place one or more items in it.

id_type is a String, either ASIN or OfferListingId. item_id is the actual ASIN or offer listing ID in question, quantity is the quantity of the item to add to the cart, and merge_cart is whether or not the remote shopping-cart should be merged with the local cart on the Amazon retail site upon check-out.

more_items is an optional list of Hash objects describing additional items to place in the cart.

Example:

cart.cart_create( :ASIN, 'B00151HZA6', 1,

{ ‘B000WC4AH0’ => 2 }, { ‘B000PY32OM’ => 3 } )

or:

cart.cart_create( :ASIN, 'B00151HZA6', 1,

{ ‘B000WC4AH0’ => 2, ‘B000PY32OM’ => 3 } )

Please note that it’s not yet possible to update a wishlist at purchase time by referring to the item’s ListItemId when adding that item to the cart.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 86

def cart_create(id_type, item_id, quantity=1, merge_cart=false,
	        *more_items)
  cc = CartCreate.new( id_type, item_id, quantity, merge_cart, nil,
		       *more_items )

  @rg = ResponseGroup.new( :Cart )
  cc.response_group = @rg

         cart = search( cc ).cart_create_response.cart

  @cart_id = cart.cart_id
  @hmac = cart.hmac
  @purchase_url = cart.purchase_url
  @cart_items = cart.cart_items.cart_item
end

#cart_get(cart_id, hmac) ⇒ Object Also known as: get

Retrieve a remote shopping-cart. This is especially useful when needing to resurrect a cart at a later time, when the Cart object containing the original data no longer exists.

cart_id is the unique ID of the cart to be retrieved and hmac is the cart’s hash message authentication code. These details can be obtained from an existing cart using the @cart_id and @hmac instance variables.

Example:

old_cart = Cart.new
old_cart.get_cart( '203-4219703-7532717',

‘o98sn9Z16JOEF/9eo6OcD8zOZA4=’ )



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 338

def cart_get(cart_id, hmac)
  cg = CartGet.new
  @rg = ResponseGroup.new( :Cart )
  cg.response_group = @rg
  cg.params.merge!( { 'CartId' => cart_id, 'HMAC' => hmac } )

  cart = search( cg ).cart_get_response.cart

  @cart_id = cart.cart_id
  @hmac = cart.hmac
  @purchase_url = cart.purchase_url

  if ci = cart.cart_items
    @cart_items = ci.cart_item
  else
    @cart_items = []
  end

  if sfl = cart.saved_for_later_items
    @saved_for_later_items = sfl.saved_for_later_item
  else
    @saved_for_later_items = []
  end

  self
end

#cart_modify(item_id_type, item_id, quantity, save_for_later = nil, *more_items) ⇒ Object Also known as: modify

Modify the quantities of one or more products already in the cart. Changing the quantity of an item to 0 effectively removes it from the cart.

item_id_type is the name of the attribute that uniquely identifies an item in the cart, such as ASIN or CartItemId. item_id is the value of the item_id_type of the item to be modified, and quantity is its new quantity.

save_for_later should be set to true if the items in question should be moved to the Save For Later area of the shopping-cart, or false if they should be moved to the active area. save_for_later therefore applies to every item specified by item_id and more_items. Use nil when the location of the items should not be changed.

Current Amazon AWS documentation claims that specifying partial quantities can be used to move some copies of an item from one area of the cart to another, whilst leaving the rest in place. In practice, however, this causes an AWS error that explains that a quantity may not be specified in combination with an instruction to move copies from one area of the cart to another. For this reason, when save_for_later is not nil, item quantities are currently ignored.

more_items is an optional list of Hash objects describing additional items whose quantity should be modified.

Example:

cart.cart_modify( :ASIN, 'B00151HZA6', 2, false,

{ ‘B0013F2M52’ => 1 }, { ‘B000HCPSR6’ => 3 } )

or:

cart.cart_modify( :ASIN, 'B00151HZA6', 2, true,

{ ‘B0013F2M52’ => 1, ‘B000HCPSR6’ => 3 } )



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 273

def cart_modify(item_id_type, item_id, quantity, save_for_later=nil,
		*more_items)
  item_quantity1 = quantity

  unless cart_item_id1 = self.include?( item_id_type, item_id )
    raise CartError,
      "Can't find item with '#{item_id_type}' of '#{item_id}' in cart."
  end

  more_items.collect! do |extra_item|
    items = []

    extra_item.each do |item|
      item_id, quantity = item
      unless cart_item_id = self.include?( item_id_type, item_id )
        raise CartError,
          "Can't find item with '#{item_id_type}' of '#{item_id}' in cart."
      end

      items << { cart_item_id => quantity }
    end

    items
  end

  more_items.flatten!

  cm = CartModify.new( cart_item_id1, item_quantity1, save_for_later,
		       *more_items )
  cm.response_group = @rg
  cm.params.merge!( { 'CartId' => @cart_id, 'HMAC' => @hmac } )

  cart = search( cm ).cart_modify_response.cart

  if ci = cart.cart_items
    @cart_items = ci.cart_item
  else
    @cart_items = []
  end

  if sfl = cart.saved_for_later_items
    @saved_for_later_items = sfl.saved_for_later_item
  else
    @saved_for_later_items = []
  end
end

#eachObject Also known as: each_item

Iterator for each item in the cart.



393
394
395
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 393

def each
  @cart_items.each { |item| yield item }
end

#include?(item_id_type, item_id) ⇒ Boolean Also known as: contain?

Returns whether or not an item is present in the cart, be it in the active or Save For Later area.

item_id_type is the name of the attribute that uniquely identifies an item, such as ASIN or CartItemId. item_id is the value of the item_id_type for the item whose presence in the cart is being determined.

If the item is present in the cart, its CartItemId is returned as a String. Otherwise, false is returned.

Example:

cart.include?( :ASIN, 'B00151HZA6' )

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 155

def include?(item_id_type, item_id)
  active?( item_id_type, item_id ) ||
  saved_for_later?( item_id_type, item_id )
end

#saved_for_later?(item_id_type, item_id) ⇒ Boolean Also known as: saved?

Returns whether or not an item is present in the Save For Later area of the cart.

item_id_type is the name of the attribute that uniquely identifies an item, such as ASIN or CartItemId. item_id is the value of the item_id_type for the item whose presence in the cart is being determined.

If the item is present in the cart, its CartItemId is returned as a String. Otherwise, false is returned.

Example:

cart.saved_for_later?( :ASIN, 'B00151HZA6' )

Returns:

  • (Boolean)


226
227
228
# File 'lib/ruby-paa/aws/shoppingcart.rb', line 226

def saved_for_later?(item_id_type, item_id)
  in_area?( @saved_for_later_items, item_id_type, item_id )
end