Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Commit

Permalink
sort-by-check helper for sorting accept headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Rahkola committed Apr 9, 2012
1 parent 5384c40 commit 9786395
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/ring/middleware/format_response.clj
Expand Up @@ -27,29 +27,13 @@
(or (= "*" sub-type) (or (= "*" sub-type)
(= (get-in encoder [:enc-type :sub-type]) sub-type)))))) (= (get-in encoder [:enc-type :sub-type]) sub-type))))))


(def sort-by-param (defn sort-by-check
(partial sort-by [by check headers]
:parameter (sort-by by (fn [a b]
(fn [a b] (cond (= (= a check) (= b check)) 0
(cond (= (nil? a) (nil? b)) 0 (= a check) 1
(nil? a) 1 :else -1))
:else -1)))) headers))

(def sort-by-type
(partial sort-by
:type
(fn [a b]
(cond (= (= a "*") (= b "*")) 0
(= a "*") 1
:else -1))))

(def sort-by-sub-type
(partial sort-by
:sub-type
(fn [a b]
(cond (= (= a "*") (= b "*")) 0
(= a "*") 1
:else -1))))


(defn parse-accept-header (defn parse-accept-header
"Parse Accept headers into a sorted sequence of maps. "Parse Accept headers into a sorted sequence of maps.
Expand All @@ -76,9 +60,9 @@
(assoc type :q 1.0)) (assoc type :q 1.0))
:parameter (s/trim (first rest)))))) :parameter (s/trim (first rest))))))
(s/split accept-header #",")) (s/split accept-header #","))
sort-by-param (sort-by-check :parameter nil)
sort-by-type (sort-by-check :type "*")
sort-by-sub-type (sort-by-check :sub-type "*")
(sort-by :q >))) (sort-by :q >)))


(defn preferred-encoder (defn preferred-encoder
Expand Down

0 comments on commit 9786395

Please sign in to comment.