Last active
October 8, 2015 05:01
-
-
Save jmorton/7db7ebdd795b6e8606dd to your computer and use it in GitHub Desktop.
accept.clj
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
(defrecord Accept [media-range quality accept-extension]) | |
(defn accept [str] | |
"Parse a single accept string into a map" | |
; according to RFC2616, the "q" parameter must precede the accept-extension | |
(let [pattern #"([^;]+)\s*(?:;q=([0-9+\.]+))?\s*(;.+)*" | |
matches (re-find pattern str) | |
[_ media-range qvalue accept-extension] matches | |
quality (java.lang.Double/parseDouble (or qvalue "1"))] | |
(Accept. media-range quality accept-extension))) |
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 accept.core-test | |
(:require [clojure.test :refer :all] | |
[accept.core :refer :all] | |
:reload)) | |
(deftest accept-parser-tests | |
(is (= (accept "text/html") | |
(map->Accept {:media-range "text/html" :quality 1.0 :accept-extension nil} ))) | |
(is (= (accept "text/html;q=0.9") | |
(map->Accept {:media-range "text/html" :quality 0.9 :accept-extension nil} )))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment