Skip to content

Instantly share code, notes, and snippets.

@sordina
Created March 11, 2014 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sordina/9481515 to your computer and use it in GitHub Desktop.
Save sordina/9481515 to your computer and use it in GitHub Desktop.
(ns urlpatternmatching
(:require [midje.sweet :refer :all]))
(defn split [string]
(let [matches (re-seq #"/([^/]+)" string)
groups (map #(% 1) matches)
]
groups))
(defn isAuthorizedSection [env patternSection urlSection]
(let [match (re-matches #"\{(.*)\}" patternSection)
pattern (and match (match 1)) ]
(if match
(= (env pattern) urlSection)
(= patternSection urlSection))))
(defn isAuthorized [env pattern url]
"Takes a environment to look up variables, a pattern-url, and a requested url,
and determines if the requested url matches the pattern."
(every? identity
(map #(isAuthorizedSection env %1 %2)
(split pattern) (split url))))
(fact (and true true true) => true)
(fact (re-matches #"a(b)" "ab") => ["ab" "b"])
(fact (['a 'b] 0) => 'a)
(fact (map #(do [%1 %2]) [1,2] [3,4]) => [[1 3] [2 4]])
(fact (and true true) => true)
(fact (and false true) => false)
(fact (+ 1 1) => 2)
(fact (isAuthorized {"name" "dave"} "/user/{name}/cats" "/user/dave/cats") => true)
(fact (isAuthorized {"name" "dave"} "/user/{name}/cats" "/user/frank/cats") => false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment