Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Safely read edn strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisetheridge committed Jan 24, 2017
1 parent 0411a86 commit 6fdfa38
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/main/clojure/jarvis/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,44 @@

(defn- env-variable [name]
(-> (System/getenv)
(get name)))
(get name)))

(defn- build-string [content]
(str "[" content "]"))

(defn config-property
([name]
(config-property name nil))
(config-property name nil))
([name default-value]
(if-let [env-value (env-variable name)]
env-value
(if-let [system-value (System/getProperty name)]
system-value
default-value))))
(if-let [env-value (env-variable name)]
env-value
(if-let [system-value (System/getProperty name)]
system-value
default-value))))

(defn enhance-message [message]
(-> message
(assoc "parent" (m/parent-message message))
(assoc "user" (user/get (m/user message)))
debug/print-message))
(assoc "parent" (m/parent-message message))
(assoc "user" (user/get (m/user message)))
debug/print-message))

(defn close-flow-connection [flow-connection]
(log/info (str "Closing flow connection - " (.flow-id flow-connection)))
(m/chat (.flow-id flow-connection) "Very well sir, goodbye.")
(f/block-user (.flow-id flow-connection) (get (user/me) "id"))
(.close flow-connection))

(defn safe-read [string]
(try
(edn/read-string string)
(catch NumberFormatException _ (str string))))

(defn message-content->vec [message]
(vec (map str (-> message
(get "content")
(subs 1)
build-string
(edn/read-string)))))
(get "content")
(subs 1)
build-string
safe-read))))

(defn test-env? []
(= "TEST" (config-property "ENVIRONMENT" false)))
(= "TEST" (config-property "ENVIRONMENT" false)))

0 comments on commit 6fdfa38

Please sign in to comment.