Skip to content

Instantly share code, notes, and snippets.

View weavejester's full-sized avatar

James Reeves weavejester

View GitHub Profile
;; I'd begin by using protocols to define the I/O boundaries of the endpoint.
;; The protocols can be extended to support the database and mailer component
;; when in production, and for testing a mock implementation can be used instead.
(defprotocol UsersDatabase
(authenticate [db credentials])
(create-user [db user]))
(defprotocol Mailer
(send-email [mailer email]))
(require '[clojure.string :as str])
(defn parse-ints [line]
(for [s (str/split line #" ")] (Integer/parseInt s)))
(defn read-service-data []
(let [[n t] (parse-ints (read-line))]
{:widths (parse-ints (read-line))
:tests (->> (repeatedly read-line) (take n) (map parse-ints))}))
@weavejester
weavejester / gist:3736775
Created September 17, 2012 11:25 — forked from sunkencity/gist:3736683
compojure test
(def action-show
"Action that shows the help and about page."
(GET "/help" [account member]
(normal-layout account
[:div
[:h1 "Title"]
[:p "lorem"]])
(defroutes app-routes
index/action-show
(defn with-mc*
"Manages the memcache connection"
[mchosts func]
(let [^MemcachedClient con (get-connection mchosts)]
(debug "set up new connection")
(Thread/sleep 1) ; fix to workaround bug #...
(binding [*db* (assoc *db* :connection con :level 0)]
(try
(func)
(finally (.shutdown con 5 TimeUnit/SECONDS)))))
(defn has-access? [id password]
(fetch-one :pads :where {:name id :password password}))
(defpage "/:id" {:keys [id password]}
(if (has-access? id password)
(common/layout id (notepad id))
(do (insert! :pads {:name id})
(resp/redirect "/"))))
(ns demo.core
(:use [ring.adapter.jetty :only (run-jetty)]
[ring.middleware.session :only (wrap-session)]
[ring.middleware.flash :only (wrap-flash)]
[ring.middleware.stacktrace :only (wrap-stacktrace)]
[hiccup.core :only (html)]
[hiccup.form-helpers :only (form-to label text-field submit-button)]
[hiccup.page-helpers :only (xhtml unordered-list)]
[compojure.core :only (defroutes GET POST)]
[compojure.route :only (not-found)]
(ns sandbox.file-upload
(:use ring.adapter.jetty
ring.util.response
ring.middleware.multipart-params
ring.middleware.stacktrace
hiccup.core
hiccup.form-helpers
compojure.core
clojure.pprint))
(defn twitter-status
[tweet]
[:p.tweet
[:div.tweet-text (tweet "text")]
[:div.tweet-user (get-in tweet ["user" "name"])]])
(ns flockr
(:use compojure )
(:use flockr.template)) ; looks for flockr/template.clj
(ns flockr.template
(:use compojure))
(defn page
[title body]
(html
[:html
[:head
[:title title]]
[:body