4

I tried to get the exchange rate like this in my Rails app :

1.to_money.exchange_to('USD')

(Note : I have set the default currency to my local currency which is lkr). This returns 0. Im using the money-rails gem.

THpubs
  • 7,804
  • 16
  • 68
  • 143
  • isn't 1 lkr == .0074 USD? Are you expecting 1 cent? – ForgetfulFellow Jun 08 '15 at 06:59
  • @ForgetfulFellow Yes it's .0074. That's exactly what I need. Somehow it's droping the last two digits... I need to add this to a hidden field so I can use this rate using javascript to calculate the USD amount real time. – THpubs Jun 08 '15 at 07:00

3 Answers3

3

I found the solution on the CurrencyFreaks API. You can also change the 'base' currency and get the response for the specific currencies. For base=SEK, you can get the SEK to USD exchange rate thru this code:

require "uri"
require "net/http"

url = URI("https://api.currencyfreaks.com/latest
    ?apikey=YOUR_APIKEY
    &base=SEK
    &symbols=USD")

https = Net::HTTP.new(url.host, url.port);
https.use_ssl = true

request = Net::HTTP::Get.new(url)

response = https.request(request)
puts response.read_body

I hope, it will work for you too.

Usman Liaqat
  • 114
  • 9
Rameez
  • 407
  • 3
  • 11
1

have you tried this:

bank.get_rate(:LKR, :USD).to_f
Community
  • 1
  • 1
aurelius
  • 3,946
  • 7
  • 40
  • 73
1

The answer can be found from the link @aurelius have posted. Without using exchange_to, there's a direct way to get just the exchange rate. Since I use google currency gem and have configured it as the default_bank in the initializer, I can get the rate like this :

MoneyRails.default_bank.get_rate(:LKR, :USD)
THpubs
  • 7,804
  • 16
  • 68
  • 143