4

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.

user2514224
  • 187
  • 2
  • 14

1 Answers1

6

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
Neil Slater
  • 26,512
  • 6
  • 76
  • 94
  • Thanks a bunch, that actually worked, although the documentation for the gem doesn't actually explain your method as the primary exchange method and instead gives you: n = 1.to_money(:USD) n.exchange_to(:EUR). I also managed to use the simple_currency gem. – user2514224 Aug 11 '13 at 20:27
  • @user2514224: Glad to help. The documentation is quite light for this gem, but you can often find your way around in `irb` and checking what's available on any object with `Thing.methods.sort - Object.methods` (where Thing can be any class name or variable referencing an object, and you want to know what it does) and a little guessing. – Neil Slater Aug 11 '13 at 20:35
  • Thanks for your original help! Please see my recent question - http://stackoverflow.com/questions/21918014/ruby-currency-exchange-gems-that-work – user2514224 Feb 20 '14 at 19:49
  • Hi i used same code but we are getting the error: "Money::Bank::GoogleCurrencyFetchError:" – koteshwarrao Ravulapally Apr 13 '18 at 06:45
  • @kotesh: You may need to ask a separate question about that. In case there was a problem (this answer is 5 years old) I just re-tried the code. Works just fine for me. Likely some issue on your machine. In your question you should show more of the stack trace - include stack trace from gems, most likely problems will be network request errors. – Neil Slater Apr 13 '18 at 07:39