1

@rubiii has previously shown (Savon soap body problem) that you can customize Savon requests with

class SomeXML
  def self.to_s
    "<some>xml</some>"
  end
end

client.request :some_action do
  soap.body = SomeXML
end

But why would you use a class method like this? It would seem more likely that you would ask an instance of a class to turn itself into a hash for the request body. i.e.

@instance = SomeClass.new

client.request :some_action do
  soap.body = @instance.to_soap
end

However, when I try doing this, @instance variable isn't in 'scope' within the request block. So I get a can't call method to_soap on nil. But if instead I use a class method then I can get it to work. i.e.

class SomeClass
  @@soap_hash = nil

  def self.soap_hash=(hash)
    @@soap_hash = hash
  end

  def self.soap_hash
    @@soap_hash
  end
end

SomeClass.soap_hash = @instance.to_soap

client.request :some_action do
  soap.body = SomeClass.soap_hash
end

I don't get it?

4

1 回答 1

1
  1. 类方法示例就是这样,一个示例。随意使用任何响应的对象to_s

  2. 该块是通过带有委托的 instance_eval处理的,这就是为什么您只能在块内使用局部变量和方法的原因。如果您需要使用实例变量,请将您的块更改为接受参数。Savon 会注意到您指定了参数并产生这些值而不是评估块。

有关要指定的参数和其他所有内容的信息,请 RTFM ;)

于 2011-10-21T09:26:14.863 回答