Created
March 11, 2014 08:17
-
-
Save sordina/9481515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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