谁在 Savon 的 SOAP 调用中使用类变量?

发布于 2024-10-16 02:46:19 字数 429 浏览 5 评论 0原文

我正在使用 Ruby 和 Savon 编写一个小客户端。从版本 0.7 到 0.8.x,界面发生了显着变化。我所有的电话都不再工作:-(。 如何传递局部成员变量。 请参阅示例,@userName 和 @userPassword 未在块内定义。


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  

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当爱已成负担 2024-10-23 02:46:19

更仔细地阅读变更日志后,我了解到 Savon 使用 instance_eval 来执行该块。因此,不能在该块中使用另一类的实例变量。变更日志中也说明了这一点。
为了解决我的问题,我将实例变量的值分配给局部变量。这对我来说很有效。

After reading the changelog more carefully I understood that Savon uses instance_eval to execute the block. Therefore one cannot use instance variables of another class within that block. That's also stated in the changelog.
To solve my problem I assigned the value of the instance variable to a local variable. That did the trick for me.

幸福不弃 2024-10-23 02:46:19

恐怕你的问题没有多大意义。

  • 块可以访问块外可用的任何内容。
  • @userName@passwd 是实例变量,而不是本地变量或类变量。 Ruby 没有任何通常称为“成员变量”的东西。无论如何,如果它们是在您进行此调用的类中设置的,则您可以在块内很好地访问它们。

作为脚注,在 Ruby 中命名变量时,惯例是使用下划线而不是驼峰式大小写 - @user_name 而不是 @userName

I'm afraid your question does not make much sense.

  • Blocks have access to anything available outside the block.
  • @userName and @passwd are instance variables, not local or class variables. Ruby doesn't have anything normally called "member variables." In any case, if they are set in the class where you are making this call, you can access them just fine within the block.

As a footnote, the convention is to use underscores and not camel case for naming variables in Ruby -- @user_name instead of @userName.

喜你已久 2024-10-23 02:46:19

为了避免将所有内容分配给局部变量,请编写从块内部获取对象(例如soap和http)的方法。因为这些方法属于类(而不是实例),所以仍然可以从块内调用它们,但是一旦处于方法的上下文中,实例变量就可供您使用。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文