From 2186eed1de3efd0e5d084c72f761e586e789ee46 Mon Sep 17 00:00:00 2001 From: Asa Venton Date: Mon, 21 Oct 2019 19:29:07 +0100 Subject: Catch cURL failures and output to user, return error when entering invalid 4 digit stop code --- livetimes.sh | 69 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/livetimes.sh b/livetimes.sh index 6c8ee17..8c783a6 100755 --- a/livetimes.sh +++ b/livetimes.sh @@ -9,42 +9,51 @@ _jq() { } _liveresult() { - livetimes=$(curl --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop}) - outtypes=(destination service_number eta) - for type in ${outtypes[@]}; do - case $type in - - destination) - result="$result Dest: " - ;; - - service_number) - result="$result | Routes:" - ;; - - eta) - result="$result | ETAs: " - ;; - - *) - ;; - - esac - for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do - result="$result $(_jq '.'$type''), " + livetimes=$(curl --fail --silent https://admin.libertybus.je/api/v1/soj/stop_updates/${stop}) + jq '.[]|=sort_by(.service_number)' > sorted.json + exitcode=$? + livetimeslen=$(echo ${livetimes} | jq '. | length') + if [ $exitcode != 0 ]; then + echo "cURL error - exiting." + exit 1 + elif [ $livetimeslen = 0 ]; then + echo "No results found." + exit 1 + else + outtypes=(destination service_number eta) + for type in ${outtypes[@]}; do + case $type in + destination) + result="$result Dest:" + ;; + service_number) + result="$result | Routes:" + ;; + eta) + result="$result | ETAs: " + ;; + *) + ;; + esac + for json in $(echo "${livetimes}" | jq -r '.[] | @base64'); do + result="$result $(_jq '.'$type''), " + done + result=$(echo $result | sed 's/,$//') done - result=$(echo $result | sed 's/,$//') - done - - echo -e $result - exit 0 + echo -e $result + exit 0 + fi } _stopsreturn() { - stops=$(curl --silent https://admin.libertybus.je/api/v1/stops/"${stop}" | jq -r '.') + stops=$(curl --fail --silent https://admin.libertybus.je/api/v1/stops/"${stop}") + exitcode=$? stopslen=$(echo ${stops} | jq '. | length') # if length of $stops array is 1 then only one bus stop matches and we can return live times for that stop - if [ $stopslen = 1 ]; then + if [ $exitcode != 0 ]; then + echo "cURL error - exiting." + exit 1 + elif [ $stopslen = 1 ]; then stop=$(echo ${stops} | jq '.[].stop_id') _liveresult elif [ $stopslen = 0 ]; then -- cgit v1.2.3