Permalink
Browse files

Add route for retrieving comments for a particular namespace and method.

  • Loading branch information...
1 parent 236801e commit fe9ca7eeb6c75575e4923ab23fecba863730904b @dakrone dakrone committed Aug 24, 2010
Showing with 35 additions and 0 deletions.
  1. +6 −0 README.markdown
  2. +29 −0 src/cd_wsapi/core.clj
View
6 README.markdown
@@ -20,6 +20,12 @@ Searching for a function
curl "http://localhost:8080/search/map"
curl "http://localhost:8080/search/clojure.core/map"
+Searching for comments on a function
+------------------------------------
+
+ curl "http://localhost:8080/comments/clojure.contrib.json/read-json"
+
+
License
-------
View
29 src/cd_wsapi/core.clj
@@ -84,6 +84,32 @@
(perform-search qv))))
+(defn format-comment
+ "Given a comment, format it for json output."
+ [c]
+ (dissoc (into {} c) :subject :parent_id :lft :rgt :id :commentable_id :commentable_type :title))
+
+
+(defn get-comments
+ "Return the comments for a given namespace and method."
+ [ns name]
+ (fn [n]
+ {:status 200
+ :headers {"Content-Type" "application/json"}
+ :body (encode-to-str
+ (with-connection db
+ (transaction
+ (when-let [id (with-query-results
+ rs
+ ["select id from functions where ns = ? and name = ?" ns name]
+ (:id (first (doall rs))))]
+ (when-let [comments (with-query-results
+ rs
+ ["select * from comments where comments.commentable_id = ? " id]
+ (doall rs))]
+ (map format-comment comments))))))}))
+
+
(defn app-handler [channel request]
(enqueue-and-close
channel
@@ -92,12 +118,15 @@
["examples" ns name] (examples ns name)
["search" ns name] (search ns name)
["search" name] (search name)
+ ["comments" ns name] (get-comments ns name)
[&] default)
request)))
+
(defn app-wrapper [channel request]
(app-handler channel request))
+
(comment (def server (start-http-server app-wrapper {:port *server-port*}))

0 comments on commit fe9ca7e

Please sign in to comment.