lib/savon/response.rb in smacks-savon-0.0.4 vs lib/savon/response.rb in smacks-savon-0.0.5
- old
+ new
@@ -1,62 +1,106 @@
-# -*- coding: utf-8 -*-
-require 'rubygems'
-require 'apricoteatsgorilla'
+require "rubygems"
+require "hpricot"
+require "apricoteatsgorilla"
module Savon
- # Savon::Response represents the SOAP response and includes methods for
- # working with the raw XML, a Hash or a Savon::Mash object.
+ # Savon::Response represents the HTTP response.
+ #
+ # === Checking for HTTP and SOAP faults
+ #
+ # response.success?
+ # response.fault?
+ #
+ # === Access the fault message and code
+ #
+ # response.fault
+ # response.fault_code
+ #
+ # === Different response formats
+ #
+ # # raw XML response:
+ # response.to_s
+ #
+ # # response as a Hash
+ # response.to_hash
+ #
+ # # response as a Hash starting at a custom root node (via XPath)
+ # response.to_hash("//item")
+ #
+ # # response as a Mash
+ # response.to_mash
+ #
+ # # response as a Mash starting at a custom root node (via XPath)
+ # response.to_mash("//user/email")
+ #
+ # ===
class Response
+ attr_reader :fault, :fault_code
+
# Initializer to set the SOAP response.
+ #
+ # === Parameters
+ #
+ # * +response+ - The Net::HTTP response.
def initialize(response)
@response = response
+ validate
end
+ # Returns true if the request was successful, false otherwise.
+ def success?
+ @fault_code.nil?
+ end
+
+ # Returns true if the request was not successful, false otherwise.
+ def fault?
+ !@fault.nil?
+ end
+
# Returns the SOAP response message as a Hash. Call with XPath expession
- # as parameter to define a custom root node. The root node itself will not
- # be included in the Hash.
+ # to define a custom +root_node+ to start parsing at. Defaults to "//return".
+ # The root node itself will not be included in the Hash.
+ #
+ # === Parameters
+ #
+ # * +root_node+ - Optional. Custom root node to start parsing at. Defaults to "//return".
def to_hash(root_node = "//return")
- ApricotEatsGorilla(@response.body, root_node)
+ return nil if fault?
+ ApricotEatsGorilla[@response.body, root_node]
end
# Returns the SOAP response message as a Savon::Mash object. Call with
- # XPath expession as parameter to define a custom root node. The root node
- # itself will not be included in the Mash object.
+ # XPath expession to define a custom +root_node+. Defaults to "//return".
+ # The root node itself will not be included in the Mash object.
+ #
+ # === Parameters
+ #
+ # * +root_node+ - Optional. Custom root node to start parsing at. Defaults to "//return".
def to_mash(root_node = "//return")
+ return nil if fault?
hash = to_hash(root_node)
Savon::Mash.new(hash)
end
# Returns the raw XML response.
def to_s
@response.body
end
- end
+ private
- # Savon::Mash converts a given Hash into a Mash object.
- class Mash
-
- def initialize(hash)
- hash.each do |key,value|
- value = Savon::Mash.new value if value.is_a? Hash
-
- if value.is_a? Array
- value = value.map do |item|
- if item.is_a?(Hash) then Savon::Mash.new(item) else item end
- end
- end
-
- # Create and initialize an instance variable for this key/value pair
- self.instance_variable_set("@#{key}", value)
- # Create the getter that returns the instance variable
- self.class.send(:define_method, key, proc{self.instance_variable_get("@#{key}")})
- # Create the setter that sets the instance variable
- self.class.send(:define_method, "#{key}=", proc{|value| self.instance_variable_set("@#{key}", value)})
+ # Checks for and stores HTTP and SOAP-Fault errors.
+ def validate
+ if @response.code.to_i >= 300
+ @fault = @response.message
+ @fault_code = @response.code
+ else
+ fault = to_hash("//soap:Fault")
+ @fault = fault[:faultstring] unless fault.nil?
+ @fault_code = fault[:faultcode] unless fault.nil?
end
end
end
-
end
\ No newline at end of file