Skip to content

Commit

Permalink
Merge branch '0.5-beta'
Browse files Browse the repository at this point in the history
Conflicts:
	src/compojure/route.clj
	test/compojure/test/route.clj
  • Loading branch information
weavejester committed Sep 21, 2010
2 parents bcafbed + 584f985 commit 2be9925
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lib
classes
compojure.jar
*.jar
pom.xml
_site
14 changes: 7 additions & 7 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(defproject compojure "0.4.1"
(defproject compojure "0.5.0-beta3"
:description "A concise web framework for Clojure"
:url "http://github/weavejester/compojure"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]
[clout "0.2.0"]
[ring/ring-core "0.2.5"]]
:dev-dependencies [[lein-clojars "0.5.0"]
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[clout "0.3.0"]
[ring/ring-core "0.3.0"]]
:dev-dependencies [[lein-clojars "0.6.0"]
[swank-clojure "1.2.1"]
[ring/ring-jetty-adapter "0.2.5"]])
[ring/ring-jetty-adapter "0.3.0"]])
23 changes: 8 additions & 15 deletions src/compojure/core.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns compojure.core
"A concise syntax for generating Ring handlers."
(:use [ring.middleware params cookies]
(:use clojure.contrib.def
[ring.middleware params cookies]
clout.core
compojure.response))

Expand Down Expand Up @@ -60,7 +61,7 @@
(if-let [route-params# (route-matches route# request#)]
(let [request# (#'assoc-route-params request# route-params#)]
(bind-request request# ~bindings
(render request# (do ~@body)))))))))
(render (do ~@body) request#))))))))

(defn routes
"Create a Ring handler by combining several handlers into one."
Expand All @@ -70,20 +71,12 @@
(fn [request]
(some #(% request) handlers)))))

(defn- apply-doc
"Return a symbol and body with an optional docstring applied."
[name doc? body]
(if (string? doc?)
(list* (vary-meta name assoc :doc doc?) body)
(list* name doc? body)))

(defmacro defroutes
"Define a Ring handler function from a sequence of routes. Takes an optional
doc-string."
[name doc? & routes]
(let [[name & routes] (apply-doc name doc? routes)]
`(def ~name
(routes ~@routes))))
"Define a Ring handler function from a sequence of routes. The name may be
optionally be followed by a doc-string and metadata map."
[name & routes]
(let [[name routes] (name-with-attributes name routes)]
`(def ~name (routes ~@routes))))

(defmacro GET "Generate a GET route."
[path args & body]
Expand Down
72 changes: 41 additions & 31 deletions src/compojure/response.clj
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
(ns compojure.response
"Methods for generating Ring response maps"
(:use [ring.util.response :only (response header)])
(:import java.util.Map
[java.io File InputStream]
[clojure.lang IDeref IFn ISeq]))

(defmulti render
"Given the request map and an arbitrary value x, turn x into a valid HTTP
response map. Dispatches on the type of x."
(fn [_ x] (type x)))

(defmethod render nil [_ _] nil)

(defmethod render String [_ html]
{:status 200
:headers {"Content-Type" "text/html"}
:body html})

(defmethod render Map [_ m]
(merge {:status 200, :headers {}, :body ""} m))

(defmethod render IFn [request handler]
(render request (handler request)))

(defmethod render IDeref [request ref-like]
(render request (deref ref-like)))

(defmethod render File [_ file]
{:status 200, :headers {}, :body file})

(defmethod render ISeq [_ coll]
{:status 200, :headers {}, :body coll})

(defmethod render InputStream [_ stream]
{:status 200, :headers {}, :body stream})

(prefer-method render Map IFn)
(defprotocol Renderable
(render [this request]
"Render the object into a form suitable for the given request map."))

(extend-type nil
Renderable
(render [_ _] nil))

(extend-type String
Renderable
(render [this _]
(-> (response this)
(header "Content-Type" "text/html"))))

(extend-type Map
Renderable
(render [this _]
(merge (response "") this)))

(extend-type IFn
Renderable
(render [this request]
(render (this request) request)))

(extend-type IDeref
Renderable
(render [this request]
(render (deref this) request)))

(extend-type File
Renderable
(render [this _] (response this)))

(extend-type ISeq
Renderable
(render [this _] (response this)))

(extend-type InputStream
Renderable
(render [this _] (response this)))
4 changes: 2 additions & 2 deletions src/compojure/route.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
(defn resources
"A route for serving resources on the classpath. Accepts the following
keys:
:root - the root prefix to get the resources from. Defaults to 'public'."
:root - the root prefix to get the resources from. Defaults to '/public'."
[path & [options]]
(GET (add-wildcard path) {{resource-path "*"} :params}
(let [options (merge {:root "public"} options)]
(let [options (merge {:root "/public"} options)]
(resource-response resource-path options))))

(defn not-found
Expand Down
8 changes: 4 additions & 4 deletions test/compojure/test/response.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
(:require [compojure.response :as response]))

(deftest response-with-nil
(is (nil? (response/render {} nil))))
(is (nil? (response/render nil {}))))

(def test-response
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>Foo</h1>"})

(deftest response-with-string
(is (= (response/render {} "<h1>Foo</h1>")
(is (= (response/render "<h1>Foo</h1>" {})
test-response)))

(deftest response-with-fn
(is (= (response/render {} (constantly test-response))
(is (= (response/render (constantly test-response) {})
test-response)))

(deftest response-with-deref
(is (= (response/render {} (future test-response))
(is (= (response/render (future test-response) {})
test-response)))
4 changes: 2 additions & 2 deletions test/compojure/test/route.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns compojure.test.route
(:use clojure.test
[clojure.contrib.duck-streams :only (slurp*)])
[clojure.contrib.io :only (slurp*)])
(:require [compojure.route :as route]))

(deftest not-found-route
Expand All @@ -9,7 +9,7 @@
(is (= (:body response) "foo"))))

(deftest resources-route
(let [route (route/resources "/foo" {:root "resources"})
(let [route (route/resources "/foo" {:root "/resources"})
response (route {:request-method :get, :uri "/foo/test.txt"})]
(is (= (:status response) 200))
(is (= (slurp* (:body response)) "foobar\n"))))

0 comments on commit 2be9925

Please sign in to comment.