Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6187e9b
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pom.xml | ||
*jar | ||
lib | ||
classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
) | ||
) | ||
) |