diff options
author | Asa Venton <asav1410@gmail.com> | 2019-10-21 19:29:07 +0100 |
---|---|---|
committer | Asa Venton <asav1410@gmail.com> | 2019-10-21 19:29:07 +0100 |
commit | 2186eed1de3efd0e5d084c72f761e586e789ee46 (patch) | |
tree | 61b5aa5724257fa1650b4a1e3488a35df56ffb6b /livetimes.sh | |
parent | 3623e41ebfc8bc6dbdae4c17c5270c660e20598f (diff) |
Catch cURL failures and output to user, return error when entering invalid 4 digit stop code
Diffstat (limited to 'livetimes.sh')
-rwxr-xr-x | livetimes.sh | 69 |
1 files 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 |