From 8ce9df1ec89e62c50233f7e6e1266af436758689 Mon Sep 17 00:00:00 2001 From: ronreg-ribdev <56719257+ronreg-ribdev@users.noreply.github.com> Date: Mon, 1 Jun 2020 01:07:07 -0700 Subject: [PATCH] Abstract away a handler --- urbit/app/bartinfo.hoon | 56 +++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/urbit/app/bartinfo.hoon b/urbit/app/bartinfo.hoon index ad6ecf4..bc4fe03 100644 --- a/urbit/app/bartinfo.hoon +++ b/urbit/app/bartinfo.hoon @@ -131,6 +131,19 @@ :: get .root | .stations | .station for list of stations ++ bart-api-key "Q5RQ-PUEB-999T-DWEI" ++ bart-api-url-base "http://api.bart.gov/api" +++ with-json-handler + |= [response=client-response:iris jsonhandler=$-(json json)] + ^- json + =, format + ?. ?=(%finished -.response) + %- pairs:enjs [fulltext+s+'bart response error' ~] + =/ data=(unit mime-data:iris) full-file.response + ?~ data %- pairs:enjs ~ + =/ ujon=(unit json) (de-json:html q.data.u.data) + ?~ ujon %- pairs:enjs ~ + ?> ?=(%o -.u.ujon) + =/ parsed-json=json u.ujon + (jsonhandler parsed-json) ++ bart-api-request-stations ^- request:http =/ url (crip "{bart-api-url-base}/stn.aspx?cmd=stns&key={bart-api-key}&json=y") @@ -141,30 +154,25 @@ |= response=client-response:iris ^- json =, format - ?. ?=(%finished -.response) - %- pairs:enjs [fulltext+s+'bart response error' ~] - =/ data=(unit mime-data:iris) full-file.response - ?~ data %- pairs:enjs ~ - =/ ujon=(unit json) (de-json:html q.data.u.data) - ?~ ujon %- pairs:enjs ~ - ?> ?=(%o -.u.ujon) - =/ parsed-json u.ujon - =/ root=json (~(got by p.parsed-json) 'root') - ?> ?=(%o -.root) - =/ stations (~(got by p.root) 'stations') - ?> ?=(%o -.stations) - =/ station=json (~(got by p.stations) 'station') - ?> ?=(%a -.station) - =/ inner p.station - =/ abbr-and-name %- turn :- inner |= item=json - ^- json - ?> ?=(%o -.item) - =/ name (~(got by p.item) 'name') - ?> ?=(%s -.name) - =/ abbr (~(got by p.item) 'abbr') - ?> ?=(%s -.abbr) - (pairs:enjs [name+s+p.name abbr+s+p.abbr ~]) - (pairs:enjs [[%stations %a abbr-and-name] ~]) + =/ handler |= parsed-json=json + ?> ?=(%o -.parsed-json) + =/ root=json (~(got by p.parsed-json) 'root') + ?> ?=(%o -.root) + =/ stations (~(got by p.root) 'stations') + ?> ?=(%o -.stations) + =/ station=json (~(got by p.stations) 'station') + ?> ?=(%a -.station) + =/ inner p.station + =/ abbr-and-name %- turn :- inner |= item=json + ^- json + ?> ?=(%o -.item) + =/ name (~(got by p.item) 'name') + ?> ?=(%s -.name) + =/ abbr (~(got by p.item) 'abbr') + ?> ?=(%s -.abbr) + (pairs:enjs [name+s+p.name abbr+s+p.abbr ~]) + (pairs:enjs [[%stations %a abbr-and-name] ~]) + (with-json-handler response handler) :: ++ bart-api-elevator-status ^- request:http