Skip to content

Commit

Permalink
Update list of currency codes
Browse files Browse the repository at this point in the history
This commit will also create a Bash script to update that list in the
future.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed May 28, 2019
1 parent c519316 commit 45d109f
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 97 deletions.
40 changes: 40 additions & 0 deletions bin/update-currency-codes
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# Usage: {script} TLD_FILENAME
# Update list of TLD

set -euo pipefail

declare -r IFS=$'\n'
declare -r URL="https://www.currency-iso.org/dam/downloads/lists/list_one.xml"
declare -r RULE="${1}"
declare -r TEMPORARY_XML=$(mktemp)

download_list()
{
echo "Downloading list from '${URL}'"
curl --silent --location "${1}" --output "${2}"
}

update_currency_codes()
{
local -r filename_rule="${1}"
local -r filename_xml="${2}"
local -r number_of_items=$(grep "<CcyNtry>" "${filename_xml}" | wc --lines)
local -r temporary_rule=$(mktemp)

echo "Updating list in '${filename_rule}'"
{
sed -n '/^</,/ private/p' "${filename_rule}"
for index in $(seq 1 ${number_of_items}); do
local name=$(xml sel -t -v "//CcyNtry[${index}]/CcyNm" < "${filename_xml}")
local code=$(xml sel -t -v "//CcyNtry[${index}]/Ccy" < "${filename_xml}")
echo " '${code}', // ${name}"
done
sed -n '/^ ]/,/^}/p' "${filename_rule}"
} > "${temporary_rule}"

mv "${temporary_rule}" "${filename_rule}"
}

download_list "${URL}" "${TEMPORARY_XML}"
update_currency_codes "${RULE}" "${TEMPORARY_XML}"
Loading

0 comments on commit 45d109f

Please sign in to comment.