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/receipt.

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.

Direct Known Subclasses

Subscription::RecurrentItem

Defined Under Namespace

Classes: DigitalContent, Subscription

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shopping_cart) ⇒ Item

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



293
294
295
# File 'lib/google4r/checkout/shared.rb', line 293

def initialize(shopping_cart)
  @shopping_cart = shopping_cart
end

Instance Attribute Details

#descriptionObject

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



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

def description
  @description
end

#digital_contentObject (readonly)

DigitalContent information for this item. Optional.



252
253
254
# File 'lib/google4r/checkout/shared.rb', line 252

def digital_content
  @digital_content
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.



223
224
225
# File 'lib/google4r/checkout/shared.rb', line 223

def id
  @id
end

#nameObject

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



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

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.



231
232
233
# File 'lib/google4r/checkout/shared.rb', line 231

def private_data
  @private_data
end

#quantityObject

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



219
220
221
# File 'lib/google4r/checkout/shared.rb', line 219

def quantity
  @quantity
end

#shopping_cartObject (readonly)

The cart that this item belongs to.



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

def shopping_cart
  @shopping_cart
end

#subscriptionObject (readonly)

Subscription information for this item. Optional.



272
273
274
# File 'lib/google4r/checkout/shared.rb', line 272

def subscription
  @subscription
end

#tax_tableObject

The tax table to use for this item. Optional.



240
241
242
# File 'lib/google4r/checkout/shared.rb', line 240

def tax_table
  @tax_table
end

#unit_priceObject

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



195
196
197
# File 'lib/google4r/checkout/shared.rb', line 195

def unit_price
  @unit_price
end

#weightObject

The weigth of the cart item (Weight, required when carrier calculated shipping is used)



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

def weight
  @weight
end

Class Method Details

.create_from_element(element, shopping_cart) ⇒ Object

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



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/google4r/checkout/shared.rb', line 298

def self.create_from_element(element, shopping_cart)
  result = Item.new(shopping_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
  
  weight_element = element.elements['item-weight']
  if not weight_element.nil?
    result.weight = Weight.create_from_element(weight_element)
  end

  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 = shopping_cart.owner.tax_tables.find {|table| table.name == table_selector }
  end

  unit_price = (BigDecimal.new(element.elements['unit-price'].text) * 100).to_i
  unit_price_currency = element.elements['unit-price'].attributes['currency']
  result.unit_price = Money.new(unit_price, unit_price_currency)
  
  digital_content_element = element.elements['digital-content']
  if not digital_content_element.nil?
    result.create_digital_content(DigitalContent.create_from_element(digital_content_element))
  end
  
  subscription_element = element.elements['subscription']
  if not subscription_element.nil?
    result.create_subscription(Subscription.create_from_element(subscription_element, result))
  end
  
  return result
end

Instance Method Details

#create_digital_content(digital_content = nil, &block) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/google4r/checkout/shared.rb', line 254

def create_digital_content(digital_content=nil, &block)
  
  if @digital_content.nil?
    if digital_content.nil?
      @digital_content = DigitalContent.new
    else
      @digital_content = digital_content
    end
  end
  
  if block_given?
    yield @digital_content
  end
  
  return @digital_content
end

#create_subscription(subscription = nil, &block) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/google4r/checkout/shared.rb', line 274

def create_subscription(subscription=nil, &block)
  
  if @subscription.nil?
    if subscription.nil?
      @subscription = Subscription.new
    else
      @subscription = subscription
    end
  end
  
  if block_given?
    yield @subscription
  end
  
  return @subscription
end