Class: Google4R::Checkout::ShoppingCart

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/checkout/shared.rb

Overview

ShoppingCart instances are containers for Item instances. You can add Items to the class using #create_item (see the documentation of this method for an example).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ ShoppingCart

Initialize a new ShoppingCart with an empty Array for the items.



117
118
119
120
# File 'lib/google4r/checkout/shared.rb', line 117

def initialize(owner)
  @owner = owner
  @items = Array.new
end

Instance Attribute Details

#expires_atObject

You can set the <cart-expiration> time with this property. If left unset then the tag will not be generated and the cart will never expire.



75
76
77
# File 'lib/google4r/checkout/shared.rb', line 75

def expires_at
  @expires_at
end

#itemsObject (readonly)

The items in the cart. Do not modify this array directly but use #create_item to add items.



70
71
72
# File 'lib/google4r/checkout/shared.rb', line 70

def items
  @items
end

#ownerObject (readonly)

The owner of this cart. At the moment, this always is the CheckoutCartCommand.



66
67
68
# File 'lib/google4r/checkout/shared.rb', line 66

def owner
  @owner
end

#private_dataObject

You can set almost arbitrary data into the cart using this method.

The data will be converted to XML in the following way: The keys are converted to tag names (whitespace becomes “-”, all chars not matching /[a-zA-Z0-9-_])/ will be removed.

If a value is an array then the key for this value will be used as the tag name for each of the arrays’s entries.

Arrays will be flattened before it is processed.

Example

cart.private_data = { 'foo' => { 'bar' => 'baz' } })

# will produce the following XML

<foo>
  <bar>baz</bar>
</foo>

cart.private_data = { 'foo' => [ { 'bar' => 'baz' }, "d'oh", 2 ] }

# will produce the following XML

<foo>
  <bar>baz</bar>
</foo>
<foo>d&amp;</foo>
<foo>2</foo>


108
109
110
# File 'lib/google4r/checkout/shared.rb', line 108

def private_data
  @private_data
end

Class Method Details

.create_from_element(element, owner) ⇒ Object

Creates a new ShoppingCart object from a REXML::Element object.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/google4r/checkout/shared.rb', line 159

def self.create_from_element(element, owner)
  result = ShoppingCart.new(owner)
  
  text = element.elements['cart-expiration/good-until-date'].text rescue nil
  result.expires_at = Time.parse(text) unless text.nil?
  
  data_element = element.elements['merchant-private-data']
  value = PrivateDataParser.element_to_value(data_element) unless data_element.nil?
  
  result.private_data = value unless value.nil?
  
  element.elements.each('items/item') do |item_element|
    result.items << Item.create_from_element(item_element, result)
  end
  
  return result
end

Instance Method Details

#create_item {|item| ... } ⇒ Object

Use this method to add a new item to the cart. If you use a block with this method then the block will be given the new item. The new item will be returned in any case.

Passing a block is the preferred way of using this method.

Example

# Using a block (preferred).
cart = ShoppingCart.new

cart.create_item do |item|
  item.name = "Dry Food Pack"
  item.description = "A pack of highly nutritious..."
  item.unit_price = Money.new(3500, "USD") # $35.00
  item.quantity = 1
end

# Not using a block.
cart = ShoppingCart.new

item = cart.create_item
item.name = "Dry Food Pack"
item.description = "A pack of highly nutritious..."
item.unit_price = Money.new(3500, "USD") # $35.00
item.quantity = 1

Yields:

  • (item)


148
149
150
151
152
153
154
155
156
# File 'lib/google4r/checkout/shared.rb', line 148

def create_item(&block)
  item = Item.new(self)
  @items << item

  # Pass the newly generated item to the given block to set its attributes.
  yield(item) if block_given?
  
  return item
end