Search code examples
ruby-on-rails-4converterscurrencygoogle-finance

Google Finance currency converter issue in rails


I am working on Google currency converter but it's not working. It just redirects to:

http://finance.google.com:80/bctzjpnsun/converter?a=1&from=EUR&to=USD

or it's gives error 404 Not Found

This my code:

gem file:

gem 'money'
gem 'google_currency', '~> 3.4.1'

converter method

def self.exchange_to_USD annual_salary, currency
  begin
    mothly_salary = annual_salary / 12
    if currency.present?
      salary = Money.new(mothly_salary, currency).exchange_to(:USD).fractional
    else
      salary = mothly_salary  
    end
  rescue Money::Bank::UnknownRate => e
    salary = mothly_salary
  rescue Exception => e
    salary = mothly_salary
  end
  salary
end

application.rb file

require 'money'
require 'money/bank/google_currency'

# set the seconds after than the current rates are automatically expired
# by default, they never expire
Money::Bank::GoogleCurrency.ttl_in_seconds = 86400

# set default bank to instance of GoogleCurrency
Money.default_bank = Money::Bank::GoogleCurrency.new

Solution

  • Use eu_central_bank Instead.

    We ended up replacing google_currency with eu_central_bank and it was very easy and worked great. It's part of RubyMoney so it should be well maintained. Basic replacement looks like this:

    eu_central_bank = EuCentralBank.new
    
    eu_central_bank.update_rates # TODO: Make this use a cache in a shared directory and just update it every day.
    
    Money.default_bank = eu_central_bank
    

    Definitely a good option to consider.