Permalink
Browse files

sort-by-check helper for sorting accept headers

  • Loading branch information...
1 parent 5384c40 commit 9786395ce5314cd1a687d114d9e5abf79b99c9df Jani Rahkola committed Apr 9, 2012
Showing with 10 additions and 26 deletions.
  1. +10 −26 src/ring/middleware/format_response.clj
@@ -27,29 +27,13 @@
(or (= "*" sub-type)
(= (get-in encoder [:enc-type :sub-type]) sub-type))))))
-(def sort-by-param
- (partial sort-by
- :parameter
- (fn [a b]
- (cond (= (nil? a) (nil? b)) 0
- (nil? a) 1
- :else -1))))
-
-(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 sort-by-check
+ [by check headers]
+ (sort-by by (fn [a b]
+ (cond (= (= a check) (= b check)) 0
+ (= a check) 1
+ :else -1))
+ headers))
(defn parse-accept-header
"Parse Accept headers into a sorted sequence of maps.
@@ -76,9 +60,9 @@
(assoc type :q 1.0))
:parameter (s/trim (first rest))))))
(s/split accept-header #","))
- sort-by-param
- sort-by-type
- sort-by-sub-type
+ (sort-by-check :parameter nil)
+ (sort-by-check :type "*")
+ (sort-by-check :sub-type "*")
(sort-by :q >)))
(defn preferred-encoder

0 comments on commit 9786395

Please sign in to comment.