(require 'url)
(require 'url-cache)
+(eval-when-compile
+ (require 'subr-x))
(defgroup gravatar nil
"Gravatars."
"Base URL for getting gravatars.")
(defun gravatar-hash (mail-address)
- "Create a hash from MAIL-ADDRESS."
- (md5 (downcase mail-address)))
+ "Return the Gravatar hash for MAIL-ADDRESS."
+ ;; https://gravatar.com/site/implement/hash/
+ (md5 (downcase (string-trim mail-address))))
(defun gravatar-build-url (mail-address)
"Return a URL to retrieve MAIL-ADDRESS gravatar."
retrieval failed."
(let ((url (gravatar-build-url mail-address)))
(with-current-buffer (if (url-cache-expired url gravatar-cache-ttl)
- (url-retrieve-synchronously url)
+ (url-retrieve-synchronously url t)
(url-fetch-from-cache url))
(gravatar-retrieved () #'identity))))
This function is intended as a callback for `url-retrieve'."
(let ((data (unless (plist-get status :error)
(gravatar-get-data))))
- (and url-current-object ; Only cache if not already cached.
+ (and data ; Only cache on success.
+ url-current-object ; Only cache if not already cached.
gravatar-automatic-caching
(url-store-in-cache))
(prog1 (apply cb (if data (create-image data nil t) 'error) cbargs)
--- /dev/null
+;;; gravatar-tests.el --- tests for gravatar.el -*- lexical-binding: t -*-
+
+;; Copyright (C) 2019 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'gravatar)
+
+(ert-deftest gravatar-hash ()
+ "Test `gravatar-hash'."
+ (should (equal (gravatar-hash "") "d41d8cd98f00b204e9800998ecf8427e"))
+ (let ((hash "acbd18db4cc2f85cedef654fccc4a4d8"))
+ (should (equal (gravatar-hash "foo") hash))
+ (should (equal (gravatar-hash "foo ") hash))
+ (should (equal (gravatar-hash " foo") hash))
+ (should (equal (gravatar-hash " foo ") hash))))
+
+;;; gravatar-tests.el ends here