我想创建一个Savon模型,但在初始化客户端时传递WSDL的URL。像这样:
class MyService
extend Savon::Model
client :wsdl self.url
def initialize(url)
self.url = url
end
end
知道如何实现这个目标吗?
答案 0 :(得分:1)
客户端和操作是类级方法(宏),因此您只需要在构造函数中使用self.class引用它们。
因此,这是一个工作动态MyService类的示例,它接受端点的url以及要执行的操作,以逗号分隔...
require 'savon' # I am assuming savon 2.4.0 here...
class MyService
extend Savon::Model
def initialize(url, *operations)
self.class.client wsdl: url
operations.each { |operation| self.class.operations operation }
end
end
url = "http://www.webservicex.net/stockquote.asmx?WSDL"
service = MyService.new(url, :get_quote)
response = service.get_quote(message: { symbol: "AAPL" })
puts response.body[:get_quote_response][:get_quote_result]
示例输出
<StockQuotes><Stock><Symbol>AAPL</Symbol><Last>592.98</Last><Date>5/2/2014</Date><Time>11:55am</Time> <Change>+1.50</Change><Open>592.89</Open><High>594.20</High><Low>589.71</Low><Volume>3406189</Volume><MktCap>510.8B</MktCap><PreviousClose>591.48</PreviousClose><PercentageChange>+0.25%</PercentageChange><AnnRange>388.87 - 599.43</AnnRange><Earns>41.727</Earns><P-E>14.17</P-E><Name>Apple Inc.</Name></Stock></StockQuotes>