Ruby currency exchange gems that work?

2020-07-11 08:58发布

问题:

This is based on an earlier question that was resolved. I need to load sale prices for my ruby-based app in different currencies. I was recently using the gem google_currency to convert the prices based on the Google API. At some point recently it stopped working and I have no idea why. I have tried testing in various ways but can't work out what the problem is.

I am now trying to use the 'exchange' gem which has good documentation however the method I am using is not producing anything in the view files when running.

According to the exchange gem the simple conversion should be something like:

def exchange4
   puts 10.in(:eur).to(:usd)

end

However it is not loading anything in the html view. Any suggestions including other working gems welcome!


Currently this code seems like it would pass however now Action Controller is telling me it doesn't know the conversion rates:

def exchange4(goods)
    require 'money'
    require 'money-rails'

   exr = Money.new(1, goods.currency).exchange_to(buyer.currency)
    puts exr
end

The error Action Controller is giving is:

No conversion rate known for 'GBP' -> 'EUR'

Very strange..

回答1:

RubyMoney organization has a very good options to deal with currencies, money and exchange. I use money and it really works. For Rails integration they have money-rails.

Examples of exchange:

  • Money.us_dollar(100).exchange_to('EUR')

  • Money.new(100, 'USD').exchange_to('EUR')

You can use eu_central_bank gem (compatible with money) to extract all exchange rates. Example usage (in rails console):

>> bank = EuCentralBank.new
>> bank.update_rates # if bank.last_updated.blank? || bank.last_updated < 1.day.ago
>> Money.default_bank = bank    

Then:

>> Money.new(1, 'GBP').exchange_to('EUR')
=> #<Money fractional:1 currency:EUR>