Skip to content

Commit

Permalink
Make it possible to fetch messages before a certain timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
fhd committed Apr 18, 2012
1 parent 2a06cd4 commit 36b209b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/flurfunk/server/routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
(defn- parse-message [s]
(ms/unmarshal-message (ms/parse-xml s)))

(defn- parse-timestamp [s]
(if (or (not s) (= s "NaN"))
nil
(Long. s)))

(defroutes main-routes
(GET "/" {uri :uri}
(response/redirect (str uri (if (not (.endsWith uri "/")) "/")
"index.html")))
(GET "/messages" {params :params}
(ms/marshal-messages
(let [since (:since params)]
(if (and since (not (= since "NaN")))
(storage/get-messages {:since (Long. since)})
(storage/get-messages)))))
(let [since (parse-timestamp (:since params))
before (parse-timestamp (:before params))
opts {:since since :before before}
opts (apply dissoc opts
(for [[k v] opts :when (nil? v)] k))]
(if (empty? opts)
(storage/get-messages)
(storage/get-messages opts)))))
(GET "/message/:id" [id]
(if-let [message (storage/find-message id)]
(ms/marshal-message message)
Expand Down
28 changes: 22 additions & 6 deletions test/flurfunk/server/test_routes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,28 @@
(let [messages (ms/unmarshal-messages
(http-get-xml "/messages" {:since "1000000000000"}))
message (first messages)]
(is (= (count messages) 1))
(are [v k] (= v (k message))
"foo" :body
"1337" :id
"thomas" :author
1000000000001 :timestamp))))
(and (is (= (count messages) 1))
(are [v k] (= v (k message))
"foo" :body
"1337" :id
"thomas" :author
1000000000001 :timestamp)))))

(deftest test-get-messages-before
(with-redefs [storage/get-messages
(fn ([])
([options] (if (= (:before options) 1000000000001)
[{:body "foo" :id "1337" :author "thomas"
:timestamp 1000000000000}])))]
(let [messages (ms/unmarshal-messages
(http-get-xml "/messages" {:before "1000000000001"}))
message (first messages)]
(and (is (= (count messages) 1))
(are [v k] (= v (k message))
"foo" :body
"1337" :id
"thomas" :author
1000000000000 :timestamp)))))

(deftest test-get-message-not-found
(with-redefs [storage/find-message (fn [id] nil)]
Expand Down

0 comments on commit 36b209b

Please sign in to comment.