2

I am consuming a web service using ruby (1.8.7) and soap4r (1.5.8). The web service has a method named "type" and I am not able to get the value.

@driver=SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver
result = @driver.GetJob(:jobid => "123")
puts "jobname is #{result.name}"
puts "jobtype is #{result.type}"

The fourth line gives me "warning: Object#type is deprecated; use Object#class". I know that Object.type is deprecated. I want to call the "type" method of the result obtained from the web service. I don't own the web service so I can't change it.

Any help appreciated. Thanks in advance.

MPK
  • 133
  • 2
  • 9
  • What about `result.send :type`? – Sergio Tulentsev Oct 22 '12 at 20:24
  • What type is `result`? Which methods it has? – Mladen Jablanović Oct 22 '12 at 20:30
  • Thanks for answering. When I used "result.send :type" I got - "SOAP::Mapping::Object" and the same warning - "warning: Object#type is deprecated; use Object#class" – MPK Oct 22 '12 at 20:34
  • @Mladen Jablanović - The object result contains the response given by the web service. I can provide the XML response also if needed. Output of result.methods is ["inspect", "summary", "priority", "name", "tap", "clone", "type", "protected_methods", "==", "marshal_dump", "fileIndex", "===", "is_a?", "[]", "startTime", "meta"] – MPK Oct 22 '12 at 20:37
  • Just thought this would be helpful too. The XML response of the web service. 6867bb85-9937-410b-8703-f876e65235c3 0 Undefined – MPK Oct 22 '12 at 20:54

3 Answers3

4

The type method is probably invoked using method_missing and when the method exists on Object this mechanism doesn't work. If so this little piece of monkey patching gets rid of Object.type:

class Object
  undef_method :type
end

Put this code somewhere before the call to the web service and it should work.

Another way to solve it is upgrading to ruby 1.9. The type method is gone!

froderik
  • 4,642
  • 3
  • 33
  • 43
  • Thanks @froderik for your answer. IT worked! Thankfully I encountered only one method ("type") while consuming the web service, and used your suggestion to get the output I want. Hopefully there no such ambiguity. Regarding upgrade to 1.9, there is huge code base to migrate so I will eventually get there some day. – MPK Oct 23 '12 at 06:35
  • 2
    Just be careful with monkey-patching the whole Object class, IMO it would be better just to monkey-patch the object involved i.e. `class << result; undef_method :type; end` – Mladen Jablanović Oct 23 '12 at 08:06
1

I would look for a method that allows you to pass in a symbol representing the attribute you want. It'd be something like

puts "jobtype is #{result.some_method(:type)}"

or (less likely)

puts "jobtype is #{result.some_method("type")}"

@froderik's answer may help you for one particular scenario, but what if the attribute you wanted was called "class", or "send", or "initialize"? The soap library would have to handle such scenarios somehow, unless it was badly designed.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • Thanks Andrew for your answer. Your approach was the first thing I too took when I observed the error. I looked in the WSDL and in the client (stub) generated from wsdl2ruby utility. The web service provider (written in .Net) did not incorporate such functionality. I will try froderik's solution in my code and test it – MPK Oct 23 '12 at 06:26
1

Just guessing, but how about result['type']? Or result[:type] ?

maniek
  • 7,087
  • 2
  • 20
  • 43
  • Thanks for answering. Just tried 'puts "type is #{result['type']}"' and 'puts "type2 is #{result[:type]}"'. The output in the console is "type is" and "type2 is" (i.e. empty) – MPK Oct 23 '12 at 06:20