class EndiciaLabelServer::Builders::BuilderBase

The {BuilderBase} class builds Endicia XML Objects.

@author Paul Trippett @since 0.1.0 @abstract @attr [Ox::Document] document The XML Document being built @attr [Ox::Element] root The XML Root

Attributes

document[RW]
root[RW]

Public Class Methods

new(root_name, opts = {}, root_attributes = nil) click to toggle source

Initializes a new {BuilderBase} object

@param [String] root_name The Name of the XML Root @return [void]

# File lib/endicia_label_server/builders/builder_base.rb, line 23
def initialize(root_name, opts = {}, root_attributes = nil)
  initialize_xml_roots(root_name)
  assign_root_attributes(root_attributes) if root_attributes

  document << root

  opts.each_pair { |k, v| add(k, v, root) }
end

Public Instance Methods

add(key, value, parent_element = nil) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 32
def add(key, value, parent_element = nil)
  parent = parent_element || root
  key = (key.is_a? String) ? key : Util.camelize(key)

  return add_element_from_hash_values(parent, key, value) if value.is_a?(Hash)
  return add_element_from_array_items(parent, key, value) if value.is_a?(Array)
  return add_single_element(parent, key, value)
end
assign_root_attributes(root_attributes) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 41
def assign_root_attributes(root_attributes)
  root_attributes.each do |attr_key, attr_value|
    root[Util.camelize(attr_key)] = attr_value
  end
end
to_http_post() click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 54
def to_http_post
  "#{post_field}=#{to_xml}"
end
to_xml(opts = {}) click to toggle source

Returns a String representation of the XML document being built

@return [String]

# File lib/endicia_label_server/builders/builder_base.rb, line 50
def to_xml(opts = {})
  Ox.to_xml(document, opts)
end

Private Instance Methods

add_element_from_array_items(parent_element, key, value) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 66
def add_element_from_array_items(parent_element, key, value)
  parent_element << Element.new(key).tap do |element|
    value.each do |array_item|
      array_item.each_pair do |child_key, child_value|
        add(child_key, child_value, element)
      end
    end
  end
end
add_element_from_hash_values(parent_element, key, value) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 60
def add_element_from_hash_values(parent_element, key, value)
  parent_element << Element.new(key).tap do |element|
    value.each_pair { |child_key, child_value| add(child_key, child_value, element) }
  end
end
add_single_element(parent_element, key, value) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 76
def add_single_element(parent_element, key, value)
  parent_element << element_with_value(key, value)
end
element_with_value(name, value) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 85
def element_with_value(name, value)
  fail InvalidAttributeError, name unless value.respond_to?(:to_str)
  Element.new(name).tap do |request_action|
    request_action << value.to_str
  end
end
initialize_xml_roots(root_name) click to toggle source
# File lib/endicia_label_server/builders/builder_base.rb, line 80
def initialize_xml_roots(root_name)
  self.document = Document.new
  self.root = Element.new(root_name)
end