Skip to content

Commit

Permalink
Compojure: Clamp query count between 1 and 500 to comply with test spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith R. Gustafson committed Apr 23, 2013
1 parent b18adea commit 34473d5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions compojure/hello/src/hello/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,22 @@
queries ; Number of queries to run
(repeatedly get-world-raw)))))

(defn get-query-count [queries]
"Parse provided string value of query count, clamping values to between 1 and 500."
(let [q (try (Integer/parseInt queries)
(catch Exception e 1))] ; default to 1 on parse failure
(if (> q 500)
500 ; clamp to 500 max
(if (< q 1)
1 ; clamp to 1 min
q)))) ; otherwise use provided value

; Define route handlers
(defroutes app-routes
(GET "/" [] "Hello, World!")
(GET "/json" [] (response {:message "Hello, World!"}))
(GET "/db/:queries" [queries] (response (run-queries (Integer/parseInt queries))))
(GET "/dbraw/:queries" [queries] (response (run-queries-raw (Integer/parseInt queries))))
(GET "/db/:queries" [queries] (response (run-queries (get-query-count queries))))
(GET "/dbraw/:queries" [queries] (response (run-queries-raw (get-query-count queries))))
(route/not-found "Not Found"))

; Format responses as JSON
Expand Down

0 comments on commit 34473d5

Please sign in to comment.