utils.lisp 3.1 KB
Newer Older
ieslick's avatar
ieslick committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
(in-package :twitter)

;;
;; Support for parsing element records
;;

(defun get-value (name object)
  (awhen (assoc name object)
    (cdr it)))
(defun set-value (value name object) 
  (setf (cdr (assoc name object)) value))
(defsetf get-value set-value)

;;
;; Convenient symbol conversions
;;

(defvar *lisp->twitter-symbols* (make-hash-table :test 'eq)
  "Maintain conversions between _ and - forms for simplicity")
(defvar *twitter->lisp-symbols* (make-hash-table :test 'eq)
  "Maintain conversions between _ and - forms for simplicity")

(defun lisp->twitter (sym)
  (aif (gethash sym *lisp->twitter-symbols*) it
       sym))

(defun lisp->twitter-plist (plist)
  (loop for elt in plist 
       for i from 1 collect
       (if (oddp i) 
	   (lisp->twitter elt)
	   elt)))

(defun twitter->lisp (sym)
  (aif (gethash sym *twitter->lisp-symbols*) it
       sym))

(defun twitter->lisp-alist (alist)
  (loop for cell in alist do
       (setf (car cell) (twitter->lisp (car cell))))
  alist)

(defun maybe-add-conversion (lisp-sym)
  "Add a conversion between _ and - forms of argument symbols"
  (let* ((lisp-key (as-keyword lisp-sym))
	 (lisp-name (symbol-name lisp-key)))
    (when (find #\- lisp-name)
      (let ((twitter-sym (convert-to-twitter lisp-name)))
	(setf (gethash lisp-key *lisp->twitter-symbols*) twitter-sym
	      (gethash twitter-sym *twitter->lisp-symbols*) lisp-key)))))

(defun convert-to-twitter (string)
  (intern (string-upcase (substitute #\_ #\-  (copy-seq string))) :keyword))

ieslick's avatar
ieslick committed
55 56 57 58 59 60
(defun rem-keywords (list keywords)
  "Remove keywords from a keylist"
  (loop for (value indicator) on list by #'cddr
	unless (member value keywords)
	  nconc (list value indicator)))

ieslick's avatar
ieslick committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

;;
;; Property list utilities
;;

(defun plist->alist (plist)
  (when (valid-plist plist)
    (cons (cons (first plist) (second plist))
	  (plist->alist (cddr plist)))))

(defun alist->plist (alist)
  (when (consp alist)
    (cons (caar alist)
	  (cons (cdar alist)
		(alist->plist (cdr alist))))))

(defun plist->uri-params (plist &optional escape-p)
  (when (valid-plist plist)
    (cons (string-downcase (as-string (first plist)))
	  (cons (to-uri-param (second plist) escape-p)
		(plist->uri-params (cddr plist))))))

(defun plist-keywords (plist)
  (when (valid-plist plist)
    (cons (first plist) (plist-keywords (cddr plist)))))

(defun valid-plist (plist)
  (and (consp plist) (consp (cdr plist))))

(defun strip-keyword (keyword plist)
  (when (valid-plist plist)
    (if (eq (car plist) keyword)
	(strip-keyword keyword (cddr plist))
	(cons (first plist)
	      (cons (second plist)
		    (strip-keyword keyword (cddr plist)))))))

;;
;; Other
;;

(defun as-string (arg)
  (typecase arg
    (string arg)
    (symbol (symbol-name arg))))

(defun as-keyword (arg)
  (typecase arg
    (string (intern arg :keyword))
    (symbol (intern (symbol-name arg) :keyword))))

ieslick's avatar
ieslick committed
112
(defun localize-symbol (sym &optional (package *package*))
ieslick's avatar
ieslick committed
113
  (typecase sym
ieslick's avatar
ieslick committed
114 115
    (string (intern sym package))
    (symbol (intern (symbol-name sym) package))))
ieslick's avatar
ieslick committed
116 117 118 119 120 121

(defun to-uri-param (arg escape-p)
  (if escape-p 
      (trivial-http:escape-url-query 
       (princ-to-string arg))
      (princ-to-string arg)))