Giter VIP home page Giter VIP logo

clout's Introduction

Clout

Build Status

Clout is a library for matching Ring HTTP requests. It uses the same routing syntax as used by popular Ruby web frameworks like Ruby on Rails and Sinatra.

Installation

Add the following to your project.clj dependencies:

[clout "2.0.0"]

Usage

Require Clout in the normal way:

(require '[clout.core :as clout])

These following examples also make use of the ring-mock library to generate Ring request maps:

(require '[ring.mock.request :as mock])

Routes can match by keyword:

(clout/route-matches
 "/article/:title"
 (mock/request :get "/article/clojure"))

=> {:title "clojure"}

Or with wildcards:

(clout/route-matches
 "/public/*"
 (mock/request :get "/public/style/screen.css"))
 
=> {:* "style/screen.css"}

Clout can also match absolute routes:

(clout/route-matches
 "http://subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

And scheme-relative routes:

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "http://subdomain.example.com/"))

=> {}

(clout/route-matches
 "//subdomain.example.com/"
 (mock/request :get "https://subdomain.example.com/"))
 
=> {}

Clout supports both keywords and wildcards. Keywords (like ":title") will match any character but the following: / . , ; ?. Wildcards (*) will match anything.

If a route does not match, nil is returned:

(clout/route-matches "/products" (mock/request :get "/articles"))

=> nil

For additional performance, you can choose to pre-compile a route:

(def user-route
  (clout/route-compile "/user/:id"))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:id "10"}

When compiling a route, you can specify a map of regular expressions to use for different keywords. This allows more specific routing:

(def user-route
  (clout/route-compile "/user/:id" {:id #"\d+"}))

(clout/route-matches user-route (mock/request :get "/user/10"))

=> {:user "10"}

(clout/route-matches user-route (mock/request :get "/user/jsmith"))

=> nil

License

Copyright © 2014 James Reeves

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

clout's People

Contributors

amalloy avatar duckyuck avatar gfredericks avatar joelittlejohn avatar weavejester avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.