THpubs
THpubs

Reputation: 8172

How to get the exchange currency exchange rate in rails?

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.

Upvotes: 4

Views: 2885

Answers (3)

Rameez
Rameez

Reputation: 477

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.

Upvotes: 3

aurelius
aurelius

Reputation: 4076

have you tried this:

bank.get_rate(:LKR, :USD).to_f

Upvotes: 1

THpubs
THpubs

Reputation: 8172

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)

Upvotes: 1

Related Questions