Class: Google4R::Checkout::MerchantCalculationResults

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

Overview

This class represents a merchant-calculation-results XML

Usage Sample

results = MerchantCalculationResults.new coupon_result = CouponResult.new(true, 'FirstVisitCoupon', Money.new(500, 'USD'), 'Congratulations! You saved $5.00 on your first visit!') gift_certificate_result = GiftCertificateResult.new(true, 'GiftCert012345', Money.new(1000, 'USD'), 'You used your Gift Certificate!')

results.create_merchant_calculation_result do |result| result.shipping_name = 'SuperShip' result.address_id = '739030698069958' result.shipping_rate = Money.new(703, 'USD') result.shippable = true result.total_tax = Money.new(1467, 'USD') result.create_merchant_code_result(@coupon_result) result.create_merchant_code_result(@gift_certificate_result) end

results.create_merchant_calculation_result do |result| result.shipping_name = 'UPS Ground' result.address_id = '739030698069958' result.shipping_rate = Money.new(556, 'USD') result.shippable = true result.total_tax = Money.new(1467, 'USD') result.create_merchant_code_result(@coupon_result) result.create_merchant_code_result(@gift_certificate_result) end

results.to_xml # To create the XML to return to Google

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMerchantCalculationResults

Returns a new instance of MerchantCalculationResults.



199
200
201
# File 'lib/google4r/checkout/merchant_calculation.rb', line 199

def initialize()
  @merchant_calculation_results = Array.new
end

Instance Attribute Details

#merchant_calculation_resultsObject (readonly)

An array of merchant calcuation results



197
198
199
# File 'lib/google4r/checkout/merchant_calculation.rb', line 197

def merchant_calculation_results
  @merchant_calculation_results
end

Instance Method Details

#create_merchant_calculation_result(result = nil, &block) ⇒ Object

This method takes a MerchantCalculationResult object and add it to the merchant_calculation_results array. If the object is not provided, it will instantiate one and add it to the array. An optional code block can be supplied to set the attributes of the new MerchantCalculationResult object.

Raises RuntimeError exceptions if the given object is not of type MerchantCalculationResult.



210
211
212
213
214
215
216
217
218
219
# File 'lib/google4r/checkout/merchant_calculation.rb', line 210

def create_merchant_calculation_result(result=nil, &block)
  if result.nil?
    result = MerchantCalculationResult.new
    # Pass the newly generated item to the given block to set its attributes.
    yield(result) if block_given?
  else
    raise "Not a MerchantCalculationResult!" unless result.kind_of?(MerchantCalculationResult)
  end
  @merchant_calculation_results << result
end

#to_xmlObject



221
222
223
# File 'lib/google4r/checkout/merchant_calculation.rb', line 221

def to_xml()
  return MerchantCalculationResultsXmlGenerator.new(self).generate()
end