Class: Google4R::Checkout::Item

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

Overview

An Item object represents a line of goods in the shopping cart/reciep.

You should never initialize them directly but use ShoppingCart#create_item instead.

Note that you have to create/set the tax tables for the owner of the cart in which the item is before you can set the tax_table attribute.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ Item

Create a new Item in the given Cart. You should not instantize this class directly but use Cart#create_item instead.



238
239
240
# File 'lib/google4r/checkout/shared.rb', line 238

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

The cart that this item belongs to.



182
183
184
# File 'lib/google4r/checkout/shared.rb', line 182

def cart
  @cart
end

#descriptionObject

The description of the cart item (string, required).



188
189
190
# File 'lib/google4r/checkout/shared.rb', line 188

def description
  @description
end

#idObject

Optional string value that is used to store the item’s id (defined by the merchant) in the cart. Serialized to <merchant-item-id> in XML. Displayed by Google Checkout.



208
209
210
# File 'lib/google4r/checkout/shared.rb', line 208

def id
  @id
end

#nameObject

The name of the cart item (string, required).



185
186
187
# File 'lib/google4r/checkout/shared.rb', line 185

def name
  @name
end

#private_dataObject

Optional hash value that is used to store the item’s id (defined by the merchant) in the cart. Serialized to <merchant-private-item-data> in XML. Not displayed by Google Checkout.

Must be a Hash. See ShoppingCart#private_data on how the serialization to XML is done.



216
217
218
# File 'lib/google4r/checkout/shared.rb', line 216

def private_data
  @private_data
end

#quantityObject

Number of units that this item represents (integer, required).



204
205
206
# File 'lib/google4r/checkout/shared.rb', line 204

def quantity
  @quantity
end

#tax_tableObject

The tax table to use for this item. Optional.



225
226
227
# File 'lib/google4r/checkout/shared.rb', line 225

def tax_table
  @tax_table
end

#unit_priceObject

The price for one unit of the given good (Money instance, required).



191
192
193
# File 'lib/google4r/checkout/shared.rb', line 191

def unit_price
  @unit_price
end

Class Method Details

.create_from_element(element, cart) ⇒ Object

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/google4r/checkout/shared.rb', line 243

def self.create_from_element(element, cart)
  result = Item.new(cart)
  
  result.name = element.elements['item-name'].text
  result.description = element.elements['item-description'].text
  result.quantity = element.elements['quantity'].text.to_i
  result.id = element.elements['merchant-item-id'].text rescue nil

  data_element = element.elements['merchant-private-item-data']
  if not data_element.nil? then
    value = PrivateDataParser.element_to_value(data_element)
    result.private_data = value unless value.nil?
  end
  
  table_selector = element.elements['tax-table-selector'].text rescue nil
  if not table_selector.nil? then
    result.tax_table = cart.owner.tax_tables.find {|table| table.name == table_selector }
  end

  unit_price = (element.elements['unit-price'].text.to_f * 100).to_i
  unit_price_currency = element.elements['unit-price/@currency'].value
  result.unit_price = Money.new(unit_price, unit_price_currency)
  
  return result
end