Browse Source

refactoring + new tests

master
Christian Mueller 12 years ago
parent
commit
c80918f390
  1. 9
      src/notehub/api.clj
  2. 14
      src/notehub/handler.clj
  3. 8
      test/notehub/test/api.clj

9
src/notehub/api.clj

@ -46,7 +46,7 @@ @@ -46,7 +46,7 @@
(map #(+ (second %) (.get (Calendar/getInstance) (first %)))
{Calendar/YEAR 0, Calendar/MONTH 1, Calendar/DAY_OF_MONTH 0}))
(defn create-response
(defn- create-response
([success] {:success success})
([success message & params]
(assoc (create-response success) :message (apply format message params))))
@ -59,6 +59,13 @@ @@ -59,6 +59,13 @@
(str domain "/" (storage/create-short-url token {:year year :month month :day day :title title}))
(str domain (url year month day title))))))
(defn version-manager [f params]
(if-let [version (:version params)]
(f (if (and (:noteID params) (< (Double/parseDouble version) 1.3))
(assoc params :noteID (sreplace (params :noteID) #" " "/"))
params))
(create-response false "API version expected")))
(defn get-note [{:keys [noteID]}]
(if (storage/note-exists? noteID)
(let [note (storage/get-note noteID)]

14
src/notehub/handler.clj

@ -90,27 +90,19 @@ @@ -90,27 +90,19 @@
{:headers {"Content-Type" ctype}
:body content})
(defn version-manager [f params]
(generate-string
(if-let [version (:version params)]
(f (if (and (:noteID params) (< (Float/parseFloat version) 1.3))
(assoc params :noteID (sreplace (params :noteID) #" " "/"))
params))
(api/create-response false "API version expected"))))
(defroutes api-routes
(GET "/" [] (layout (get-message :api-title)
(md-node :article (slurp "API.md"))))
(GET "/note" {params :params}
(version-manager api/get-note params))
(generate-string (api/version-manager api/get-note params)))
(POST "/note" {params :params}
(version-manager api/post-note params))
(generate-string (api/version-manager api/post-note params)))
(PUT "/note" {params :params}
(version-manager api/update-note params)))
(generate-string (api/version-manager api/update-note params))))
(defroutes app-routes
(context "/api" [] api-routes)

8
test/notehub/test/api.clj

@ -121,6 +121,14 @@ @@ -121,6 +121,14 @@
(is (get-in body ["status" "success"]))
(is (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.0" :noteID noteID}))) "note")))
(is (= "API version expected" ((parse-string
(:body (send-request [:get "/api/note"] {:noteID noteID}))) "message")))
(is (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.1"
:noteID (clojure.string/replace noteID #"/" " ")}))) "note")))
(isnt (= note ((parse-string
(:body (send-request [:get "/api/note"] {:version "1.3"
:noteID (clojure.string/replace noteID #"/" " ")}))) "note")))
(is (do
(storage/delete-note noteID)
(not (storage/note-exists? noteID)))))))

Loading…
Cancel
Save