SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is there a way to import Exchange rates automatically?

    Hi
    I want to show product prices in many currencies
    Is there a way to import Exchange rates automatically?
    Thanks
    Erez

  2. #2
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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....

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by erezbens View Post
    How do I import this data automatically in to my application (ones a day) ?
    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:
    Code:
    ENV['RAILS_ENV'] = 'production'
    require 'config/environment.rb'
    
    ExchangeRate.update_rates_from_external_source
    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).

    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.

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW, you may be interested in this code:
    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
    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!

  8. #8
    SitePoint Member
    Join Date
    May 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    BTW, you may be interested in this code:
    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
    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!
    Nice. I'll have to hold onto this. Thanks!

  9. #9
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    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
    3. Create a rate in the rates table:
    id = 1
    name = dollar
    rate = 0
    and the date

    4. This is the method in the model:
    rate.rb
    Code:
    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
    I hade to updat 'hpricot' version
    Code:
    gem instal hpricot
    5.and the last thing is the file exchange_rate_update.rb
    Code:
    #ENV['RAILS_ENV'] = 'production'
    require 'config/environment.rb'
    
    Rate.update_rates_from_external_source
    I hade to coment out the first line becouse I am in the development stage

    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"><&#37;=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

  10. #10
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you add this to application_helper.rb

    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
    I think you'll be able to simplify your output code to:
    Code:
    <div class="price"><&#37;=h number_to_shekels(@asset.price_shekel) %></div>
    <div class="price"><%=h shekels_to_dollars(@asset.price_shekel) %></div>

  11. #11
    SitePoint Member
    Join Date
    Mar 2008
    Location
    Jerusalem
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    <% if Rate.find(1).updated_at.strftime("%d/%m/%Y") != Time.now.strftime("%d/%m/%Y") %>
    <% Rate.update_rates_from_external_source %>
    <% end %>
    That way The first user to enter the web site will update the dollar rate,
    and it will happen ones a day.

    Thanks

    Erez

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •