Hi
I want to show product prices in many currencies
Is there a way to import Exchange rates automatically?
Thanks
Erez
| SitePoint Sponsor |
Hi
I want to show product prices in many currencies
Is there a way to import Exchange rates automatically?
Thanks
Erez
Its not a matter of importing the exchange rates, but getting the data.
First you must find that data, then you can use a Rake task to update your database with the latest exchange rates every day or at a set interval.
So you need some place like xe.com, but with an API of some sort....



Have a search for RubyFinance
There are a couple of similar plug-ins that use Yahoo finance api to dynamically lookup exchange rate and other financial information, such as stock rates.
Thanks for your replies
I do have the data however I dont know how to use it
This is where I get the Dollar rate:
http://www.bankisrael.gov.il/heb.she...cy.php?curr=01
How do I import this data automatically in to my application (ones a day) ?
Thanks in advance for your help
Erez



This is how I do it.
- First create a method in the model that will do the update.
- Then create a short Ruby script that will run that method.
- Then I schedule a task (I run Rails on Windows) to fire off the script every night. On Linux you would do the same using a cron job.
So say I have a model ExchangeRate. I create a method to update them called self.update_rates_from_external_source. The self. make the method a Class method rather than an instance method. Then I'd write a script something like this:
I'd save that in a file called exchange_rate_update.rb and save it into the root of my Rails app. It can then be run as a standalone app (using ruby exchange_rate_update.rb on a Windows system).Code:ENV['RAILS_ENV'] = 'production' require 'config/environment.rb' ExchangeRate.update_rates_from_external_source
Note, this example is for a Rails app. The first two lines tell the system to run in production mode and by including environment.rb, it loads up all the classes available from within the Rails instance. If you are not using Rails, you'll need to replace this code with a require statement that will load the ExchangeRate class.



BTW, you may be interested in this code:
I've been playing with this sort of thing recently and I found the simplest solution came from a post in ruby-forum.com. It seems to work a treat!Code:require 'soap/wsdlDriver' WSDL_URL = "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL" soap = SOAP::WSDLDriverFactory.new(WSDL_URL).create_rpc_driver result = soap.ConversionRate(:FromCurrency => "USD", :ToCurrency => "GBP") puts result.conversionRateResult
Plenty of ways to do it using open-uri, net/http, and others. Here's one using open-uri and Hpricot.
Code:require 'rubygems' require 'open-uri' require 'hpricot' doc = Hpricot.XML(open('http://www.bankisrael.gov.il/heb.shearim/currency.php?curr=01')) rate = (doc/:RATE).first puts rate.inner_text
Bookmarks