emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master b4b1eda 3/4: Fix some minor gravatar.el issues


From: Basil L. Contovounesios
Subject: [Emacs-diffs] master b4b1eda 3/4: Fix some minor gravatar.el issues
Date: Fri, 2 Aug 2019 09:37:10 -0400 (EDT)

branch: master
commit b4b1eda7fbf4c4f3fa6377bd18d1d1a22e6e4b42
Author: Basil L. Contovounesios <address@hidden>
Commit: Basil L. Contovounesios <address@hidden>

    Fix some minor gravatar.el issues
    
    For discussion, see the following thread:
    https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html
    * lisp/image/gravatar.el (gravatar-hash): Trim leading and trailing
    whitespace in given address, as per the Gravatar docs.
    (gravatar-retrieve-synchronously): Silence call to
    url-retrieve-synchronously for consistency with gravatar-retrieve.
    (gravatar-retrieved): Only cache buffer on successful retrieval.
    * test/lisp/image/gravatar-tests.el: New file.
---
 lisp/image/gravatar.el            | 12 ++++++++----
 test/lisp/image/gravatar-tests.el | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el
index fb539bc..52fd875 100644
--- a/lisp/image/gravatar.el
+++ b/lisp/image/gravatar.el
@@ -26,6 +26,8 @@
 
 (require 'url)
 (require 'url-cache)
+(eval-when-compile
+  (require 'subr-x))
 
 (defgroup gravatar nil
   "Gravatars."
@@ -76,8 +78,9 @@ Valid sizes range from 1 to 2048 inclusive."
   "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."
@@ -114,7 +117,7 @@ Value is either an image descriptor, or the symbol `error' 
if the
 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))))
 
@@ -125,7 +128,8 @@ an image descriptor, or the symbol `error' on failure.
 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)
diff --git a/test/lisp/image/gravatar-tests.el 
b/test/lisp/image/gravatar-tests.el
new file mode 100644
index 0000000..e6239da
--- /dev/null
+++ b/test/lisp/image/gravatar-tests.el
@@ -0,0 +1,34 @@
+;;; 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]