class Aws::Xml::Builder

Public Class Methods

new(rules, options = {}) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 11
def initialize(rules, options = {})
  @rules = rules
  @location_name =
    options[:location_name].nil? ? @rules.location_name : options[:location_name]
  @xml = options[:target] || []
  indent = options[:indent] || ''
  pad = options[:pad] || ''
  @builder = DocBuilder.new(target: @xml, indent: indent, pad: pad)
end

Public Instance Methods

serialize(params)
Alias for: to_xml
to_xml(params) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 21
def to_xml(params)
  structure(@location_name, @rules, params)
  @xml.join
end
Also aliased as: serialize

Private Instance Methods

blob(value) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 102
def blob(value)
  value = value.read unless String === value
  Base64.strict_encode64(value)
end
list(name, ref, values) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 52
def list(name, ref, values)
  if ref[:flattened] || ref.shape.flattened
    values.each do |value|
      member(name, ref.shape.member, value)
    end
  else
    node(name, ref) do
      values.each do |value|
        mname = ref.shape.member.location_name || 'member'
        member(mname, ref.shape.member, value)
      end
    end
  end
end
map(name, ref, hash) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 67
def map(name, ref, hash)
  key_ref = ref.shape.key
  value_ref = ref.shape.value
  if ref[:flattened] || ref.shape.flattened
    hash.each do |key, value|
      node(name, ref) do
        member(key_ref.location_name || 'key', key_ref, key)
        member(value_ref.location_name || 'value', value_ref, value)
      end
    end
  else
    node(name, ref) do
      hash.each do |key, value|
        # Pass in a new ShapeRef to create an entry node
        node('entry', ShapeRef.new) do
          member(key_ref.location_name || 'key', key_ref, key)
          member(value_ref.location_name || 'value', value_ref, value)
        end
      end
    end
  end
end
member(name, ref, value) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 90
def member(name, ref, value)
  case ref.shape
  when StructureShape then structure(name, ref, value)
  when ListShape      then list(name, ref, value)
  when MapShape       then map(name, ref, value)
  when TimestampShape then node(name, ref, timestamp(ref, value))
  when BlobShape      then node(name, ref, blob(value))
  else
    node(name, ref, value.to_s)
  end
end
node(name, ref, *args, &block) click to toggle source

The ‘args` list may contain:

* [] - empty, no value or attributes
* [value] - inline element, no attributes
* [value, attributes_hash] - inline element with attributes
* [attributes_hash] - self closing element with attributes

Pass a block if you want to nest XML nodes inside. When doing this, you may not pass a value to the ‘args` list.

# File lib/aws-sdk-core/xml/builder.rb, line 127
def node(name, ref, *args, &block)
  attrs = args.last.is_a?(Hash) ? args.pop : {}
  attrs = shape_attrs(ref).merge(attrs)
  args << attrs
  @builder.node(name, *args, &block)
end
shape_attrs(ref) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 134
def shape_attrs(ref)
  if (xmlns = ref['xmlNamespace'])
    case xmlns
    when String
      { 'xmlns' => xmlns }
    when Hash
      if (prefix = xmlns['prefix'])
        { "xmlns:#{prefix}" => xmlns['uri'] }
      else
        { 'xmlns' => xmlns['uri'] }
      end
    end
  else
    {}
  end
end
structure(name, ref, values) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 29
def structure(name, ref, values)
  if values.empty?
    node(name, ref)
  else
    node(name, ref, structure_attrs(ref, values)) do
      ref.shape.members.each do |member_name, member_ref|
        next if values[member_name].nil?
        next if xml_attribute?(member_ref)
        member(member_ref.location_name, member_ref, values[member_name])
      end
    end
  end
end
structure_attrs(ref, values) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 43
def structure_attrs(ref, values)
  ref.shape.members.inject({}) do |attrs, (member_name, member_ref)|
    if xml_attribute?(member_ref) && values.key?(member_name)
      attrs[member_ref.location_name] = values[member_name]
    end
    attrs
  end
end
timestamp(ref, value) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 107
def timestamp(ref, value)
  case ref['timestampFormat'] || ref.shape['timestampFormat']
  when 'unixTimestamp' then value.to_i
  when 'rfc822' then value.utc.httpdate
  else
    # xml defaults to iso8601
    value.utc.iso8601
  end
end
xml_attribute?(ref) click to toggle source
# File lib/aws-sdk-core/xml/builder.rb, line 151
def xml_attribute?(ref)
  !!ref['xmlAttribute']
end