Search code examples
rubysavon

Who use class variables in SOAP calls with Savon?


I'm writing a little client using Ruby and Savon. The interface changed significantly from version 0.7 to 0.8.x. All my calls don't work anymore :-(. How can I pass on a local member variable. Please see the example, @userName and @userPassword are not defined within the block.


begin
    @response = @authentication_svc.request :wsdl, "AuthenticateUser" do  
        http.headers["SOAPAction"] = "AuthenticateUser"  
        soap.body = "#{@userName}#{@passwd}"  
    end  
rescue Savon::SOAP::Fault => e  
    @last_soap_error = e.message  
end  


Solution

  • To avoid having to assign everything to local variables, write methods which take objects (like soap and http) from inside the block. Because the methods belong to the class (not the instance), they can still be called from within the block, but once you are in the context of the method, your instance variables are available to you.

    def do_request
      begin
        @response = @authentication_svc.request :wsdl, "AuthenticateUser" do  
          prepare_soap(soap,http)
        end
      rescue Savon::SOAP::Fault => e  
        @last_soap_error = e.message  
      end 
    end
    
    def prepare_soap(soap, http)
      http.headers["SOAPAction"] = "AuthenticateUser"  
      soap.body = "#{@userName}#{@passwd}"  
    end