Rails currency exchange gem that works?

大兔子大兔子 提交于 2019-12-07 07:23:45

问题


Just wondering if anyone has used a currency exchange ruby gem that gets exchange rates from an external api and actually works. I have tried the following gems but have not been able to get any of them to function as described:

goog_currency, yahoo_currency, google_currency, yahoo_finance_currency

One of these uses deprecated code and hence does not work correctly.

I am just trying to display a currency exchange rate in a rails view which has been grabbed from an external api.

I have also considered trying to parse the json from yahoo/google finance urls but this seems much harder to do than it should.


回答1:


I picked google_currency from your list as most popular and it has been recently maintained. It worked fine for me, I ran the code from the synopsis without a problem.

If you saw the message:

You are using an old or stdlib version of json gem
Please upgrade to the recent version by adding this to your Gemfile:

  gem 'json', '~> 1.7.7'

Then you may need to update your copy of multi_json to avoid the json deprecation warnings:

gem install json
gem install multi_json

Which should (at the time of writing) install json 1.8.0 and multi_json 1.7.8. Or of course, if this is for a project, you should be able to pick these versions in your Gemfile, provided there is no conflict with other libraries in your project. The message may of caused you concern, but it is not a problem with google_currency.

I didn't notice any conflict or problems with the google_currency gem, using either the deprecated JSON modules or the most recent ones. To do something similar to what you want:

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

bank = Money::Bank::GoogleCurrency.new
bank.get_rate(:GBP, :USD).to_f
 => 1.5513


来源:https://stackoverflow.com/questions/18162830/rails-currency-exchange-gem-that-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!