Permalink
Browse files

Clean up, and update for Clojure 1.3.

  • Loading branch information...
1 parent 742066a commit 9f859e06c40e7724ef8fec40759e136faf23aa73 @sjl sjl committed Feb 2, 2012
Showing with 35 additions and 47 deletions.
  1. +1 −1 README.markdown
  2. +4 −5 project.clj
  3. +27 −31 src/postmark/core.clj
  4. +3 −10 test/postmark/test/core.clj
View
@@ -12,7 +12,7 @@ Clojure.
Slap this in your `project.clj` `:dependencies`:
- [postmark "1.0.0"]
+ [postmark "1.1.0"]
## Usage
View
@@ -1,6 +1,5 @@
-(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"]])
View
@@ -4,61 +4,57 @@
(: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))
-
@@ -3,24 +3,17 @@
(: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"))
(deftest test-get-to-string
(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")

0 comments on commit 9f859e0

Please sign in to comment.