factor out pages and util namespaces per #6

This commit is contained in:
Morgan 2015-03-10 09:29:58 +00:00
parent 1ff93e23bc
commit d88f645e26
3 changed files with 56 additions and 34 deletions

33
src/pronouns/pages.clj Normal file
View File

@ -0,0 +1,33 @@
(ns pronouns.pages
(:require [clojure.string :as s]
[pronouns.util :as u]))
(defn format-pronoun-examples
[subject object possessive-determiner possessive-pronoun reflexive]
(s/join "\n"
[(str subject " went to the park")
(str "I went with " object)
(str subject " brought " possessive-determiner " frisbee")
(str "at least I think it was " possessive-pronoun)
(str subject " threw it to " reflexive)]))
(defn parse-pronouns-with-lookup [pronouns-string pronouns-table]
(let [inputs (s/split pronouns-string #"/")
n (count inputs)]
(if (>= n 5)
(take 5 inputs)
(u/table-lookup inputs pronouns-table))))
(defn front []
(str "pronoun.is is a www site for showing people how to use pronouns"))
(defn not-found []
(str "We couldn't find those pronouns in our database, please ask us to "
"add them, or issue a pull request at "
"https://github.com/witch-house/pronoun.is/blob/master/resources/pronouns.tab"))
(defn pronouns [path pronouns-table]
(let [pronouns (parse-pronouns-with-lookup path pronouns-table)]
(if pronouns
(apply format-pronoun-examples pronouns)
(not-found))))

16
src/pronouns/util.clj Normal file
View File

@ -0,0 +1,16 @@
(ns pronouns.util
(:require [clojure.string :as s]))
(defn slurp-tabfile [path]
(let [lines (s/split (slurp path) #"\n")]
(map #(s/split % #"\t") lines)))
(defn table-lookup
[query-key table]
(let [arity (count query-key)
filtered-table (filter #(= query-key (take arity %)) table)]
(first filtered-table)))
(defn tabfile-lookup
[query-key tabfile]
(table-lookup query-key (slurp-tabfile tabfile)))

View File

@ -9,51 +9,24 @@
[ring.middleware.session :as session]
[ring.middleware.session.cookie :as cookie]
[ring.adapter.jetty :as jetty]
[environ.core :refer [env]]))
[environ.core :refer [env]]
[pronouns.util :as u]
[pronouns.pages :as pages]))
(def config {:default-server-port 5000
:pronoun-table-path "resources/pronouns.tab"})
(defn slurp-tabfile [path]
(let [lines (s/split (slurp path) #"\n")]
(map #(s/split % #"\t") lines)))
(defn lookup [inputs]
(let [pronouns-table (slurp-tabfile (:pronoun-table-path config))
n (count inputs)
filtered-table (filter #(= inputs (take n %)) pronouns-table)]
(first filtered-table)))
(defn parse-pronouns-with-lookup [pronouns-string]
(let [inputs (s/split pronouns-string #"/")
n (count inputs)]
(if (>= n 5)
(take 5 inputs)
(lookup inputs))))
(defn render-examples-page
([subject object possessive-determiner possessive-pronoun reflexive]
(s/join "\n"
[(str subject " went to the park")
(str "I went with " object)
(str subject " brought " possessive-determiner " frisbee")
(str "at least I think it was " possessive-pronoun)
(str subject " threw it to " reflexive)]))
([nothing]
(str "We couldn't find those pronouns in our database, please ask us to "
"add them, or issue a pull request at "
"https://github.com/witch-house/pronoun.is/blob/master/resources/pronouns.tab")))
(def pronouns-table (u/slurp-tabfile (:pronoun-table-path config)))
(defroutes app-routes
(GET "/" []
{:status 200
:headers {"Content-Type" "text/plain"}
:body "a blurb explaining how to use this site"})
:body (pages/front)})
(GET "/*" {params :params}
{:status 200
:headers {"Content-Type" "text/plain"}
:body (let [pronouns (parse-pronouns-with-lookup (:* params))]
(apply render-examples-page (or pronouns [:error])))})
:body (pages/pronouns (:* params) pronouns-table)})
(ANY "*" []
(route/not-found (slurp (io/resource "404.html")))))