SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

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

  6. #6
    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!

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


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
  •