forked from phoet/asin
/
cart.rb
54 lines (41 loc) · 1 KB
/
cart.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'hashie'
module ASIN
# =Item
#
# The +Item+ class is a wrapper for the Amazon XML-REST-Response.
#
# A Hashie::Mash is used for the internal data representation and can be accessed over the +raw+ attribute.
#
class Cart
attr_reader :raw
def initialize(hash)
@raw = Hashie::Mash.new(hash)
end
def cart_id
@raw.CartId
end
def hmac
@raw.HMAC
end
def url
@raw.PurchaseURL
end
def price
@raw.SubTotal.FormattedPrice
end
def items
return [] unless @raw.CartItems
@raw.CartItems.CartItem.is_a?(Array) ? @raw.CartItems.CartItem : [@raw.CartItems.CartItem]
end
def saved_items
return [] unless @raw.SavedForLaterItems
@raw.SavedForLaterItems.SavedForLaterItem.is_a?(Array) ? @raw.SavedForLaterItems.SavedForLaterItem : [@raw.SavedForLaterItems.SavedForLaterItem]
end
def valid?
@raw.Request.IsValid == 'True'
end
def empty?
@raw.CartItems.nil?
end
end
end