Skip to content

Instantly share code, notes, and snippets.

@adamamyl
Last active September 18, 2018 19:34
Show Gist options
  • Save adamamyl/8be06a7f7c6a6d39b99daeeec9bd5ac1 to your computer and use it in GitHub Desktop.
Save adamamyl/8be06a7f7c6a6d39b99daeeec9bd5ac1 to your computer and use it in GitHub Desktop.
A dirty bash script to check the levels of a ledger with watermark; see the help guide on guides.railsbank.com for more context.
#! /usr/local/bin/bash
set -e
# set this to be your ledger's ledger_id
floatledgerid=some-ledger-uuid
# this is perhaps a little more than needed,
# but we use Keychain Access for our API Keys — it
# has a simple interface that works in scripts
if [[ "$OSTYPE" == "darwin"* ]]; then
# assumes our API key is stored in Mac's keychain
export apikey=$(security find-generic-password -a ${USER} -s livekeys-for-railsbank -w) || echo "Can't find that"; exit 111
elif [ ! -e ${HOME}/livekeyfile ]; then
echo "Can't find an API Key file (${HOME}/livekeyfile)!!"
echo "Bye!"
exit 222
else
export apikeystring=$(cat ~/playliveapikey)
export apikey="API-Key $apikeystring"
fi
# now we have an API key we can do something
# first we assign our responses some variable names
# and then hit curl parsing the response via jq (rather messily)
IFS=',' read -r ledgerid iban amount wmark < <(curl --silent -X \
GET "https://live.railsbank.com/v1/customer/ledgers/${floatledgerid}" \
-H "accept: application/json" \
-H "Authorization: ${apikey}" | \
jq '[.ledger_id, .iban, .amount, .ledger_meta.watermark] | @csv' --raw-output | \
sed 's/"//g')
# in reviewing the script, you might find this line below useful?
# echo "Ledger ${ledgerid} (IBAN: ${iban}) has a \n balance of ${amount}. The watermark is ${wmark}"
# and here comes the maths
if (( $(echo "$amount > $wmark" | bc -l) )); then
# echo "Watermark not reached"
exit 0
else
echo "Watermark is reached. Fund the ledger"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment