Skip to content

Commit

Permalink
Provide additional information in scripts. Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauer committed Mar 9, 2015
1 parent 12f8fdf commit 19d4a05
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[uochan/watchtower "0.1.4"]
[me.raynes/fs "1.4.6"]
[xmpp-clj "0.3.1"]
[jivesoftware/smackx "3.1.0"]
[clj-http "1.0.1"]
[com.taoensso/timbre "3.4.0"]
[sonian/carica "1.1.0" :exclusions [cheshire]]]
Expand Down
4 changes: 3 additions & 1 deletion scripts/hi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

(defn init
"Simple module for greating"
[msg] "Hello!")
[data]
(let [user (:from-name data)]
(str "Hello, " user)))
5 changes: 3 additions & 2 deletions scripts/weather.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@

; External API

(defn init [location]
(defn init [data]
(try
(let [json (fetch-weather location)
(let [location (:text data)
json (fetch-weather location)
data (get-in json [:query :results :channel :item :forecast])]
(report data))
(catch Exception e
Expand Down
2 changes: 1 addition & 1 deletion src/jabberjay/app.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


(defn -main [& args]
(script/init)
(script/init true)
(jabber/init))

;(-main)
6 changes: 3 additions & 3 deletions src/jabberjay/jabber.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
(defn- process-message [msg]
(or
(try
(when-let [text (:body msg)]
(when-let [text (string/trim (:body msg))]
(if-not (string/blank? text)
(do
(timbre/info "Received message:" text)
(script/execute (string/trim text)))))
(timbre/debug "Received message:" msg)
(script/execute msg))))
(catch Exception e
(timbre/error e)
(.getMessage e)))
Expand Down
49 changes: 29 additions & 20 deletions src/jabberjay/script.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [jabberjay.config :as config]
[clojure.string :as string]
[watchtower.core :as wt]
[me.raynes.fs :as fs]))
[me.raynes.fs :as fs]
[taoensso.timbre :as timbre]))


; Constants & options
Expand All @@ -14,7 +15,7 @@
(defn- conf-folder [] (fs/file (conf-wd :folder)))


; Internal API
; Internal API: Script engine

(defn- scan-scripts []
(let [dir (conf-folder)
Expand All @@ -30,27 +31,15 @@
scripts (map reload-script files)
res (zipmap names scripts)]
(reset! SCRIPTS res)
(println "Modules reloaded: " names)
(timbre/info "Modules reloaded:" names)
res))

(defn- fs-watcher [_] (reload-scripts))


; External API

(defn modules []
(keys @SCRIPTS))
; Internal API: Watchdog

(defn execute [text]
(let [args (string/split text #"\s")
cmd (string/lower-case (first args))
argv (string/trim (subs text (count cmd)))
script (get @SCRIPTS cmd)]
(if-not (nil? script)
(script argv))))
(defn- fs-watcher [_] (reload-scripts))

(defn init []
(reload-scripts)
(defn- init-fs-watcher []
(let [f (conf-folder)
d (conf-delay)]
(if (and
Expand All @@ -63,6 +52,26 @@
(wt/file-filter (wt/extensions :clj))
(wt/on-change fs-watcher)))))

;(init)

; External API

(defn modules []
(keys @SCRIPTS))

(defn execute [data]
(let [text (string/trim (:body data))
args (string/split text #"\s")
cmd (string/lower-case (first args))
argv (string/trim (subs text (count cmd)))
script (get @SCRIPTS cmd)
context (assoc data :text argv)]
(if-not (nil? script)
(script context))))

(defn init [wd]
(reload-scripts)
(if wd (init-fs-watcher)))

;(init true)
;(reload-scripts)
;(execute "hi")
;(execute {:body "Hi Bot!" :from-name "Bob"})
9 changes: 9 additions & 0 deletions test/jabberjay/script_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns jabberjay.script-test
(:require [clojure.test :refer :all]
[jabberjay.script :as script]))

(deftest script-execute
(testing "script: hi"
(script/init false)
(is (= "Hello, Bob" (script/execute {:body "hi" :from-name "Bob"})))
(is (not (empty? (script/execute {:body "weather Novosibirsk"}))))))

0 comments on commit 19d4a05

Please sign in to comment.