Skip to content

Instantly share code, notes, and snippets.

@ekhall
Created October 15, 2020 19:47
Show Gist options
  • Save ekhall/01ad567edfe3f6801e4c4f59dcd1c175 to your computer and use it in GitHub Desktop.
Save ekhall/01ad567edfe3f6801e4c4f59dcd1c175 to your computer and use it in GitHub Desktop.
(ns giggin.api
(:require [ajax.core :refer [GET]]
[giggin.state :as state]
[clojure.walk :as walk]))
(defn handler
"Resets the state/gigs atom (app state) to the response of the following data
transformation. A GET request is made to the URL below in fetch-gigs. The
keywords? option below turns all JSON key strings into symbols (`key`: `string`
to :key `string`). The `response` is then passed to the series of function calls."
[response]
(reset! state/gigs (->> response
;; https://clojuredocs.org/clojure.core/juxt
;; (into {} (map (juxt identity name) [:a :b :c :d]))
;; ;;=> {:a "a" :b "b" :c "c" :d "d"}
(map (juxt :id identity))
;; The into function forces the result to be a map rather
;; than a vector.
(into {})
;; This function walks through the results and converts the
;; strings "gig-02" to keys :gig-02.
(walk/keywordize-keys))))
(defn error-handler [{:keys [status status-text]}]
(.log js/console (str "something bad happened: " status " " status-text)))
(defn fetch-gigs
[]
(GET "https://gist.githubusercontent.com/jacekschae/8c10aa58a6163905fffcec33dd6f5a01/raw/20874f73b2c1d0ad08d828131d6ea8392950780a/gigs.json"
{:handler handler
:error-handler error-handler
:response-format :json
:keywords? true}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment