Sha256: d9c0f3278cb786eeedda12bf3bda358749e105ecdcb6247278015819df7cf4da

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'lolsoap/errors'
require 'lolsoap/fault'
require 'lolsoap/hash_builder'
require 'nokogiri'

module LolSoap
  class Response
    attr_reader :request, :doc

    # Create a new instance from a raw XML string
    def self.parse(request, raw)
      new(request, Nokogiri::XML::Document.parse(raw))
    end

    def initialize(request, doc)
      @request = request
      @doc     = doc

      raise FaultRaised.new(fault) if fault
    end

    # Namespace used for SOAP Envelope tags
    def soap_namespace
      request.soap_namespace
    end

    # The XML node for the body of the envelope
    def body
      @body ||= doc.at_xpath('/soap:Envelope/soap:Body/*', 'soap' => soap_namespace)
    end

    # Convert the body node to a Hash, using WSDL type data to determine the structure
    def body_hash(builder = HashBuilder)
      builder.new(body, request.output_type).output
    end

    # The XML node for the header of the envelope
    def header
      @header ||= doc.at_xpath('/soap:Envelope/soap:Header', 'soap' => soap_namespace)
    end

    # SOAP fault, if any (an exception will be raised in the initializer, if there is one)
    def fault
      @fault ||= begin
        node = doc.at_xpath('/soap:Envelope/soap:Body/soap:Fault', 'soap' => soap_namespace)
        Fault.new(request, node) if node
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lolsoap-0.1.1 lib/lolsoap/response.rb
lolsoap-0.1.0 lib/lolsoap/response.rb