Permalink
Browse files

add missing file

  • Loading branch information...
1 parent b4c024d commit f10c9787ebc0d05f8046fe14e25341dd484a8b7d @ninjudd ninjudd committed Jul 8, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 src/wakeful/content_type.clj
@@ -0,0 +1,33 @@
+(ns wakeful.content-type
+ (:use [useful.map :only [update]]
+ [useful.fn :only [!]]
+ [useful.utils :only [fix]]
+ [clojure.string :only [split]])
+ (:require [clj-json.core :as json]))
+
+(defn slurp-body [body]
+ (fix body (! string?) slurp))
+
+(defn content-type [_ content-type]
+ (first (split content-type #"\s*;")))
+
+(defmulti decode-body content-type)
+(defmulti encode-body content-type)
+
+(defmethod decode-body :default [body content-type] body)
+(defmethod encode-body :default [body content-type] body)
+
+(defmethod decode-body "application/json" [body content-type]
+ (when body
+ (-> body slurp-body json/parse-string)))
+
+(defmethod encode-body "application/json" [body content-type]
+ (json/generate-string body))
+
+(defn wrap-content-type [handler content-type]
+ (fn [request]
+ (let [response (handler (-> (assoc request :form-params {})
+ (update :body decode-body content-type)))]
+ (when response
+ (-> (assoc-in response [:headers "Content-Type"] content-type)
+ (update :body encode-body content-type))))))

0 comments on commit f10c978

Please sign in to comment.