How To Use Your Ruby Currency Converter to exit the country
Converting currencies by 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 code:
- Simple Currency Conversion (in Ruby):
To convert from 12.50 USD (US Dollar) to GBP (British Pound):
https://www.exchangerate-api.com/FROM_CURRENCY/TO_CURRENCY/AMOUNT?k=YOUR_API_KEY
require 'net/http'
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):
require 'net/http'
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:
- If an invalid amount is used (must be numeric floating point or integer numbers)
- If either of the 3 letter currency codes are invalid (Check our supported currencies)
- If an invalid key is used (Get an API key)
- If your monthly API query limit is reached (Click here for extended usage)
- If an unresolvable IP address is provided (This only applies for our "Automatic Country/Flag Information" query.)
2. Automatic Currency Detection/Conversion (in Ruby):
To convert from 12.50 USD (US Dollar) to your user's currency:
require 'net/http'
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:
require 'net/http'
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: $)
For more information the site below is a great way to become self-aware of your time. :D
Shout-out to @steemfest !!