Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Last active December 15, 2015 01:39
Show Gist options
  • Save glenjamin/548670fe5855c7943d81 to your computer and use it in GitHub Desktop.
Save glenjamin/548670fe5855c7943d81 to your computer and use it in GitHub Desktop.
Crazily indented clojure macro
(ns http)
(defn post [a b] "test")
(def ^:private http-extras
{:insecure? true, :debug true})
(let [orig http/post
f (fn [u r] (orig u (merge r http-extras)))]
(defmacro patch-http
"Monkey-patch HTTP requests to do stuff we want"
[& body]
`(with-redefs [http/post ~f] ~@body)))
(defn get-token
"Get an OAuth request token"
[callback]
(patch-http
(+ 1 2)))
@edw
Copy link

edw commented Mar 17, 2013

(ns http)

(defn post [a b] "test")

(def ^:private http-extras
  {:insecure? true, :debug true})

(let [orig-post http/post]
  (defmacro patch-http
    "Monkey-patch HTTP requests to do stuff we want"
    [& body]
    `(with-redefs [http/post #(~orig-post %1 (merge %2 http-extras))]
       ~@body)))

(defn get-token
  "Get an OAuth request token"
  [callback]
  (let [orig-post http/post]
    (with-redefs [http/post #(orig-post %1 (merge %2 http-extras))]
      (+ 1 2))))

(defn get-token-2
  "Get an OAuth request token"
  [callback]
  (patch-http (+ 1 2)))

@edw
Copy link

edw commented Mar 17, 2013

(ns http)

(defn post [a b] [a b])

(def ^:private http-extras
  {:insecure? true, :debug true})

(let [orig-post http/post]
  (defmacro patch-http
    "Monkey-patch HTTP requests to do stuff we want"
    [& body]
    `(with-redefs [http/post #(~orig-post %1 (merge %2 http-extras))]
       ~@body)))

(defn get-token
  "Get an OAuth request token"
  [callback]
  (let [orig-post http/post]
    (with-redefs [http/post #(orig-post %1 (merge %2 http-extras))]
      (post "/foo" {}))))

(defn get-token-2
  "Get an OAuth request token"
  [callback]
  (patch-http (post "/foo" {})))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment