Skip to content

Commit

Permalink
Created Return record in processor
Browse files Browse the repository at this point in the history
  • Loading branch information
johncowie committed Aug 1, 2013
1 parent 7d8ea09 commit 93ccedc
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/nuotl_twitter/processor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,58 @@
(re-pattern (url :url))
(html-url (url :display-url) (url :expanded-url))))

(defrecord Return [output success])
(defrecord Output [event tweeter message])

(defn- swap-in-urls [tweet output]
(let [urls (tweet :urls)]
(loop [i 0 text (:text (:event output))]
(if (< i (count urls))
(recur (inc i) (fix-text text (urls i)))
{:output (assoc-in output [:event :text] text) :success true}))))
(Return. (assoc-in output [:event :text] text) true)))))

(defn is-me? [tweet output]
(if (= (:_id (:tweeter tweet)) (:application-id tweet))
{:output output :success false}
{:output output :success true}))
(Return. output false)
(Return. output true)))

(defn get-tweeter [tweet output]
{:output (assoc output :tweeter (:tweeter tweet)) :success true})
(Return. (assoc output :tweeter (:tweeter tweet)) true))

(defn is-approved? [tweet output]
(if (contains?
(set (get-in (conf/config) [:twitter :authorised]))
(. (:name (:tweeter tweet)) toLowerCase))
{:output output :success true}
(if (dao/tweeter-approved? (:_id (:tweeter tweet)))
{:output output :success true}
{:output (assoc output :message :unapproved) :success false})))
(Return. output true)
(Return. (assoc output :message :unapproved) false))))

(defn is-mention? [tweet output]
(if (=
(str "@" (. (:application-screen-name tweet) toLowerCase))
(nth (clojure.string/split (:text tweet) #" ") 0))
{:output output :success true}
{:output output :success false}
(Return. output true)
(Return. output false)
))

(defn parse-tweet [tweet output]
(let [parse-result (tweet-parser/parse-tweet (:text tweet))]
(if (nil? (:event parse-result))
{:output (assoc output :message (:error parse-result)) :success false}
{:output (assoc output :event (merge (:event output) (:event parse-result))) :success true})))
(Return.(assoc output :message (:error parse-result)) false)
(Return.(assoc output :event (merge (:event output) (:event parse-result))) true))))

(defn url-present? [tweet output]
(if (empty? (:urls tweet))
{:output (merge output {:event nil :message :no-url}) :success false}
{:output output :success true}))
(Return. (merge output {:event nil :message :no-url}) false)
(Return. output true)))

(defn clean-up-event [tweet output]
(let [event (merge
(:event output)
(select-keys tweet [:_id :tags :in-response-to]))]
(let [event (assoc event :tweeter (:_id (:tweeter tweet)))]
{:output (merge output {:event event :message :success}) :success true})))
(Return. (merge output {:event event :message :success}) true))))

(def logic [
is-me?
Expand All @@ -74,7 +77,7 @@
])

(defn process-tweet [tweet]
(loop [i 0 ret {:output {:event nil :tweeter nil :message nil} :success true}]
(loop [i 0 ret (Return. {:event nil :tweeter nil :message nil} true)]
(if (and (:success ret) (< i (count logic)))
(recur (inc i) ((logic i) tweet (:output ret)))
(:output ret))))

0 comments on commit 93ccedc

Please sign in to comment.