Created
August 6, 2018 06:15
-
-
Save jeluard/dee7451bebad0eca15055faf25c0b4f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns pluto.storage.gist | |
(:require [pluto.storage :as storage])) | |
(defn result [xhr] | |
(let [status (.-status xhr)] | |
(if (= 404 status) | |
{:type :error :value status} | |
{:type :success :value [{:content (.-responseText xhr)}]}))) | |
;; TODO Handle all edn files types, not only extension.edn | |
(defn gist-url [id] | |
(str "https://gist.githubusercontent.com/" id "/raw")) | |
(deftype GistStorage [] | |
storage/Storage | |
(fetch [_ {:keys [value]} callback] | |
(let [xhr (js/XMLHttpRequest.)] | |
(.open xhr "GET" (gist-url value) true) | |
(.send xhr nil) | |
(set! (.-onreadystatechange xhr) | |
#(when (= (.-readyState xhr) 4) | |
(callback (result xhr))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment