Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/diogok/clj-diogok-sandbox

My Clojure sandbox/utils namespaces and scripts on file, io, http and etcetera.
https://github.com/diogok/clj-diogok-sandbox

Last synced: about 2 months ago
JSON representation

My Clojure sandbox/utils namespaces and scripts on file, io, http and etcetera.

Awesome Lists containing this project

README

        

# clj-utils

This is my clojure scripts and namespaces utils and sandbox. Includes utilities for:
- http-get a resource
- Serialization/unserialization of objetcs
- Serialization/unserialization of objetcs to/from file
- md5sum (for hashing)
- file creation (return a file and create it and its parents dirs if needed)
- read only part of a file
- Key/Value tuple in file storage (fragile)
- Key/Value tuple in file storage2 (single file, append-only, little better)
- couchdb testing

## Usage

HTTP:

(let [response (http-get "http://github.com")]
(if (= (:response-code response) 200)
(println (:content response))
))
; contains :url :header :response-code :response-message and :content

SERIALIZATION:

(let [obj {:name "Diogok" :value ["[email protected]" "[email protected]"]}]
(def serial (serialize obj))
(= obj (unserialize serial));true
(let [file (str (md5 (:name obj)) ".txt")]
(save-to-file obj file)
(= (load-from-file file) obj);true
)
)

STORAGE (and storage2):

(let [db (bucket "foo/bar")]
(put! db "diogok" {:name "Diogo Souza da Silva" :github "http://github.com/diogok"})
(let [obj (tuple "foo" "bar")]
(put! db obj)
(= obj (get! db "foo")); true
)
)

COUCHDB

(require [clj-diogok-sandbox.couchdb :as couch])
(def server (couch/server "localhost" 5984))
(def db (couch/db server "testdb"))
(couch/put db {:_id "foo" :foo "bar"})
(couch/get db "foo") ;; {:_id "foo" :_rev "..." ...}
(couch/put db {:docs [{:foo "bar"},{:foo "foobar"}]}) ;; bulk insert
(def myview (couch/view db "my-design-doc" "my-view-name"))
(couch/query myview)
(couch/query myview "key")