An open API service indexing awesome lists of open source software.

https://github.com/r0man/routes-clj

A Clojure & ClojureScript library to build url and path fns.
https://github.com/r0man/routes-clj

Last synced: 6 months ago
JSON representation

A Clojure & ClojureScript library to build url and path fns.

Awesome Lists containing this project

README

          

#+title: routes-clj
#+author: roman
#+LANGUAGE: en

[[https://travis-ci.org/r0man/routes-clj][https://travis-ci.org/r0man/routes-clj.svg]]
[[http://jarkeeper.com/r0man/routes-clj][http://jarkeeper.com/r0man/routes-clj/status.svg]]
[[http://jarkeeper.com/r0man/routes-clj][http://jarkeeper.com/r0man/routes-clj/downloads.svg]]

* Installation

[[https://clojars.org/routes-clj][https://clojars.org/routes-clj/latest-version.svg]]

* Usage

Require the library.

#+BEGIN_SRC clojure :exports code :results silent
(require '[routes.core :refer :all])
#+END_SRC

Define routes by passing vectors of route pattern and route names do
=defroutes=.

#+BEGIN_SRC clojure :exports code :results silent
(defroutes my-routes
["/countries" :countries]
["/countries/:id-:name" :country]
["/spots" :spots]
["/spots/:id-:name" :spot])
#+END_SRC

Build URL paths by route name.

#+BEGIN_SRC clojure :exports both :results verbatim
(path-for :countries)
#+END_SRC

#+RESULTS:
: "/countries"

#+BEGIN_SRC clojure :exports both :results verbatim
(path-for :country {:id 1 :name "Spain"})
#+END_SRC

#+RESULTS:
: "/countries/1-Spain"

Build URL by route name.

#+BEGIN_SRC clojure :exports code :results silent
(def server
{:scheme :https
:server-name "example.com"
:server-port 443})
#+END_SRC

#+BEGIN_SRC clojure :exports both :results verbatim
(url-for server :countries)
#+END_SRC

#+RESULTS:
: "https://example.com/countries"

#+BEGIN_SRC clojure :exports both :results verbatim
(url-for server :country {:id 1 :name "Spain"})
#+END_SRC

#+RESULTS:
: "https://example.com/countries/1-Spain"

Build Ring request maps by route name.

#+BEGIN_SRC clojure :exports both :results verbatim
(request-for server :countries {:query-params {:sort "asc"}})
#+END_SRC

#+RESULTS:
: {:uri "/countries", :query-params {:sort "asc"}, :server-port 443, :server-name "example.com", :scheme :https, :request-method :get}

* License

Copyright © 2012-2015 r0man

Distributed under the Eclipse Public License, the same as Clojure.