ruby - 使用 soap4r 访问 SOAP 服务无法访问返回对象的内容

标签 ruby soap wsdl soap4r

所以我需要从我的 Rails 应用程序访问此服务。我正在使用 soap4r 读取 WSDL 并动态生成访问服务的方法。

根据我的阅读,我应该能够链接方法来访问嵌套的 XML 节点,但我无法让它工作。我尝试使用 wsdl2ruby 命令并通读生成的代码。据我所知,soap 库没有生成这些访问器方法。我是 ruby​​ 的新手,所以我不知道我是否遗漏了什么?

我知道当我检查元素时,我可以看到我想要的数据。我就是做不到。

例如,如果我使用以下代码:

require "soap/wsdlDriver"
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
response = driver.getChannels('nill')
puts response.inspect

我得到以下输出:

ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}binding
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}operation
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}body
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}address
#<SOAP::Mapping::Object:0x80b96394 {http://www.trms.com/CablecastWS/}GetChannelsResult=#<SOAP::Mapping::Object:0x80b96178 {http://www.trms.com/CablecastWS/}Channel=[#<SOAP::Mapping::Object:0x80b95f5c {http://www.trms.com/CablecastWS/}ChannelID="1" {http://www.trms.com/CablecastWS/}Name="CTN 5">, #<SOAP::Mapping::Object:0x80b9519c {http://www.trms.com/CablecastWS/}ChannelID="2" {http://www.trms.com/CablecastWS/}Name="PPAC 2">, #<SOAP::Mapping::Object:0x80b94620 {http://www.trms.com/CablecastWS/}ChannelID="14" {http://www.trms.com/CablecastWS/}Name="Test Channel">]>>

所以数据肯定在那里!

这是 wsdl2ruby 为上面使用的方法生成的代码:

# {http://www.trms.com/CablecastWS/}GetChannels
class GetChannels
  def initialize
  end
end

# {http://www.trms.com/CablecastWS/}GetChannelsResponse
#   getChannelsResult - ArrayOfChannel
class GetChannelsResponse
  attr_accessor :getChannelsResult

  def initialize(getChannelsResult = nil)
    @getChannelsResult = getChannelsResult
  end
end

抱歉发了这么长的帖子,我认为信息越多,就越有可能有人能为我指明正确的方向。

谢谢

射线

最佳答案

回答

require "soap/wsdlDriver"
wsdl = "http://frontdoor.ctn5.org/CablecastWS/CablecastWS.asmx?WSDL"
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
response = driver.getChannels('nill')

for item in response.getChannelsResult.channel
  puts item.name
  puts item.channelID
end

我是如何得到答案的

你可以通过

找出响应的方法
response.methods

这会给你带来一长串难以分类的方法,所以我想去掉通用方法。 Ruby 可让您减去数组。

response.methods - Object.new.methods

使用此技术,我找到了用于响应的 getChannelsResult 方法。我重复了这个过程

resonse.getChannelsResult.methods - Object.new.methods

我找到了其结果的 channel 方法。再来一次!

response.getChannelsResult.channel.methods - Object.new.methods

这返回了一堆方法,包括:sort、min、max 等。所以我猜是 Array。一个简单的确认是有序的

response.getChannelsResult.channel.class

果然返回了Array。为了简单起见,我只使用数组的第一项来获取它的方法

response.getChannelsResult.channel.first.methods - Object.new.methods

哇哦,我又找到了两个方法“name”和“channelID”

关于ruby - 使用 soap4r 访问 SOAP 服务无法访问返回对象的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1653130/

相关文章:

java - 具有两个元素的 SOAP 主体

xml - 在架构内定义不同的命名空间 (WSDL)

java - 为什么 XSD 模式和 WSDL 模式之间存在差异?

java - SOAP 客户端真的需要使用 WSDL 吗?

ruby - IntelliJ + Ruby 插件与 RubyMine 之间的最大区别是什么?

ruby-on-rails - Rails 控制台错误需要 './example_user'

soap - 为 Paypal SetExpressCheckout 指定 CallbackURL 时无法识别 FlatRateShippingOptions

ruby-on-rails - 当以不同的方式编写时,范围解析在 ruby​​ 中的工作方式不同

jquery - Rack 抛出 EOFError(错误的内容正文)

javascript - 结果类名称未出现在 SOAP 结果中