Skip to content

Commit

Permalink
Support piped json-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jrincon2 committed Aug 22, 2019
1 parent 9455549 commit 99e72ca
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rester/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,20 @@
(.getData e)))
(#(update % :body coerce-payload (get-in % [:headers "Content-Type"])))))

(defn piped-json-path [path data]
(loop [[path & paths] (str/split path #"\|")
data data]
(if path
(recur paths (at-path (if (str/starts-with? path "$") path (str "$" path)) data))
data)))

(defn extract-data [{:keys [status body headers] :as resp} extractions]
(into {} (for [[name path] extractions]
(try
(let [result (cond
(.startsWith path "$") (at-path path body)
(.startsWith path "$") (piped-json-path path body)
(.startsWith path "#") (re-find (re-pattern path) body)
true (at-path (str "$." path) resp))]
true (piped-json-path (str "$." path) resp))]
(log/info "extracted:" name "=" result)
[name result])
(catch Exception e
Expand Down

0 comments on commit 99e72ca

Please sign in to comment.