aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2024-10-22 23:39:42 +0200
committerOscar Najera <hi@oscarnajera.com>2024-10-22 23:39:42 +0200
commite75ba059ab8bd87ff95247e4d958277f50fde3cd (patch)
tree1626083b82e241a1cfa329c1ef46b620580611b4
parentc9946684cb6e38c9a7c3353bc8e769ca3b958770 (diff)
downloaddotfiles-e75ba059ab8bd87ff95247e4d958277f50fde3cd.tar.gz
dotfiles-e75ba059ab8bd87ff95247e4d958277f50fde3cd.tar.bz2
dotfiles-e75ba059ab8bd87ff95247e4d958277f50fde3cd.zip
grocy new product
-rw-r--r--elisp/grocy.el40
1 files changed, 39 insertions, 1 deletions
diff --git a/elisp/grocy.el b/elisp/grocy.el
index 6001a70..2129113 100644
--- a/elisp/grocy.el
+++ b/elisp/grocy.el
@@ -9,7 +9,7 @@
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/titan/grocy
-;; Package-Requires: ((emacs "27.1"))
+;; Package-Requires: ((emacs "29.1"))
;;
;; This file is not part of GNU Emacs.
;;
@@ -21,6 +21,8 @@
(require 'url)
(require 'dash)
+(require 'yaml)
+(require 'yaml-mode)
;; Silence byte-compiler.
(defvar url-http-end-of-headers)
@@ -186,6 +188,15 @@ Set METHOD to other request types and include the already encoded DATA."
(json-encode)
(encode-coding-string 'utf-8)))))
+(defun grocy-buffer-yaml->json (buffer)
+ "Read the YAML on BUFFER and encode it to json."
+ (with-current-buffer buffer
+ (thread-first
+ (buffer-string)
+ (yaml-parse-string)
+ (json-encode)
+ (encode-coding-string 'utf-8))) )
+
(defun grocy-update-object (object-endpoint)
"Update on the OBJECT-ENDPOINT with the Lisp object on buffer."
(grocy--request
@@ -196,5 +207,32 @@ Set METHOD to other request types and include the already encoded DATA."
"PUT"
(grocy-buffer-obj->json (current-buffer))))
+(defun grocy-new-product ()
+ (interactive)
+ (with-current-buffer (get-buffer-create "* Grocy new product *")
+ (erase-buffer)
+ (yaml-mode)
+ (keymap-local-set
+ "C-c C-c"
+ (lambda () (interactive)
+ (grocy--request
+ "objects/products"
+ (lambda (status)
+ (message "%S\n%S" status (buffer-string)))
+ "POST"
+ (grocy-buffer-yaml->json (current-buffer)))))
+ (insert "name: \n# ")
+ (maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations)
+ (insert "\nlocation_id: \n\# ")
+ (maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-units)
+ (insert "\nqu_id_stock: 3
+qu_id_purchase: 3
+qu_id_consume: 3
+qu_id_price: 3
+
+min_stock_amount: 0
+default_best_before_days: -1"))
+ (grocy-buffer-yaml->json (current-buffer)))
+
(provide 'grocy)
;;; grocy.el ends here