Class: EndiciaLabelServer::Builders::BuilderBase Abstract

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Ox
Defined in:
lib/endicia_label_server/builders/builder_base.rb

Overview

This class is abstract.

The BuilderBase class builds Endicia XML Objects.

Author:

  • Paul Trippett

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_name, opts = {}, root_attributes = nil) ⇒ void

Parameters:

  • root_name (String)

    The Name of the XML Root

Since:

  • 0.1.0



23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#documentOx::Document

The XML Document being built

Returns:

  • (Ox::Document)

    the current value of document

Since:

  • 0.1.0



12
13
14
# File 'lib/endicia_label_server/builders/builder_base.rb', line 12

def document
  @document
end

#rootOx::Element

The XML Root

Returns:

  • (Ox::Element)

    the current value of root

Since:

  • 0.1.0



12
13
14
# File 'lib/endicia_label_server/builders/builder_base.rb', line 12

def root
  @root
end

Instance Method Details

#add(key, value, parent_element = nil) ⇒ Object

Since:

  • 0.1.0



32
33
34
35
36
37
38
39
# 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) ⇒ Object

Since:

  • 0.1.0



41
42
43
44
45
# 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_postObject

Since:

  • 0.1.0



54
55
56
# File 'lib/endicia_label_server/builders/builder_base.rb', line 54

def to_http_post
  "#{post_field}=#{to_xml}"
end

#to_xml(opts = {}) ⇒ String

Returns a String representation of the XML document being built

Returns:

  • (String)

Since:

  • 0.1.0



50
51
52
# File 'lib/endicia_label_server/builders/builder_base.rb', line 50

def to_xml(opts = {})
  Ox.to_xml(document, opts)
end