Skip to content

Instantly share code, notes, and snippets.

@arthuredelstein
Last active September 27, 2016 05:05
Show Gist options
  • Save arthuredelstein/4970228 to your computer and use it in GitHub Desktop.
Save arthuredelstein/4970228 to your computer and use it in GitHub Desktop.
Reading a file on github via jsonp (no authentication needed) with clojurescript in the web browser. Requires JQuery.
(ns github-jsonp)
(defn store-callback [reference address-vec]
(fn [val]
(swap! reference assoc-in address-vec val)))
(defn base64-decode [x]
(js/atob (clojure.string/replace x #"\s" "")))
(defn jsonp [url callback]
(.getJSON js/jQuery (str url "&callback=?") #(callback (js->clj %))))
(def files (atom nil))
(defn github-file
([{:keys [user repo path]} callback]
(let [url (str "https://api.github.com/repos/" user "/" repo "/contents/" path "?")]
(jsonp url #(-> % "data" "content" base64-decode callback))))
([github-map]
(github-file github-map
(store-callback files [(assoc github-map :source :github)]))))
(defn test-github []
(github-file
{:user "arthuredelstein" :repo "clooj" :path "src/clooj/repl.clj"}
(store-callback test-results [:test-file])))
(defn load-hiccups []
(dorun (map github-file [{:user "teropa" :repo "hiccups" :path "src/hiccups/runtime.clj"}
{:user "teropa" :repo "hiccups" :path "src/hiccups/runtime.cljs"}
{:user "teropa" :repo "hiccups" :path "src/hiccups/core.clj"}])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment