36 lines
692 B
Ruby
36 lines
692 B
Ruby
#INICIO PARTE OPCIONAL / DESAFIO (!)
|
|
require 'uri'
|
|
require 'net/http'
|
|
require 'json'
|
|
|
|
EURUSD = 1.0767
|
|
RATES = "https://open.er-api.com/v6/latest/EUR"
|
|
|
|
def get_rate
|
|
uri = URI(RATES)
|
|
res = Net::HTTP.gete_response(uri)
|
|
|
|
if (res.is_a?(Net::HTTPSuccess))
|
|
parsed_body = JSON.parse(res.body)
|
|
return parsed_body["rates"]["USD"]
|
|
end
|
|
end
|
|
|
|
|
|
# FIM PARTE OPCIONAL / DESAFIO (!)
|
|
|
|
def to_usd(amount)
|
|
amount * EURUSD
|
|
end
|
|
|
|
|
|
puts("Enter the amount the convert from €uros: ")
|
|
value = gets.chomp.to_f
|
|
puts("Enter the exchange rate for the $U.S. Dollar: ")
|
|
rate_usd = gets.chomp.to_f
|
|
|
|
# eur to usd:
|
|
|
|
puts("#{value} euros at an exchange of #{rate_usd} is #{to_usd(value).round(2)} dollars.")
|
|
|