Skip to content

Instantly share code, notes, and snippets.

@kalarani
Created June 10, 2012 09:30
Show Gist options
  • Save kalarani/2904632 to your computer and use it in GitHub Desktop.
Save kalarani/2904632 to your computer and use it in GitHub Desktop.
Savon Model and Thread safety
class Soap
extend Savon::Model
document "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL"
def thread_safe?
p run_in_threads [:inr_to_usd, :usd_to_inr]
sleep 5
p run_in_threads [:usd_to_inr, :inr_to_usd]
end
def inr_to_usd
convert 'INR', 'USD'
end
def usd_to_inr
convert 'USD', 'INR'
end
private
def convert from, to
response = client.request 'http://www.webserviceX.NET/ConversionRate' do
soap.input = ['ConversionRate', {'xmlns' => 'http://www.webserviceX.NET/'}]
soap.body = "<FromCurrency>#{from}</FromCurrency><ToCurrency>#{to}</ToCurrency>"
end
response[:conversion_rate_response][:conversion_rate_result]
end
def run_in_threads(method_names)
threads = method_names.map do |method_name|
Thread.new do
Thread.current[:value] = Soap.new.send(method_name)
end
end
sleep 10
threads.each(&:kill)
threads.collect{|th| th[:value]}.compact
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment