add resource handler for random ascii art under a given name

This commit is contained in:
Gered 2014-04-06 17:04:35 -04:00
parent 363edd0200
commit df0f6d7429

View file

@ -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 "<pre>" (:ascii ctx) "</pre>")
(: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)))