diff --git a/src/toascii/routes/api/art.clj b/src/toascii/routes/api/art.clj index 35fa811..a73e237 100644 --- a/src/toascii/routes/api/art.clj +++ b/src/toascii/routes/api/art.clj @@ -18,5 +18,35 @@ (:error ctx)) ) +(defresource get-random-art [{:keys [name format]}] + :media-type-available? + (fn [ctx] + (let [type (condp = format + "html" "text/html" + "text" "text/plain" + "text/plain")] + {:representation {:media-type type}})) + :malformed? + (fn [_] + (if-not (art/valid-name? name) + {:error "Invalid name."})) + :exists? + (fn [_] + (if-let [ascii (art/get-random name)] + {:ascii ascii} + [false {:error "No art found under that name."}])) + :handle-ok + (fn [ctx] + (if (= "text/html" (get-in ctx [:representation :media-type])) + (str "
" (:ascii ctx) "") + (:ascii ctx))) + :handle-malformed + (fn [ctx] + (:error ctx)) + :handle-not-found + (fn [ctx] + (:error ctx))) + (register-routes api-art-routes + (ANY "/api/art/:name" {params :params} (get-random-art params)) (ANY "/api/art" {{q :q} :params} (art-search q)))