emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master cf569e5 2/4: DRY in gravatar.el


From: Basil L. Contovounesios
Subject: [Emacs-diffs] master cf569e5 2/4: DRY in gravatar.el
Date: Fri, 2 Aug 2019 09:37:10 -0400 (EDT)

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

    DRY in gravatar.el
    
    For discussion, see the following thread:
    https://lists.gnu.org/archive/html/emacs-devel/2019-07/msg00528.html
    * lisp/image/gravatar.el (gravatar-data->image): Remove.
    (gravatar-retrieve, gravatar-retrieve-synchronously): Reuse
    url-fetch-from-cache and gravatar-retrieved to reduce duplication.
    (gravatar-retrieved): Only cache buffer if url-current-object is
    non-nil and return result of callback.  This affords reusing this
    function in cached URL buffers.
---
 lisp/image/gravatar.el | 48 +++++++++++++++++-------------------------------
 1 file changed, 17 insertions(+), 31 deletions(-)

diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el
index ea746b7..fb539bc 100644
--- a/lisp/image/gravatar.el
+++ b/lisp/image/gravatar.el
@@ -95,14 +95,6 @@ Valid sizes range from 1 to 2048 inclusive."
          (search-forward "\n\n" nil t)
          (buffer-substring (point) (point-max)))))
 
-(defun gravatar-data->image ()
-  "Get data of current buffer and return an image.
-If no image available, return 'error."
-  (let ((data (gravatar-get-data)))
-    (if data
-       (create-image data nil t)
-      'error)))
-
 ;;;###autoload
 (defun gravatar-retrieve (mail-address callback &optional cbargs)
   "Asynchronously retrieve a gravatar for MAIL-ADDRESS.
@@ -112,11 +104,8 @@ where GRAVATAR is either an image descriptor, or the symbol
   (let ((url (gravatar-build-url mail-address)))
     (if (url-cache-expired url gravatar-cache-ttl)
         (url-retrieve url #'gravatar-retrieved (list callback cbargs) t)
-      (apply callback
-             (with-temp-buffer
-               (url-cache-extract (url-cache-create-filename url))
-               (gravatar-data->image))
-             cbargs))))
+      (with-current-buffer (url-fetch-from-cache url)
+        (gravatar-retrieved () callback cbargs)))))
 
 ;;;###autoload
 (defun gravatar-retrieve-synchronously (mail-address)
@@ -124,26 +113,23 @@ where GRAVATAR is either an image descriptor, or the 
symbol
 Value is either an image descriptor, or the symbol `error' if the
 retrieval failed."
   (let ((url (gravatar-build-url mail-address)))
-    (if (url-cache-expired url gravatar-cache-ttl)
-        (with-current-buffer (url-retrieve-synchronously url)
-         (when gravatar-automatic-caching
-            (url-store-in-cache (current-buffer)))
-          (prog1 (gravatar-data->image)
-            (kill-buffer (current-buffer))))
-      (with-temp-buffer
-        (url-cache-extract (url-cache-create-filename url))
-        (gravatar-data->image)))))
+    (with-current-buffer (if (url-cache-expired url gravatar-cache-ttl)
+                             (url-retrieve-synchronously url)
+                           (url-fetch-from-cache url))
+      (gravatar-retrieved () #'identity))))
 
 (defun gravatar-retrieved (status cb &optional cbargs)
-  "Callback function used by `gravatar-retrieve'."
-  ;; Store gravatar?
-  (when gravatar-automatic-caching
-    (url-store-in-cache (current-buffer)))
-  (if (plist-get status :error)
-      ;; Error happened.
-      (apply cb 'error cbargs)
-    (apply cb (gravatar-data->image) cbargs))
-  (kill-buffer (current-buffer)))
+  "Handle Gravatar response data in current buffer.
+Return the result of (apply CB DATA CBARGS), where DATA is either
+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.
+         gravatar-automatic-caching
+         (url-store-in-cache))
+    (prog1 (apply cb (if data (create-image data nil t) 'error) cbargs)
+      (kill-buffer))))
 
 (provide 'gravatar)
 



reply via email to

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