Currency conversion webservice in a rails app?


#1

Hi,

I’m interested in using a currency conversion web service in my rails
app. Does anyone have a free service that they use and like? I found
this one

http://www.webservicex.net/WS/WSDetails.aspx?CATID=2&WSID=10

I’ve tried to make a simple, plain Ruby script to connect to this and
get a conversion rate without success.

How do I integrate a webservice into my rails app? I have looked in
the Agile Rails book but can only see a tiny bit about this at the end
of the web services chapter.

All help greatly appreciated.

Thanks,
Peter


#2

Hi,

I’m interested in using a currency conversion web service in my rails app.

I’m struggling trying to figure out how to use a webservice and due to
the small amount of documention I’ve found I stressing that I won’t
get it working. I have tried the following but I get “undefined method
`web_client_api’ for #StoreController:0x26a37f8”. I looked in my
environment config files and the actionwebservice package is not being
skipped as far as I can tell. (In fact I’ve used it to create a web
service server so it must be working.)

I have no real idea how to set these few lines of code up and would
really appreciate some pointers.

Feeling a little let down by page 438 in the Agile Rails book,
Peter

in my controller

def conversion
#con = ActionWebService::Client::Soap.new(WhatGoesHere?,
http://www.webserviceX.NET/ConversionRate”)
#web_client_api :con, :soap,
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
web_client_api :con, :soap,
http://www.webserviceX.NET/ConversionRate

con.ConversionRate("CND", "USD")
@rate = con

end


#3

ActionWebService::Client::Soap has a special purpose in sense of that
you
should use it only for calling your own services with defined
ActionWebService::API. In order to call third party web services you’re
better of using soap4r API directly.

Kent.


#4

If you are using ActionWebService then it should be automatically
included.
Otherwise, my best bet would be config/environment.rb


#5

Would you happen to have a simple example you could cut and paste for
me? I looked in my Pickaxe book and don’t see soap4r in the index. I
can see the SOAP library in the book,

Thanks,
Peter


#6

Hi Kent,

Thanks for the reply.

ActionWebService::Client::Soap has a special purpose in sense of that you
should use it only for calling your own services with defined
ActionWebService::API.

OK

In order to call third party web services you’re
better of using soap4r API directly.

I will look into using the Ruby soap4r library. If I want to use it
from inside rails do I have to include it somewhere? Where do I do
this?

Thanks,
Peter


#7

Hi Peter

here’s one way of doing this :

service =
SOAP::RPC::Driver.new("http://www.webservicex.net/CurrencyConvertor.asmx","http://www.webserviceX.NET/
http://www.webservicex.net/CurrencyConvertor.asmx","http://www.webserviceX.NET/
")
service.default_encodingstyle =
SOAP::EncodingStyle::ASPDotNetHandler::Namespace
service.add_method_with_soapaction(“ConversionRate”,
http://www.webserviceX.NET/ConversionRatehttp://www.webservicex.net/ConversionRate”,
“FromCurrency”, “ToCurrency”)

rate = service.ConversionRate(“EUR”,“GBP”)
cheers

Thibaut

[blog] http://www.dotnetguru2.org/tbarrere


#8

Hi Thibaut,

Thanks for the code! I tried it and no matter which currencies
combination I try I get a rate of zero. Did you try this and get a
different result? It maybe that the web service doesn’t work but I
hope not.

Thanks,
Peter


#9

you may also want to have a look at this page (which I just discovered)

http://wiki.rubyonrails.org/rails/pages/How+To+Consume+.NET+WebServices/

HTH

Thibaut


#10

Hi Peter

I had this zero rate a some point, can’t remember exactly under which
circumstances. I finally managed to get the good conversion rate - I’ll
check my code back at home.

Three ideas until then:

  • try the code in IRB to see what’s actually returned
  • try adding service.wiredump_dev = STDERR to dump the communication (on
    the
    webservice webpage you’ll see the expected input and output)
  • try ConversionRate(xxxx).to_f

Thibaut


#11

I just wanted to mention that the beta book “Enterprise Integration with
Ruby” by Maik Schmidt (available at the Pragmatic Programmers web site,
as
usual) has an extensive section on processing XML files. It also
discusses
soap4r, REXML, and any other way of creating or consuming web services
that
you can think of.

Like most of their Beta Books, it’s awfully close to being a fully
polished
product. In short, that book rocks.

Ken


Kenneth A. Kousen, Ph.D.
President
Kousen IT, Inc.
http://www.kousenit.com
removed_email_address@domain.invalid


#12

It works for me. What version of soap4r are you using?

$ ruby -rsoap/soap -e ‘puts SOAP::Version’
1.5.5


#13

Hi Kent,

It works? You got a number other than zero?

$ ruby -rsoap/soap -e ‘puts SOAP::Version’
1.5.3-ruby1.8.2

Does this mean I need an upgrade? It would be strange since the Ruby
runs without error. It is the SOAP response that has the zero. I
realize it must be something wrong on my end. Maybe I have a typo. Did
you cut and paste the code?

What is soap4r? Is it just a catchy phrase meaning SOAP in the Ruby
environment? Or is it a library?

Thanks,
Peter


#14

Hi Thibaut,

I’ve been trying to get this working. No luck yet. With wiredump it
looks like the webservice really is returning zero. Any other ideas?

Thanks,
Peter


#15

Hi Kent,

I upgraded from Ruby 1.8.2 to 1.8.4 and now I have SOAP 1.5.5. And the
example works! Thanks.

Thanks to Thibaut also.

This is a major mystery and stress I hope is now over.

Thanks again,
Peter