Permalink
Browse files

Add API for getting problem data. Fixes #147.

  • Loading branch information...
1 parent a63175e commit 1b4f9d4e38fb44bd6201b166c7b6a701a87cef9b @amalloy amalloy committed Oct 5, 2011
Showing with 49 additions and 18 deletions.
  1. +2 −1 project.clj
  2. +18 −0 src/foreclojure/api.clj
  3. +2 −0 src/foreclojure/core.clj
  4. +8 −8 src/foreclojure/problems.clj
  5. +13 −3 src/foreclojure/ring.clj
  6. +6 −6 src/foreclojure/users.clj
View
@@ -8,7 +8,8 @@
[sandbar "0.4.0-SNAPSHOT"]
[org.clojars.christophermaier/congomongo "0.1.4-SNAPSHOT"]
[org.jasypt/jasypt "1.7"]
- [useful "0.7.0-beta1"]
+ [cheshire "2.0.2"]
+ [useful "0.7.0-beta5"]
[amalloy/ring-gzip-middleware "[0.1.0,)"]
[clj-github "1.0.1"]
[ring "0.3.7"]
View
@@ -0,0 +1,18 @@
+(ns foreclojure.api
+ (:require [cheshire.core :as json])
+ (:use [foreclojure.ring :only [wrap-json wrap-debug]]
+ [foreclojure.utils :only [as-int]]
+ [compojure.core :only [routes GET]]
+ [somnium.congomongo :only [fetch-one]]
+ [useful.map :only [update-each]]))
+
+(def api-routes
+ (-> (routes
+ (GET "/api/problem/:id" [id]
+ (when-let [problem (fetch-one :problems :where {:_id (as-int id)
+ :approved true})]
+ (-> problem
+ (dissoc :_id :approved)
+ (update-each [:restricted :tags]
+ #(or % ()))))))
+ (wrap-json)))
View
@@ -5,6 +5,7 @@
[sandbar.stateful-session :as session])
(:use [compojure.core :only [defroutes routes GET]]
[foreclojure.static :only [static-routes welcome-page]]
+ [foreclojure.api :only [api-routes]]
[foreclojure.problems :only [problems-routes]]
[foreclojure.login :only [login-routes]]
[foreclojure.register :only [register-routes]]
@@ -42,6 +43,7 @@
social-routes
version-routes
graph-routes
+ api-routes
golf-routes)
((if (:wrap-reload config)
#(wrap-reload % '(foreclojure.core))
@@ -2,7 +2,8 @@
(:require [foreclojure.users :as users]
[sandbar.stateful-session :as session]
[clojure.string :as s]
- [ring.util.response :as response])
+ [ring.util.response :as response]
+ [cheshire.core :as json])
(:import [org.apache.commons.mail EmailException])
(:use [foreclojure.utils :only [from-mongo get-user get-solved login-link flash-msg flash-error row-class approver? can-submit? send-email image-builder with-user as-int maybe-update escape-html]]
[foreclojure.ring-utils :only [*url*]]
@@ -19,8 +20,7 @@
[hiccup.core :only [html]]
[useful.debug :only [?]]
[amalloy.utils :only [defcomp]]
- [compojure.core :only [defroutes GET POST]]
- [clojure.contrib.json :only [json-str]]))
+ [compojure.core :only [defroutes GET POST]]))
(def solved-stats (agent {:total 0}))
@@ -249,11 +249,11 @@ Return a map, {:message, :error, :url, :num-tests-passed}."
(defn rest-run-code [id raw-code]
(let [{:keys [message error url num-tests-passed]} (run-code id raw-code)]
- (json-str {:failingTest num-tests-passed
- :message message
- :error error
- :golfScore (html (render-golf-score))
- :golfChart (html (render-golf-chart))})))
+ (json/generate-string {:failingTest num-tests-passed
+ :message message
+ :error error
+ :golfScore (html (render-golf-score))
+ :golfChart (html (render-golf-chart))})))
(defn wants-no-javascript-codebox? []
(when (session/session-get :user)
View
@@ -1,9 +1,10 @@
(ns foreclojure.ring
(:require [clojure.java.io :as io]
[clojure.string :as s]
- [compojure.route :as route])
+ [compojure.route :as route]
+ [cheshire.core :as json])
(:import [java.net URL])
- (:use [compojure.core :only [GET]]
+ (:use [compojure.core :only [GET routes]]
[foreclojure.version-utils :only [strip-version-number]]
[foreclojure.ring-utils :only [get-host]]
[useful.debug :only [?]]
@@ -39,10 +40,19 @@
(assoc-in resp [:headers "Cache-control"]
"public, max-age=31536000"))))
-(defn wrap-debug [handler]
+(defn wrap-debug [handler label]
(fn [request]
+ (println "In" label)
(? (handler (? request)))))
+(let [content-type [:headers "Content-Type"]]
+ (defn wrap-json [handler]
+ (fn [request]
+ (when-let [resp (handler request)]
+ (-> resp
+ (assoc-in content-type "application/json")
+ (update-in [:body] json/generate-string))))))
+
(defn split-hosts [host-handlers]
(let [default (:default host-handlers)]
(fn [request]
View
@@ -1,16 +1,16 @@
(ns foreclojure.users
(:require [ring.util.response :as response]
[clojure.string :as string]
- [sandbar.stateful-session :as session])
+ [sandbar.stateful-session :as session]
+ [cheshire.core :as json])
(:use [foreclojure.utils :only [from-mongo row-class rank-class get-user with-user]]
[foreclojure.template :only [def-page content-page]]
[foreclojure.ring-utils :only [*http-scheme* static-url]]
[foreclojure.config :only [config repo-url]]
[somnium.congomongo :only [fetch-one fetch update!]]
[compojure.core :only [defroutes GET POST]]
[hiccup.form-helpers :only [form-to hidden-field]]
- [hiccup.page-helpers :only [link-to]]
- [clojure.contrib.json :only [json-str]])
+ [hiccup.page-helpers :only [link-to]])
(:import org.apache.commons.codec.digest.DigestUtils
java.net.URLEncoder))
@@ -229,9 +229,9 @@
(defn rest-follow-user [username follow?]
(follow-user username follow?)
- (json-str {"following" follow?
- "next-action" (follow-url username (not follow?))
- "next-label" (if follow? "Unfollow" "Follow")}))
+ (json/generate-string {"following" follow?
+ "next-action" (follow-url username (not follow?))
+ "next-label" (if follow? "Unfollow" "Follow")}))
(defn set-disable-codebox [disable-flag]
(with-user [{:keys [_id]}]

0 comments on commit 1b4f9d4

Please sign in to comment.