Commits

Steve Losh  committed 4c3261f

Clean up, and update for Clojure 1.3.

  • Participants
  • Parent commits 66ced2d
  • Tags v1.1.0

Comments (0)

Files changed (4)

File README.markdown

 
 Slap this in your `project.clj` `:dependencies`:
 
-    [postmark "1.0.0"]
+    [postmark "1.1.0"]
 
 ## Usage
 
-(defproject postmark "1.0.0"
+(defproject postmark "1.1.0"
   :description "Clojure bindings for http://postmarkapp.com/"
-  :dependencies [[org.clojure/clojure "1.2.1"]
-                 [cheshire "2.0.2"]
-                 [clj-http "0.2.1"]]
-  :dev-dependencies [[lein-marginalia "0.6.1"]])
+  :dependencies [[org.clojure/clojure "[1.2.1],[1.3.0]"]
+                 [cheshire "2.1.0"]
+                 [clj-http "0.3.0"]])

File src/postmark/core.clj

   (:use [cheshire.core :only (generate-string parse-string)]))
 
 
-;; # Internal Functions
-(defn- maybe-assoc [m k v]
-  (if v
-    (assoc m k v)
-    m))
+(defn- mail-to-json
+  "Return the JSON for the given mail map.
 
-(defn- parse-mail [mail]
-  (generate-string
-    (loop [m {}
-           ks (keys mail)
-           vs (vals mail)]
-      (if (seq ks)
-        (recur (maybe-assoc m (first ks) (first vs))
-               (rest ks)
-               (rest vs))
-        m))))
+  Falsey values will be removed from the map before serializing."
+  [mail]
+  (generate-string (into {} (filter second mail))))
 
 (defn- send-to-postmark [api-key mail]
   (let [resp (client/post "http://api.postmarkapp.com/email"
-                          {:basic-auth ["user" "pass"]
-                           :body (parse-mail mail)
+                          {:body (mail-to-json mail)
                            :headers {"X-Postmark-Server-Token" api-key}
                            :content-type :json
                            :accept :json})
         body (parse-string (:body resp))]
     (assoc resp :body body)))
 
-(defn- get-to-string [to]
+(defn- get-to-string
+  "Create a string appropriate for the to/cc/bcc fields in a Postmark call.
+
+  Can be passed a email address as a string like 'foo@bar.com' or
+  'Foo Bar <foo@bar.com>', or a seq of such strings."
+  [to]
   (when to
     (if (= java.lang.String (class to))
       to
       (join "," to))))
 
+(defn- no-more-than-20-recipients [to]
+  (or (= java.lang.String (class to))
+      (<= (count to) 20)))
+
+
 (defn- mail
   "Send an email with the Postmark API.
 
   Remember: Postmark only lets you send to at most twenty addresses at once."
-  [api-key from mail]
-  {:pre [(or (= java.lang.String (class (:to mail)))
-             (<= (count (:to mail)) 20))]}
+  [api-key from {:keys [to subject cc bcc tag text html reply-to]}]
+  {:pre [(no-more-than-20-recipients to)]}
   (send-to-postmark api-key {"From" from
-                             "To" (get-to-string (:to mail))
-                             "Subject" (:subject mail)
-                             "Cc" (get-to-string (:cc mail))
-                             "Bcc" (get-to-string (:bcc mail))
-                             "Tag" (:tag mail)
-                             "TextBody" (:text mail)
-                             "HtmlBody" (:html mail)
-                             "ReplyTo" (:reply-to mail)}))
+                             "To" (get-to-string to)
+                             "Subject" subject
+                             "Cc" (get-to-string cc)
+                             "Bcc" (get-to-string bcc)
+                             "Tag" tag
+                             "TextBody" text
+                             "HtmlBody" html
+                             "ReplyTo" reply-to}))
 
 
-;; # External API
 (defn postmark [api-key from]
   (partial mail api-key from))
 
 (defn postmark-test [from]
   (postmark "POSTMARK_API_TEST" from))
-

File test/postmark/test/core.clj

   (:use [cheshire.core :only (generate-string parse-string)])
   (:use [clojure.test]))
 
-(deftest test-maybe-assoc
-  (is (= (@#'postmark.core/maybe-assoc {:foo 1} :bar 2)
-         {:foo 1 :bar 2})
-      "maybe-assoc didn't work with a truthy value")
 
-  (is (= (@#'postmark.core/maybe-assoc {:foo 1} :bar nil)
-         {:foo 1})
-      "maybe-assoc didn't work with a falsy value"))
-
-(deftest test-parse-mail
-  (is (= (@#'postmark.core/parse-mail {"Subject" "foo" "Nothing" nil})
+(deftest test-mail-to-json
+  (is (= (@#'postmark.core/mail-to-json {"Subject" "foo" "Nothing" nil})
          (generate-string {"Subject" "foo"}))
       "parse mail"))
 
   (is (= (@#'postmark.core/get-to-string nil)
          nil)
       "get-to-string didn't handle nil")
+
   (is (= (@#'postmark.core/get-to-string "foo")
          "foo")
       "get-to-string didn't pass a string through")