Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Jan 7, 2011
0 parents commit 6187e9b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
pom.xml
*jar
lib
classes
18 changes: 18 additions & 0 deletions 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 changes: 7 additions & 0 deletions 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 changes: 70 additions & 0 deletions 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 changes: 39 additions & 0 deletions 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.