Class: Google4R::Checkout::CheckoutCommandXmlGenerator

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

Overview

Use the CheckoutXmlGenerator to create an XML document from a CheckoutCommand object.

Usage:

checkout = CheckoutCommand.new
# set up the CheckoutCommand

generator = CheckoutCommandXmlGenerator.new(checkout)
puts generator.generate # => "<xml? version=..."
File.new('some.xml', 'w') { |f| f.write generator.generate }

– TODO

  • Refactor the big, monolitic generator into smaller, easier testable ones. One for each major part of the resulting XML document. This will also reduce the overhead in generating other types of XML documents.

++

Instance Method Summary collapse

Constructor Details

#initialize(checkout_command) ⇒ CheckoutCommandXmlGenerator

Initializes the CheckoutCommandXmlGenerator with the CheckoutCommand it is to conver to XML.



56
57
58
# File 'lib/google4r/checkout/xml_generation.rb', line 56

def initialize(checkout_command)
  @checkout_command = checkout_command
end

Instance Method Details

#generateObject

Creates an XML document from the checkout_command passed into the constructor. Returns the resulting XML string.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/google4r/checkout/xml_generation.rb', line 62

def generate
  @document = REXML::Document.new
  
  declaration = REXML::XMLDecl.new
  declaration.encoding = 'utf-8'
  @document << declaration
  
  self.process_checkout_command(@checkout_command)
  
  io = StringIO.new
  @document.write(io, 0) # TODO: Maybe replace 0 by -1 so no spaces are inserted?
  return io.string
end