Class: EndiciaLabelServer::Builders::BuilderBase Abstract
- Inherits:
-
Object
- Object
- EndiciaLabelServer::Builders::BuilderBase
- 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.
Direct Known Subclasses
ChangePassPhraseBuilder, PostageLabelBuilder, PostageRateBuilder, PostageRatesBuilder, UserSignUpBuilder
Instance Attribute Summary collapse
-
#document ⇒ Ox::Document
The XML Document being built.
-
#root ⇒ Ox::Element
The XML Root.
Instance Method Summary collapse
- #add(*args) ⇒ Object
- #add_array(root_key, data) ⇒ Object
- #add_hash(root_key, data) ⇒ Object
-
#initialize(root_name, opts = {}) ⇒ void
constructor
Initializes a new BuilderBase object.
- #to_http_post ⇒ Object
-
#to_xml ⇒ String
Returns a String representation of the XML document being built.
Constructor Details
#initialize(root_name, opts = {}) ⇒ void
Initializes a new EndiciaLabelServer::Builders::BuilderBase object
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
#document ⇒ Ox::Document
The XML Document being built
12 13 14 |
# File 'lib/endicia_label_server/builders/builder_base.rb', line 12 def document @document end |
#root ⇒ Ox::Element
The XML Root
12 13 14 |
# File 'lib/endicia_label_server/builders/builder_base.rb', line 12 def root @root end |
Instance Method Details
#add(*args) ⇒ Object
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
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
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_post ⇒ Object
72 73 74 |
# File 'lib/endicia_label_server/builders/builder_base.rb', line 72 def to_http_post "#{post_field}=#{to_xml}" end |
#to_xml ⇒ String
Returns a String representation of the XML document being built
68 69 70 |
# File 'lib/endicia_label_server/builders/builder_base.rb', line 68 def to_xml Ox.to_xml document end |