SupaIrish
SupaIrish

Reputation: 766

Ruby Savon Requests

@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?

Upvotes: 1

Views: 346

Answers (1)

rubiii
rubiii

Reputation: 6983

  1. The class-method example was just that, an example. Feel free to use any object that responds to to_s.

  2. The block is processed via instance_eval with delegation, which is why you can only use local variables and methods inside the block. If you need to use instance variables, change your block to accept arguments. Savon will notice that you specified arguments and yield those values instead of evaluating the block.

For information about which arguments to specifiy and everything else, please RTFM ;)

Upvotes: 1

Related Questions