Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Last active March 28, 2020 00:25
Show Gist options
  • Save adrianparvino/7e8c85792da140235262f2a08b985150 to your computer and use it in GitHub Desktop.
Save adrianparvino/7e8c85792da140235262f2a08b985150 to your computer and use it in GitHub Desktop.
;; Package-Requires: ((request "0.3.2"))
(require 'request)
(defmacro access (trail value)
(let ((trail_ (reverse trail))
(result value))
(dolist (focus trail_ result)
(pcase focus
(`(vector . ,index) (setq result `(aref ,result ',index)))
(`(object . ,key) (setq result `(alist-get ',key ,result)))))))
(defmacro vector-alist-let (spec value body)
(let ((varlist '())
(stack `((,spec . nil))))
(while stack
(let* ((_ (pop stack))
(focus (car _))
(trail (cdr _)))
(cond
((listp focus)
(dolist (spec focus)
(pcase spec
(`(,key . ,spec)
(push `(,spec . ((object . ,key) . ,trail)) stack))
(`,key
(push `(,key . ((object . ,key) . ,trail)) stack)))))
((vectorp focus)
(let ((index 0))
(mapc (lambda (elt)
(push `(,elt . ((vector . ,index) . ,trail)) stack)
(setq index (1+ index)))
focus)))
((symbolp focus)
(push `(,focus (access ,trail ,value)) varlist)))))
`(let ,varlist ,body)))
(request
"https://corona-stats.online/PH?format=json"
:params '(("key" . "value") ("key2" . "value2"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(vector-alist-let
((data . [(cases todayCases deaths todayDeaths)]))
data
(setq corona-stats (format " [COVID-19 %d(^%d) dead: %d(^%d)]" cases todayCases deaths todayDeaths))))))
(add-to-list 'global-mode-string 'corona-stats 'APPEND)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment