about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--rent.scm96
1 files changed, 49 insertions, 47 deletions
diff --git a/rent.scm b/rent.scm
index 062795b..64ec3e0 100644
--- a/rent.scm
+++ b/rent.scm
@@ -266,6 +266,13 @@ times and comparing them using @var{time-predicate}."
     (call-with-input-file cache-file
       get-bytevector-all)))
 
+(define* (json-get uri #:optional cache-live-time)
+  "Invoke @code{http-get*} on @var{uri} and process the response as
+JSON."
+  (call-with-port (open-bytevector-input-port
+                   (http-get* uri cache-live-time))
+    json->scm))
+
 (define (house-page-sxml house)
   "Return the HTML of the page for @var{house} in SXML form."
   (call-with-port (open-bytevector-input-port
@@ -351,58 +358,53 @@ characters. Else, return @code{#f}."
   "Compute the cycling distance between @var{source}, a point, and
 @var{destinations}, a list of points. The returned distance is a list
 of actual distances by cycle, and not distance as the crow flies."
-  (call-with-port (open-bytevector-input-port
-                   (http-get*
-                    ;; We use ~f for latitude/longitude since ~a
-                    ;; would use the exponential float notation for
-                    ;; small numbers and the API does not like
-                    ;; that.
-                    (format #f "~a/table/v1/bike/~a?annotations=distance&sources=0&destinations=~a"
-                            %osrm-base-url
-                            (string-join
-                             (map (match-lambda
-                                    (#(latitude longitude)
-                                     (format #f "~f,~f" longitude latitude)))
-                                  (cons source destinations))
-                             ";")
-                            (string-join
-                             (map number->string
-                                  (iota (length destinations) 1))
-                             ";"))))
-    (lambda (port)
-      (map (cut / <> 1000)
-           (vector->list (json-ref (json->scm port)
-                                   "distances" 0))))))
+  (map (cut / <> 1000)
+       (vector->list
+        (json-ref
+         (json-get
+          ;; We use ~f for latitude/longitude since ~a would use the
+          ;; exponential float notation for small numbers and the API does
+          ;; not like that.
+          (format #f "~a/table/v1/bike/~a?annotations=distance&sources=0&destinations=~a"
+                  %osrm-base-url
+                  (string-join
+                   (map (match-lambda
+                          (#(latitude longitude)
+                           (format #f "~f,~f" longitude latitude)))
+                        (cons source destinations))
+                   ";")
+                  (string-join
+                   (map number->string
+                        (iota (length destinations) 1))
+                   ";")))
+         "distances" 0))))
 
 (define (lines-at-station station-name)
   "Return a list of tube lines serving @var{station-name}."
   (let* ((modes (list "dlr" "elizabeth-line" "tube"))
          (station-id
-          (call-with-port (open-bytevector-input-port
-                           (http-get* (string-append %tfl-base-url
-                                                     "/StopPoint/Search/"
-                                                     (uri-encode station-name)
-                                                     "?modes="
-                                                     (string-join modes ","))))
-            (lambda (port)
-              ;; Assume the first search result is the station we are
-              ;; looking for.
-              (json-ref (json->scm port)
-                        "matches" 0 "id")))))
-    (call-with-port (open-bytevector-input-port
-                     (http-get* (string-append %tfl-base-url
-                                               "/StopPoint/" station-id)))
-      (lambda (port)
-        (vector->list
-         ;; Assume that we actually find tube lines at this
-         ;; station. This may not be the case if the supplied
-         ;; station-name is not actually a tube station.
-         (json-ref (find (lambda (mode-group)
-                           (member (json-ref mode-group "modeName")
-                                   modes))
-                         (vector->list (json-ref (json->scm port)
-                                                 "lineModeGroups")))
-                   "lineIdentifier"))))))
+          ;; Assume the first search result is the station we are
+          ;; looking for.
+          (json-ref
+           (json-get (string-append %tfl-base-url
+                                    "/StopPoint/Search/"
+                                    (uri-encode station-name)
+                                    "?modes="
+                                    (string-join modes ",")))
+           "matches" 0 "id")))
+    ;; Assume that we actually find tube lines at this station. This
+    ;; may not be the case if the supplied station-name is not
+    ;; actually a tube station.
+    (vector->list
+     (json-ref (find (lambda (mode-group)
+                       (member (json-ref mode-group "modeName")
+                               modes))
+                     (vector->list
+                      (json-ref
+                       (json-get (string-append %tfl-base-url
+                                                "/StopPoint/" station-id))
+                       "lineModeGroups")))
+               "lineIdentifier"))))
 
 (define (list-house house)
   "Display details of @var{house} on the current output port."