Permalink
Browse files

Merge branch 'release/1.0'

  • Loading branch information...
2 parents 236801e + 2e68a26 commit 6bc2788d5088b34025c4f0f2b9a17ac813e0c9cd @dakrone dakrone committed Aug 25, 2010
Showing with 86 additions and 5 deletions.
  1. +10 −0 README.markdown
  2. +1 −1 project.clj
  3. +75 −4 src/cd_wsapi/core.clj
View
10 README.markdown
@@ -20,6 +20,16 @@ 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"
+
+Getting the 'see-also' functions
+--------------------------------
+
+ curl "http://localhost:8080/see-also/clojure.test/are"
+
License
-------
View
2 project.clj
@@ -1,4 +1,4 @@
-(defproject cd-wsapi "0.1.0-SNAPSHOT"
+(defproject cd-wsapi "0.1.0"
:description "ClojureDocs External API"
:dependencies [[org.clojure/clojure "1.2.0-RC1"]
[org.clojure/clojure-contrib "1.2.0-RC1"]
View
79 src/cd_wsapi/core.clj
@@ -29,6 +29,17 @@
:body "null"})
+(defn get-id
+ "Retrieve the id of a given namespace and method.
+ Must be called from within a transaction."
+ [ns name]
+ (when-let [id (with-query-results
+ rs
+ ["select id from functions where ns = ? and name = ?" ns name]
+ (:id (first (doall rs))))]
+ id))
+
+
(defn format-example
"Given an example, format the example for the API JSON output."
[example]
@@ -42,10 +53,7 @@
: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 [id (get-id ns name)]
(when-let [examples (with-query-results
rs
["select * from examples where function_id = ?" id]
@@ -84,6 +92,65 @@
(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 (get-id ns name)]
+ (when-let [comments (with-query-results
+ rs
+ ["select * from comments where comments.commentable_id = ? " id]
+ (doall rs))]
+ (map format-comment comments))))))}))
+
+
+(defn format-function
+ "Given a function, format it for json."
+ [function]
+ (dissoc (into {} function) :id :doc :source :shortdoc))
+
+
+(defn format-see-also
+ "Given an id, format the function to see also."
+ [id]
+ (with-connection db
+ (transaction
+ (when-let [functions (with-query-results
+ rs
+ ["select * from functions where id = ?" (:to_id id)]
+ (doall rs))]
+ (map format-function functions)))))
+
+
+(defn see-also
+ "Return the functions to see 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 (get-id ns name)]
+ (when-let [see-also-ids (with-query-results
+ rs
+ ["select to_id from see_alsos where from_id = ?" id]
+ (doall rs))]
+ (map format-see-also see-also-ids))))))}))
+
+
+
(defn app-handler [channel request]
(enqueue-and-close
channel
@@ -92,12 +159,16 @@
["examples" ns name] (examples ns name)
["search" ns name] (search ns name)
["search" name] (search name)
+ ["comments" ns name] (get-comments ns name)
+ ["see-also" ns name] (see-also 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 6bc2788

Please sign in to comment.