Sha256: de5e149f7b597df05ad1b76843cf4bf95c6068e0d1fba36e34c55b6cc313cacc

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

require "savon/error"
require "savon/soap/xml"

module Savon
  module SOAP

    # = Savon::SOAP::Fault
    #
    # Represents a SOAP fault. Contains the original <tt>HTTPI::Response</tt>.
    class Fault < Error

      # Expects an <tt>HTTPI::Response</tt>.
      def initialize(http)
        self.http = http
      end

      # Accessor for the <tt>HTTPI::Response</tt>.
      attr_accessor :http

      # Returns whether a SOAP fault is present.
      def present?
        @present ||= http.body[0,1000] =~ /<(.+:)?Body>(\s*)<(.+:)?Fault>/
      end

      # Returns the SOAP fault message.
      def to_s
        return "" unless present?
        @message ||= message_by_version to_hash[:fault]
      end

      # Returns the SOAP response body as a Hash.
      def to_hash
        @hash ||= Savon::SOAP::XML.to_hash http.body
      end

    private

      def message_by_version(fault)
        if fault[:faultcode]
          "(#{fault[:faultcode]}) #{fault[:faultstring]}"
        elsif fault[:code]
          "(#{fault[:code][:value]}) #{fault[:reason][:text]}"
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
savon-0.8.4 lib/savon/soap/fault.rb
savon-0.8.3 lib/savon/soap/fault.rb
savon-0.8.2 lib/savon/soap/fault.rb