Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Created September 9, 2015 10:58
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 postspectacular/051a325685a90984eb71 to your computer and use it in GitHub Desktop.
Save postspectacular/051a325685a90984eb71 to your computer and use it in GitHub Desktop.
Parse accept header into prioritized seq
(require '[thi.ng.strf.core :as strf])
(require '[clojure.string :as str])
(defn parse-accept-header
[accept]
(->> accept
(re-seq #"((([\w\*\-]+/[\w\*\-\+]+,?)+)(;q=(\d+\.\d+))?)")
(mapcat
(fn [[_ _ a _ _ q]]
(let [q (if q (strf/parse-float q 0) 1)]
(map vector (str/split a #",") (repeat q)))))
(sort-by peek)
reverse))
(parse-accept-header
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json")
;; (["application/json" 1] ["application/xml" 0.9] ["application/xhtml+xml" 0.9] ["text/html" 0.9] ["*/*" 0.8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment