From 2e9cca8e4c42f56ea273b2a917ea8161cb4aec1f Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 30 Mar 2014 04:34:19 -0400 Subject: [PATCH] add text render api route --- src/toascii/models/flf.clj | 3 +++ src/toascii/routes/api/text.clj | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/toascii/routes/api/text.clj diff --git a/src/toascii/models/flf.clj b/src/toascii/models/flf.clj index 0f20501..8d2612b 100644 --- a/src/toascii/models/flf.clj +++ b/src/toascii/models/flf.clj @@ -156,6 +156,9 @@ (defonce fonts (atom {})) +(defn get-font [name] + (get @fonts name)) + (defn load-all! [] (->> flf-files (reduce diff --git a/src/toascii/routes/api/text.clj b/src/toascii/routes/api/text.clj new file mode 100644 index 0000000..798b676 --- /dev/null +++ b/src/toascii/routes/api/text.clj @@ -0,0 +1,39 @@ +(ns toascii.routes.api.text + (:require [clojure.string :as str] + [liberator.core :refer [defresource]] + [compojure.core :refer [ANY]] + [clj-figlet.core :as figlet] + [toascii.route-utils :refer [register-routes]] + [toascii.models.flf :refer [get-font]])) + +(defresource render-text [{:keys [s font format] :as params}] + :media-type-available? + (fn [ctx] + (let [type (condp = format + "html" "text/html" + "text" "text/plain" + "text/plain")] + {:representation {:media-type type}})) + :malformed? + (fn [_] + (str/blank? s)) + :exists? + (fn [ctx] + (let [font-name (or font "standard")] + (if-let [flf (get-font font-name)] + {:flf flf}))) + :handle-ok + (fn [ctx] + (let [rendered (figlet/render-to-string (:flf ctx) s)] + (if (= "text/html" (get-in ctx [:representation :media-type])) + (str "
" rendered "
") + rendered))) + :handle-malformed + (fn [_] + "Missing text to render.") + :handle-not-found + (fn [_] + "Font not found.")) + +(register-routes api-text-routes + (ANY "/api/text" {params :params} (render-text params)))