Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 6187e9be9c611adac23d8323e9e179027eb84222 @liquidz committed Jan 7, 2011
Showing with 138 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +18 −0 README
  3. +7 −0 project.clj
  4. +70 −0 src/clj_gravatar/core.clj
  5. +39 −0 test/clj_gravatar/test/core.clj
4 .gitignore
@@ -0,0 +1,4 @@
+pom.xml
+*jar
+lib
+classes
18 README
@@ -0,0 +1,18 @@
+# clj-gravatar
+
+gravatar api wrapper for clojure
+http://ja.gravatar.com/
+
+## Usage
+
+### gravatar image
+(gravatar-image "your@mail.address")
+
+### gravatar profile
+(gravatar-profile "your@mail.address")
+
+## License
+
+Copyright (C) 2011 Masashi Iizuka
+
+Distributed under the Eclipse Public License, the same as Clojure.
7 project.clj
@@ -0,0 +1,7 @@
+(defproject
+ org.clojars.liquidz/clj-gravatar "0.0.1"
+ :description "gravatar api wrapper for clojure"
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ ]
+ )
70 src/clj_gravatar/core.clj
@@ -0,0 +1,70 @@
+(ns clj-gravatar.core
+ (:require
+ [clojure.contrib.string :as string]
+ [clojure.contrib.io :as io]
+ [clojure.contrib.json :as json]
+ )
+ (:import
+ [java.net URLEncoder]
+ [java.security MessageDigest]
+ )
+ )
+
+; util {{{
+(defn bytes->hex-str [bytes]
+ (apply str (map #(string/tail 2 (str "0" (Integer/toHexString (bit-and 0xff %)))) bytes))
+ )
+(defn digest-hex [algorithm s]
+ (if-not (string/blank? s)
+ (-> (MessageDigest/getInstance algorithm) (.digest (.getBytes s)) bytes->hex-str)
+ )
+ )
+(def str->md5 (partial digest-hex "MD5"))
+(defn- encode [x] (if (string? x) (URLEncoder/encode x) x))
+; }}}
+
+(defn- gravatar-url [secure?]
+ (if secure?
+ "https://secure.gravatar.com/"
+ "http://www.gravatar.com/"
+ )
+ )
+
+; =map->get-parameter
+(defn map->get-parameter
+ "convert map to GET parameter"
+ [m]
+ (let [ls (map (fn [[k v]] (str (name k) "=" (encode v)))
+ (remove #(-> % second nil?) m))]
+ (if-not (empty? ls)
+ (str "?" (string/join "&" ls))
+ )
+ )
+ )
+
+; =gravatar-image
+(defn gravatar-image
+ "get gravatar image url from mail address"
+ [mail-address & {:keys [size default secure?] :or {size nil, default nil, secure? false}}]
+ (if-not (string/blank? mail-address)
+ (str (gravatar-url secure?)
+ "avatar/"
+ (str->md5 mail-address)
+ (map->get-parameter {:s size :d default})
+ )
+ )
+ )
+
+; =gravatar-profile
+(defn gravatar-profile
+ "get gravatar profile from mail address"
+ [mail-address & {:keys [secure?] :or {secure? false}}]
+ (if-not (string/blank? mail-address)
+ (let [url (str (gravatar-url secure?) (str->md5 mail-address) ".json")]
+ (try
+ (json/read-json (apply str (io/read-lines url)))
+ (catch Exception e nil)
+ )
+ )
+ )
+ )
39 test/clj_gravatar/test/core.clj
@@ -0,0 +1,39 @@
+(ns clj-gravatar.test.core
+ (:use [clj-gravatar.core] :reload)
+ (:use [clojure.test]))
+
+; http://ja.gravatar.com/site/implement/profiles/json/
+(def *test-address* "beau@dentedreality.com.au")
+
+(deftest test-map->get-parameter
+ (are [x y] (= x y)
+ nil (map->get-parameter {})
+ "?a=1" (map->get-parameter {:a 1})
+ "?a=1" (map->get-parameter {:a 1 :b nil})
+ "?a=1&b=2" (map->get-parameter {:a 1 :b 2})
+ "?a=1&b=2" (map->get-parameter {:a 1 :b 2 :c nil})
+ "?s=http%3A%2F%2Fhoge.com%2F" (map->get-parameter {:s "http://hoge.com/"})
+ )
+ )
+
+(deftest test-gravatar-image
+ (are [x y] (= x y)
+ "http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50"
+ (gravatar-image *test-address*)
+
+ "https://secure.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50"
+ (gravatar-image *test-address* :secure? true)
+ )
+ )
+
+(deftest test-gravatar-profile
+ (is (nil? (gravatar-profile "NOTFOUNDUSER")))
+ (let [profile (gravatar-profile *test-address*)]
+ (is profile)
+ (are [x y] (= x y)
+ "1428" (-> profile :entry first :id)
+ "205e460b479e2e5b48aec07710c08d50" (-> profile :entry first :hash)
+ *test-address* (-> profile :entry first :emails first :value)
+ )
+ )
+ )

0 comments on commit 6187e9b

Please sign in to comment.