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
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



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
Thank you very much,
I have implemented this and it works great
1. I created a model "Rate"
2. This is the migration file:
db/migrate/007_create_rates.rb
3. Create a rate in the rates table:Code:class CreateRates < ActiveRecord::Migration def self.up create_table :rates do |t| t.column :name, :string t.column :rate, :integer, :default => 0 t.column :updated_at, :datetime end end def self.down drop_table :rates end end
id = 1
name = dollar
rate = 0
and the date
4. This is the method in the model:
rate.rb
I hade to updat 'hpricot' versionCode:require 'rubygems' require 'open-uri' require 'hpricot' class Rate < ActiveRecord::Base def self.update_rates_from_external_source doc = Hpricot.XML(open('http://www.bankisrael.gov.il/heb.shearim/currency.php?curr=01')) rate = (doc/:RATE).first @dolllar = rate.inner_text s = Rate.find(1) s.update_attribute :rate, @dolllar.to_f end end
5.and the last thing is the file exchange_rate_update.rbCode:gem instal hpricot
I hade to coment out the first line becouse I am in the development stageCode:#ENV['RAILS_ENV'] = 'production' require 'config/environment.rb' Rate.update_rates_from_external_source
When you run this file the dollar rate is updated in the rats table and is available to the app
This is what i do in the view
Code:<div class="price"><%=h number_to_currency(@asset.price_shekel, :unit => "₪") %></div> <div class="price"><%=h number_to_currency(@asset.price_shekel.to_f / @dollar.to_f) %></div>
Thank you for your help![]()
Erez



If you add this to application_helper.rb
I think you'll be able to simplify your output code to:Code:#Converts a number to amount in shekels. def number_to_shekels (number, options = {}) options[:unit] = "₪" unless options[:unit] number_to_currency(number, options) if number end #Converts a shekels amount to a dollar amount and outputs as a currency. def shekels_to_dollars (amount_in_shekels, options = {}) if amount_in_shekels amount_in_dollars = amount_in_shekels.to_f / @dollar.to_f return number_to_currency(amount_in_dollars, options) end end
Code:<div class="price"><%=h number_to_shekels(@asset.price_shekel) %></div> <div class="price"><%=h shekels_to_dollars(@asset.price_shekel) %></div>
I think I have found a very simple way to update the dollar rate with out the need to use windows and exchange_rate_update.rb
This goes in application.rhtml
That way The first user to enter the web site will update the dollar rate,Code:<% if Rate.find(1).updated_at.strftime("%d/%m/%Y") != Time.now.strftime("%d/%m/%Y") %> <% Rate.update_rates_from_external_source %> <% end %>
and it will happen ones a day.
Thanks
Erez



I think that code belongs somewhere else. Views are not the right place to be processing data retrieval.
It would be better to run that test in a Controller or Application.rb
Personally, I'd still do the update as a nightly scheduled task as I think that first user is going to get quite a delay while the rates get updated. But your way is an alternative that would work.
Bookmarks