Last active
October 19, 2022 21:30
-
-
Save jheitzeb/e32910af2f2715dd717224886cfce78e to your computer and use it in GitHub Desktop.
USD's Buying Power vs. 1 Year Ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby code to print the US dollar relative buying power vs 1 year ago at various popular travel destinations | |
CURRENCY_API_KEY = "get one for free at https://apilayer.com/marketplace/exchangerates_data-api?live_demo=show" | |
def self.currency_report | |
# using a public exchange rate API, get the historic rate vs. today's and compute the % change. | |
# List currencies of popular travel destinations | |
currencies = [ | |
{code: "JPY", name: "Japanese Yen"}, | |
{code: "EUR", name: "Euro"}, | |
{code: "MXN", name: "Mexican Peso"}, | |
{code: "CAD", name: "Canadian Dollar"}, | |
{code: "GBP", name: "British Pound"}, | |
{code: "AUD", name: "Australian Dollar"}, | |
{code: "NZD", name: "New Zealand Dollar"}, | |
{code: "BRL", name: "Brazilian Real"}, | |
{code: "ZAR", name: "South African Rand"}, | |
{code: "KRW", name: "South Korean Won"}, | |
{code: "THB", name: "Thai Baht"}, | |
{code: "VND", name: "Vietnamese Dollar"}, | |
{code: "TRY", name: "Turkish Lira"}, | |
{code: "SAR", name: "Saudi Riyal"}, | |
{code: "EGP", name: "Egyptian Pound"}, | |
{code: "ILS", name: "Israeli Shekel"}, | |
{code: "ARS", name: "Argentine Peso"}, | |
{code: "SEK", name: "Swedish Krona"}, | |
{code: "HKD", name: "Hong Kong Dollar"}, | |
{code: "SGD", name: "Singapore Dollar"}, | |
{code: "ISK", name: "Icelandic Krona"}, | |
] | |
changes = [] | |
currencies.each do |currency| | |
# get the exchange rate for today | |
today = get_exchange_rate(currency[:code]).to_f | |
# get the exchange rate for 1 year ago | |
year_ago = get_exchange_rate(currency[:code], 1.year.ago).to_f | |
# To purchase a year ago's worth of currency, how much would you have to pay today as a % discount? | |
# 1 year ago, $1 bought 114.5 yen | |
# today, $1 buys 149.8 | |
# therefore, to buy 114.5 yen today, you only need to pay 114.5/149.8 = $0.76 | |
needed_today = year_ago / today | |
# the % discount of needed_today vs 1.0 is the % change: | |
change = (1.0 - needed_today) * 100 | |
# format like %: | |
change_str = change.round(1).to_s + "%" | |
changes << {name: currency[:name], change: change, change_str: change_str} | |
end | |
# sort by changes[:change] desc: | |
changes.sort_by! {|c| c[:change]}.reverse! | |
# print report | |
changes.each do |change| | |
discount_or_premium = change[:change] > 0 ? "discount" : "premium" | |
puts "The #{change[:name]} feels like a #{change[:change_str]} #{discount_or_premium} vs. a year ago.".white.bold | |
end | |
end | |
def self.get_exchange_rate(currency_code, time_ago = Time.current) | |
api_key = CURRENCY_API_KEY | |
# format time_ago as yyyy-mm-dd | |
date_str = time_ago.strftime("%Y-%m-%d") | |
url = URI("https://api.apilayer.com/exchangerates_data/#{date_str}?symbols=#{currency_code}&base=USD") | |
https = Net::HTTP.new(url.host, url.port); | |
https.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request['apikey'] = api_key | |
response = https.request(request) | |
data = JSON.parse(response.read_body) | |
return data["rates"][currency_code] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a good time for Americans to travel overseas!