From ee81f454e9aa50c3667741a17843136805885ffb Mon Sep 17 00:00:00 2001 From: Joe Dytrych Date: Sun, 14 Feb 2016 22:51:38 +0000 Subject: Add JSON response with Accept: application/json header --- src/pronouns/pages.clj | 55 ++++++++++++++++++++++++++++---------------------- src/pronouns/util.clj | 12 +++++------ src/pronouns/web.clj | 16 +++++++++------ 3 files changed, 47 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/pronouns/pages.clj b/src/pronouns/pages.clj index 3e63437..7d68422 100644 --- a/src/pronouns/pages.clj +++ b/src/pronouns/pages.clj @@ -1,5 +1,6 @@ (ns pronouns.pages (:require [clojure.string :as s] + [clojure.data.json :as json] [pronouns.util :as u] [hiccup.core :refer :all] [hiccup.util :refer [escape-html]])) @@ -60,30 +61,34 @@ (defn contact-block [] (let [twitter-name (fn [handle] [:a {:href (str "https://www.twitter.com/" handle)} (str "@" handle)])] - [:div {:class "contact"} - [:p - "Written by " - (twitter-name "morganastra") - ", whose " - [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] - ". " - "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) + [:div {:class "contact"} + [:p + "Written by " + (twitter-name "morganastra") + ", whose " + [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] + ". " + "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) (defn format-pronoun-examples [subject object possessive-determiner possessive-pronoun reflexive] (let [title "Pronoun Island: English Language Examples"] - (html - [:html - [:head - [:title title] - [:meta {:name "viewport" :content "width=device-width"}] - [:link {:rel "stylesheet" :href "/pronouns.css"}]] - [:body - (title-block title) - (examples-block subject object possessive-determiner possessive-pronoun reflexive) - (about-block) - (contact-block)]]))) + (html + [:html + [:head + [:title title] + [:meta {:name "viewport" :content "width=device-width"}] + [:link {:rel "stylesheet" :href "/pronouns.css"}]] + [:body + (title-block title) + (examples-block subject object possessive-determiner possessive-pronoun reflexive) + (about-block) + (contact-block)]]))) + + +(defn format-pronoun-json [pronouns] + (json/write-str pronouns)) (defn parse-pronouns-with-lookup [pronouns-string pronouns-table] @@ -111,9 +116,9 @@ [:body (title-block title) [:div {:class "table"} - [:p "pronoun.is is a www site for showing people how to use pronouns in English."] - [:p "here are some pronouns the site knows about:"] - [:ul links]]] + [:p "pronoun.is is a www site for showing people how to use pronouns in English."] + [:p "here are some pronouns the site knows about:"] + [:ul links]]] (contact-block)]))) (defn not-found [] @@ -121,8 +126,10 @@ "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] +(defn pronouns [path pronouns-table accept] (let [pronouns (parse-pronouns-with-lookup (escape-html path) pronouns-table)] (if pronouns - (apply format-pronoun-examples pronouns) + (if (= accept :json) + (format-pronoun-json pronouns) + (apply format-pronoun-examples pronouns)) (not-found)))) diff --git a/src/pronouns/util.clj b/src/pronouns/util.clj index 4d4f928..51e8403 100644 --- a/src/pronouns/util.clj +++ b/src/pronouns/util.clj @@ -18,12 +18,12 @@ (defn minimum-unambiguous-path ([pronouns-table sections] (minimum-unambiguous-path pronouns-table sections 1)) ([pronouns-table sections number-of-sections] - (let [sections-subset (take number-of-sections sections) - results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] - (case (count results) - 0 nil - 1 (clojure.string/join "/" sections-subset) - (recur pronouns-table sections (+ number-of-sections 1)))))) + (let [sections-subset (take number-of-sections sections) + results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] + (case (count results) + 0 nil + 1 (clojure.string/join "/" sections-subset) + (recur pronouns-table sections (+ number-of-sections 1)))))) (defn abbreviate "given a list of pronoun rows, return a list of minimum unabiguous paths" diff --git a/src/pronouns/web.clj b/src/pronouns/web.clj index 23ac603..4ec1788 100644 --- a/src/pronouns/web.clj +++ b/src/pronouns/web.clj @@ -25,13 +25,17 @@ (GET "/pronouns.css" {params :params} {:status 200 - :headers {"Content-Type" "text/css"} - :body (slurp (io/resource "pronouns.css"))}) + :headers {"Content-Type" "text/css"} + :body (slurp (io/resource "pronouns.css"))}) - (GET "/*" {params :params} - {:status 200 - :headers {"Content-Type" "text/html"} - :body (pages/pronouns (:* params) pronouns-table)}) + (GET "/*" {params :params headers :headers} + (if (= "application/json" (get headers "accept")) + {:status 200 + :headers {"Content-Type" "application/json"} + :body (pages/pronouns (:* params) pronouns-table :json)} + {:status 200 + :headers {"Content-Type" "text/html"} + :body (pages/pronouns (:* params) pronouns-table :html)})) (ANY "*" [] (route/not-found (slurp (io/resource "404.html"))))) -- cgit From a60cfd359c283d194c24831a5454bdd8cbfe2364 Mon Sep 17 00:00:00 2001 From: Joe Dytrych Date: Mon, 15 Feb 2016 20:08:12 +0000 Subject: JSON not found response, fix whitespace --- src/pronouns/pages.clj | 64 +++++++++++++++++++++++++++----------------------- src/pronouns/util.clj | 12 +++++----- src/pronouns/web.clj | 6 ++--- 3 files changed, 44 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/pronouns/pages.clj b/src/pronouns/pages.clj index 7d68422..bdcd9d6 100644 --- a/src/pronouns/pages.clj +++ b/src/pronouns/pages.clj @@ -61,33 +61,33 @@ (defn contact-block [] (let [twitter-name (fn [handle] [:a {:href (str "https://www.twitter.com/" handle)} (str "@" handle)])] - [:div {:class "contact"} - [:p - "Written by " - (twitter-name "morganastra") - ", whose " - [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] - ". " - "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) + [:div {:class "contact"} + [:p + "Written by " + (twitter-name "morganastra") + ", whose " + [:a {:href "http://pronoun.is/ze/zir"} "pronoun.is/ze/zir"] + ". " + "Visit the project on " [:a {:href "https://github.com/witch-house/pronoun.is"} "github!"]]])) (defn format-pronoun-examples [subject object possessive-determiner possessive-pronoun reflexive] (let [title "Pronoun Island: English Language Examples"] - (html - [:html - [:head - [:title title] - [:meta {:name "viewport" :content "width=device-width"}] - [:link {:rel "stylesheet" :href "/pronouns.css"}]] - [:body - (title-block title) - (examples-block subject object possessive-determiner possessive-pronoun reflexive) - (about-block) - (contact-block)]]))) - - -(defn format-pronoun-json [pronouns] + (html + [:html + [:head + [:title title] + [:meta {:name "viewport" :content "width=device-width"}] + [:link {:rel "stylesheet" :href "/pronouns.css"}]] + [:body + (title-block title) + (examples-block subject object possessive-determiner possessive-pronoun reflexive) + (about-block) + (contact-block)]]))) + + +(defn format-pronoun-json [& pronouns] (json/write-str pronouns)) @@ -116,9 +116,9 @@ [:body (title-block title) [:div {:class "table"} - [:p "pronoun.is is a www site for showing people how to use pronouns in English."] - [:p "here are some pronouns the site knows about:"] - [:ul links]]] + [:p "pronoun.is is a www site for showing people how to use pronouns in English."] + [:p "here are some pronouns the site knows about:"] + [:ul links]]] (contact-block)]))) (defn not-found [] @@ -126,10 +126,16 @@ "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 accept] +(defn not-found-json [] + (json/write-str {:error "Not found"})) + +(defn pronouns-page [path pronouns-table format-pronouns not-found] (let [pronouns (parse-pronouns-with-lookup (escape-html path) pronouns-table)] (if pronouns - (if (= accept :json) - (format-pronoun-json pronouns) - (apply format-pronoun-examples pronouns)) + (apply format-pronouns pronouns) (not-found)))) + +(defn pronouns [path pronouns-table accept] + (if (= accept :json) + (pronouns-page path pronouns-table format-pronoun-json not-found-json) + (pronouns-page path pronouns-table format-pronoun-examples not-found))) diff --git a/src/pronouns/util.clj b/src/pronouns/util.clj index 51e8403..4d4f928 100644 --- a/src/pronouns/util.clj +++ b/src/pronouns/util.clj @@ -18,12 +18,12 @@ (defn minimum-unambiguous-path ([pronouns-table sections] (minimum-unambiguous-path pronouns-table sections 1)) ([pronouns-table sections number-of-sections] - (let [sections-subset (take number-of-sections sections) - results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] - (case (count results) - 0 nil - 1 (clojure.string/join "/" sections-subset) - (recur pronouns-table sections (+ number-of-sections 1)))))) + (let [sections-subset (take number-of-sections sections) + results (filter #(= (take number-of-sections %) sections-subset) pronouns-table)] + (case (count results) + 0 nil + 1 (clojure.string/join "/" sections-subset) + (recur pronouns-table sections (+ number-of-sections 1)))))) (defn abbreviate "given a list of pronoun rows, return a list of minimum unabiguous paths" diff --git a/src/pronouns/web.clj b/src/pronouns/web.clj index 4ec1788..bf717eb 100644 --- a/src/pronouns/web.clj +++ b/src/pronouns/web.clj @@ -25,11 +25,11 @@ (GET "/pronouns.css" {params :params} {:status 200 - :headers {"Content-Type" "text/css"} - :body (slurp (io/resource "pronouns.css"))}) + :headers {"Content-Type" "text/css"} + :body (slurp (io/resource "pronouns.css"))}) (GET "/*" {params :params headers :headers} - (if (= "application/json" (get headers "accept")) + (if (= "application/json" (.toLowerCase (get headers "accept"))) {:status 200 :headers {"Content-Type" "application/json"} :body (pages/pronouns (:* params) pronouns-table :json)} -- cgit From b0eac61d82c9e03cf9b2eb646b3acabb49c02acb Mon Sep 17 00:00:00 2001 From: Joe Dytrych Date: Wed, 17 Feb 2016 22:10:04 +0000 Subject: separate functions for json & html responses --- src/pronouns/pages.clj | 9 +++++---- src/pronouns/web.clj | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/pronouns/pages.clj b/src/pronouns/pages.clj index bdcd9d6..8aed689 100644 --- a/src/pronouns/pages.clj +++ b/src/pronouns/pages.clj @@ -135,7 +135,8 @@ (apply format-pronouns pronouns) (not-found)))) -(defn pronouns [path pronouns-table accept] - (if (= accept :json) - (pronouns-page path pronouns-table format-pronoun-json not-found-json) - (pronouns-page path pronouns-table format-pronoun-examples not-found))) +(defn pronouns [path pronouns-table] + (pronouns-page path pronouns-table format-pronoun-examples not-found)) + +(defn pronouns-json [path pronouns-table] + (pronouns-page path pronouns-table format-pronoun-json not-found-json)) diff --git a/src/pronouns/web.clj b/src/pronouns/web.clj index bf717eb..5af441f 100644 --- a/src/pronouns/web.clj +++ b/src/pronouns/web.clj @@ -32,10 +32,10 @@ (if (= "application/json" (.toLowerCase (get headers "accept"))) {:status 200 :headers {"Content-Type" "application/json"} - :body (pages/pronouns (:* params) pronouns-table :json)} + :body (pages/pronouns-json (:* params) pronouns-table)} {:status 200 :headers {"Content-Type" "text/html"} - :body (pages/pronouns (:* params) pronouns-table :html)})) + :body (pages/pronouns (:* params) pronouns-table)})) (ANY "*" [] (route/not-found (slurp (io/resource "404.html"))))) -- cgit