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 = {}) ⇒ void

Since:

  • 0.1.0



23
24
25
26
27
28
29
# File 'lib/endicia_label_server/builders/builder_base.rb', line 23

def initialize(root_name, opts = {})
  initialize_xml_roots root_name

  document << root

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

Instance Attribute Details

#documentOx::Document

The XML Document being built

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

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(*args) ⇒ Object

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/endicia_label_server/builders/builder_base.rb', line 31

def add(*args)
  first_arg = args.first
  last_arg = args.last
  root_key = Util.camelize(first_arg)

  if last_arg.is_a? Hash
    add_hash root_key, last_arg
  elsif last_arg.is_a? Array

  else
    root << element_with_value(root_key, last_arg)
  end
end

#add_array(root_key, data) ⇒ Object

Since:

  • 0.1.0



55
56
57
58
59
60
61
62
63
# File 'lib/endicia_label_server/builders/builder_base.rb', line 55

def add_array(root_key, data)
  xml_root_key = Util.camelize(root_key)
  root << Element.new(xml_root_key).tap do |org|
    child_key = "#{Util.singularize(xml_root_key)}ID"
    data.each do |value|
      org << element_with_value(child_key, value)
    end
  end
end

#add_hash(root_key, data) ⇒ Object

Since:

  • 0.1.0



45
46
47
48
49
50
51
52
53
# File 'lib/endicia_label_server/builders/builder_base.rb', line 45

def add_hash(root_key, data)
  xml_root_key = (root_key.is_a? String) ? root_key : Util.camelize(root_key)
  root << Element.new(xml_root_key).tap do |org|
    data.each_pair do |key, value|
      xml_child_key = (key.is_a? String) ? key : Util.camelize(key)
      org << element_with_value(xml_child_key, value)
    end
  end
end

#to_http_postObject

Since:

  • 0.1.0



72
73
74
# File 'lib/endicia_label_server/builders/builder_base.rb', line 72

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

#to_xmlString

Returns a String representation of the XML document being built

Since:

  • 0.1.0



68
69
70
# File 'lib/endicia_label_server/builders/builder_base.rb', line 68

def to_xml
  Ox.to_xml document
end