Class: WSDL::Response::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/wsdl/response/builder.rb

Overview

Builds SOAP response XML from a Ruby hash using WSDL schema elements.

This is the inverse of Parser: where the parser converts SOAP XML into Ruby hashes, the builder converts Ruby hashes back into SOAP XML. The hash structure follows the same convention as Contract::Template#to_h, with type placeholders replaced by actual values.

The builder validates the hash against the schema during construction, catching unknown elements, missing required elements, type mismatches, and cardinality violations.

Examples:

Building a SOAP response

builder = WSDL::Response::Builder.new(
  schema_elements: operation.contract.response.body.elements,
  soap_version: '1.1'
)

xml = builder.to_xml(
  details: {
    bezeichnung: 'Deutsche Bank',
    bic: 'DEUTDEMM',
    ort: 'München',
    plz: '80271'
  }
)

Validating without building XML

builder.validate!(details: { bezeichnung: 'Deutsche Bank' })

Constant Summary collapse

TYPE_MAP =

Maps XSD type groups to accepted Ruby classes for validation.

Returns:

  • (Hash{Symbol => Array<Class>})
{
  string: [String],
  integer: [Integer],
  decimal: [Integer, Float, BigDecimal],
  float: [Integer, Float],
  boolean: [TrueClass, FalseClass],
  date: [Date, String],
  datetime: [Time, String],
  time: [Time, String],
  base64: [String],
  hex_binary: [String],
  list: [Array]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(schema_elements:, soap_version: '1.1', output_style: 'document/literal', operation_name: nil, output_namespace: nil) ⇒ Builder

Creates a new builder for the given output schema.

For RPC/literal operations, pass output_style, operation_name, and output_namespace so the builder wraps message parts in the standard operationNameResponse element per SOAP 1.1 §7.1.

Parameters:

  • schema_elements (Array<WSDL::XML::Element>)

    the output schema elements (typically from operation.contract.response.body.elements)

  • soap_version (String) (defaults to: '1.1')

    SOAP version ('1.1' or '1.2')

  • output_style (String) (defaults to: 'document/literal')

    binding style ('document/literal' or 'rpc/literal')

  • operation_name (String, nil) (defaults to: nil)

    operation name (required for RPC/literal)

  • output_namespace (String, nil) (defaults to: nil)

    soap:body namespace for the RPC wrapper



67
68
69
70
71
72
73
74
75
# File 'lib/wsdl/response/builder.rb', line 67

def initialize(schema_elements:, soap_version: '1.1',
               output_style: 'document/literal',
               operation_name: nil, output_namespace: nil)
  @schema_elements = schema_elements
  @soap_version = soap_version
  @output_style = output_style
  @operation_name = operation_name
  @output_namespace = output_namespace
end

Instance Method Details

#to_xml(hash) ⇒ String

Validates and serializes a Ruby hash into a SOAP envelope XML string.

The hash represents the content inside the wrapper element. For example, if the schema defines getBankResponse as the wrapper, the hash should contain the children of that element (e.g. { details: { ... } }).

Parameters:

  • hash (Hash)

    the response data

Returns:

  • (String)

    the SOAP envelope XML

Raises:



86
87
88
89
# File 'lib/wsdl/response/builder.rb', line 86

def to_xml(hash)
  validate!(hash)
  serialize(hash)
end

#validate!(hash) ⇒ void

This method returns an undefined value.

Validates a Ruby hash against the output schema without serializing.

Useful for checking response definitions at load time.

Parameters:

  • hash (Hash)

    the response data to validate

Raises:



98
99
100
101
102
103
104
105
106
107
# File 'lib/wsdl/response/builder.rb', line 98

def validate!(hash)
  wrapper = @schema_elements.first
  return if wrapper.nil?

  if wrapper.complex_type?
    validate_hash(hash, wrapper, path: wrapper.name)
  else
    validate_parts(hash, @schema_elements)
  end
end