Class: Amazon::Associates::Cart

Inherits:
ApiResult show all
Defined in:
lib/amazon-associates/types/cart.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(items, args = nil) ⇒ Object

CartCreate defaults:

response_group: Cart
merge_cart: false
list_item_id: nil


23
24
25
26
27
28
29
30
31
# File 'lib/amazon-associates/types/cart.rb', line 23

def self.create(items, args = nil)
  if items.is_a?(Array)
    items = items.inject({}) do |all, (item, count)|
      all[item] = count || 1
      all
    end
  end
  Amazon::Associates.cart_create(items, args).cart
end

.get(args) ⇒ Object

CartGet



34
35
36
# File 'lib/amazon-associates/types/cart.rb', line 34

def self.get(args)
  Amazon::Associates.cart_get(args).cart
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
# File 'lib/amazon-associates/types/cart.rb', line 77

def ==(other)
  id == other.id
end

#add(item, count = 1) ⇒ Object Also known as: <<

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/amazon-associates/types/cart.rb', line 53

def add(item, count = 1)
  raise ArgumentError, "item is nil" if item.nil?
  raise ArgumentError, "count isn't positive" if count <= 0

  if @items.include? item
    action = :cart_modify
    item = @items.find {|i| i == item } # we need the CartItemId for CartModify
    count += item.quantity
  else
    action = :cart_add
  end
  # TODO: This could be much more sophisticated, collapsing operations and such
  @changes << [action, self, {:items => {item => count}}]
end

#changed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/amazon-associates/types/cart.rb', line 49

def changed?
  !@changes.empty?
end

#clearObject



69
70
71
# File 'lib/amazon-associates/types/cart.rb', line 69

def clear
  @changes << [:cart_clear, self]
end

#quantityObject



73
74
75
# File 'lib/amazon-associates/types/cart.rb', line 73

def quantity
  items.sum(&:quantity)
end

#saveObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/amazon-associates/types/cart.rb', line 38

def save
  @changes.each do |action, *args|
    cart = Amazon::Associates.send(action, *args).cart
    @items = cart.items
    @id = cart.id
    @hmac = cart.hmac
  end
  @changes.clear
  self
end

#to_hashObject



13
14
15
16
# File 'lib/amazon-associates/types/cart.rb', line 13

def to_hash
  {:id => id,
   :hmac => hmac}
end