我不熟悉使用Ruby进行OOP编程,并使用Savon gem向SOAP XML Webservice / API发出请求。 当使用更简化的非面向对象方法时,我能够成功地看到响应,但我想重构我的代码并确保它是可扩展的并且稍后在Rails中工作。我确定它很简单,但我不知道我哪里出错了!请帮忙,非常感谢!
以下是我的ruby文件reserve.rb
:
require 'savon'
class Reservation
attr_accessor :first_name
def initialize(options={})
@first_name = options[:first_name]
end
def client
client = Savon.client(wsdl: "http://some_wdsl_url_link", adapter: :curb, follow_redirects: :follow_redirects)
end
def first_name(confirm_number, email)
message = { confirmation_number: confirm_number, email: email }
response = client.call(:search_for_reservation, message: message)
response.body[:search_for_reservation_response][:reservation][:first_name]
rescue Savon::SOAPFault => error
fault_code = error.to_hash[:fault][:faultcode]
raise CustomError, fault_code
end
end
reserve = Reservation.new
puts reserve.first_name("NTSOTHJN", "otherjamesgray@gmail.com")
当我运行ruby reserve.rb
时,这是终端中的输出:
reserve.rb:20:in `rescue in first_name': uninitialized constant Reservation::CustomError (NameError)
from reserve.rb:15:in `first_name'
from reserve.rb:25:in `<main>'