Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated README

  • Loading branch information...
commit 0e09615c651d3ec56d6ee924619e9238fa83bab3 1 parent dfa593f
@weavejester weavejester authored
Showing with 27 additions and 21 deletions.
  1. +27 −21 README.md
View
48 README.md
@@ -1,30 +1,33 @@
Clout
=====
-Clout is a library for matching HTTP routes in Clojure. It uses the same
+Clout is a library for matching [Ring][1] HTTP requests. It uses the same
routing syntax as used by popular Ruby web frameworks like Ruby on Rails and
Sinatra.
-Here is an example of use:
+Usage
+-----
- user=> (use 'clout)
+These following examples make use of the [ring-mock][2] library to
+generate Ring request maps.
+
+ user=> (use 'ring.mock.request 'clout.core)
nil
- user=> (route-matches "/article/:title" "/article/clojure")
- {"title" "clojure"}
- user=> (route-matches "/public/*" "/public/style/screen.css")
- {"*" "style/screen.css"}
+ user=> (route-matches "/article/:title"
+ (request :get "/article/clojure"))
+ {:title "clojure"}
+ user=> (route-matches "/public/*"
+ (request :get "/public/style/screen.css"))
+ {:* "style/screen.css"}
-Clout can also match Ring requests:
+Clout can also match absolute routes:
- user=> (route-matches "/book/:id"
- {:request-method :get
- :headers {"Host" "example.com"}
- :uri "/book/123"}
- :body nil})
- {"id" "123"}
+ user=> (route-matches "http://subdomain.example.com/"
+ (request :get "http://subdomain.example.com/"))
+ {}
Clout supports both keywords and wildcards. Keywords (like ":title") will
-match any character but the following: `/ . , ; ?`. Wildcards will match
+match any character but the following: `/ . , ; ?`. Wildcards (*) will match
anything.
If a route does not match, nil is returned:
@@ -36,15 +39,18 @@ For additional performance, you can choose to pre-compile a route:
user=> (def user-route (route-compile "/user/:id"))
#'user/user-route
- user=> (route-matches user-route "/user/10")
- {"user" "10"}
+ user=> (route-matches user-route (request :get "/user/10"))
+ {:user "10"}
When compiling a route, you can specify a map of regular expressions to use
for different keywords. This allows more specific routing:
- user=> (def user-route (route-compile "/user/:id" {"id" #"\d+"}))
+ user=> (def user-route (route-compile "/user/:id" {:id #"\d+"}))
#'user/user-route
- user=> (route-matches user-route "/user/10")
- {"user" "10"}
- user=> (route-matches user-route "/user/jsmith")
+ user=> (route-matches user-route (request :get "/user/10"))
+ {:user "10"}
+ user=> (route-matches user-route (request :get "/user/jsmith"))
nil
+
+[1]: https://github.com/mmcgrana/ring
+[2]: https://github.com/weavejester/ring-mock
Please sign in to comment.
Something went wrong with that request. Please try again.