How to use our Ruby Currency Converter
Converting currencies using our Ruby Exchange Rate API is as easy as posting a HTTP request. Your requests would need to follow
this simple pattern:
Here are some examples in Ruby:
1. Simple Currency Conversion (in Ruby):
To convert from 12.50 USD (US Dollar) to GBP (British Pound):
from_curr = "USD"
to_curr = "GBP"
amount = 12.50
host = "www.exchangerate-api.com"
http = Net::HTTP.new(host, 80)
url = "/"+from_curr+"/"+to_curr+"/"+amount+"?k=API_KEY"
response = http.get(url)
puts response.body
Or to get how many Japanese Yen (JPY) there are to the South African Rand (ZAR):
host = "www.exchangerate-api.com"
http = Net::HTTP.new(host, 80)
response = http.get("/zar/jpy/?k=API_KEY")
puts response.body
Error Codes:
The API returns the following error codes:-1 if an invalid amount is used (must be numeric floating point or integer numbers)
-2 if either of the 3 letter currency codes are invalid (Check our supported currencies)
-3 if an invalid key is used (Get an API key)
-4 if your monthly API query limit is reached (Click here for extended usage)
-5 if an unresolvable IP address is provided (This only applies for our "Automatic Country/Flag Information" query.)
We also offer two additional types of query:
2. Automatic Currency Detection/Conversion (in Ruby):
To convert from 12.50 USD (US Dollar) to your user's currency:
host = "www.exchangerate-api.com"
http = Net::HTTP.new(host, 80)
ip = request.env['REMOTE_ADDR']
url = "/auto/USD/"+ip+"/12.50?k=API_KEY"
response = http.get(url)
result = response.body
return_array = result.split('|')
converted_amount = return_array[0]
symbol_unicode = return_array[1]
currency_code = return_array[2]
puts converted_amount #FINAL CONVERTED AMOUNT
puts currency_code #FINAL 3-DIGIT CURRENCY CODE (EG: USD)
#If you want to display the currency's symbol in HTML:
symbol_html = ""
symbol_characters = symbol_unicode.split(',')
symbol_characters.each { |x|
symbol_html = symbol_html+""+x+";"
}
puts symbol_html #FINAL SYMBOL HTML (EG: $)
3. Automatic Country/Flag Information (in Ruby):
To retrieve your user's national flag and other country information:
host = "www.exchangerate-api.com"
http = Net::HTTP.new(host, 80)
ip = request.env['REMOTE_ADDR']
url = "/country_info/"+ip+"?k=API_KEY"
response = http.get(url)
result = response.body
return_array = result.split('|')
country_name = return_array[0]
country_code_2 = return_array[1]
country_code_3 = return_array[2]
currency_code = return_array[3]
symbol_unicode = return_array[4]
flag_url = return_array[5]
puts country_name #FINAL COUNTRY NAME
puts country_code_2 #FINAL 2-DIGIT COUNTRY CODE (EG: US)
puts country_code_3 #FINAL 3-DIGIT COUNTRY CODE (EG: USA)
puts currency_code #FINAL 3-DIGIT CURRENCY CODE (EG: USD)
#If you want to display the currency's symbol in HTML:
symbol_html = ""
symbol_characters = symbol_unicode.split(',')
symbol_characters.each { |x|
symbol_html = symbol_html+""+x+";"
}
puts symbol_html #FINAL SYMBOL HTML (EG: $)
#If you want to display an image of the flag in HTML:
flag_image = "<img src='"+flag_url+"' />"
puts flag_image #FINAL FLAG IMAGE
How Our API Works
Python
PHP
Ruby
Perl
ASP.NET
Java
Applescript
iPhone C