public
Description: A webhook processor for Clojure
Homepage:
Clone URL: git://github.com/tomfaulhaber/github-hook.git
github-hook / com / infolace / parse_params.clj
100644 37 lines (31 sloc) 0.932 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns com.infolace.parse-params
  (:use
   [clojure.contrib.str-utils :only (re-split)])
  (:import java.net.URLDecoder))
 
;;; This code was lifted from Compojure and allows us to parse the posted params
 
(defn assoc-vec
  "Associate a key with a value. If the key already exists in the map, create a
vector of values."
  [map key val]
  (assoc map key
    (if-let [cur (map key)]
      (if (vector? cur)
        (conj cur val)
        [cur val])
      val)))
 
(defn urldecode
  "Decode a urlencoded string using the default encoding."
  [s]
  (URLDecoder/decode s "UTF-8"))
 
(defn parse-params
  "Parse parameters from a string into a map."
  [param-string separator]
  (reduce
    (fn [param-map s]
      (let [[key val] (re-split #"=" s)]
        (assoc-vec param-map
          (keyword (urldecode key))
          (urldecode (or val "")))))
    {}
    (remove #(or (nil? %) (= % ""))
      (re-split separator param-string))))