Ruby code examples
The following code examples demonstrate the Ruby interaction with the API.
All examples also expect your
API key.
Example 1
Get the exchange rate between two currencies
require 'open-uri'
require 'json'
stream = open(
"http://jsonrates.com/get/?"+
"from=USD"+
"&to=EUR"
)
data = stream.read
json = JSON.parse(data)
rate = json['rate'].to_f
Example 2
Convert an amount from one into another currency
require 'open-uri'
require 'json'
stream = open(
"http://jsonrates.com/convert/?"+
"from=USD"+
"&to=EUR"+
"&amount=2.99"
)
data = stream.read
json = JSON.parse(data)
amount = json['amount'].to_f
Example 3
Get historical exchange rates between two currencies for three days
require 'open-uri'
require 'json'
stream = open(
"http://jsonrates.com/historical/?"+
"from=USD"+
"&to=EUR"+
"&dateStart=2014-06-25"+
"&dateEnd=2014-06-27"
)
data = stream.read
json = JSON.parse(data)
json["rates"].each do |date, values|
histrate = values['rate'].to_f
end