Permalink
Browse files

Refactoring, improving recorder, using immutable hash map instead of …

…global atom.
  • Loading branch information...
1 parent b4544a5 commit 501f1ec00544504836941f6266ad849419dd4fff @ifesdjeen committed Jun 7, 2012
Showing with 64 additions and 23 deletions.
  1. +7 −17 src/vcr_clj/core.clj
  2. +36 −0 src/vcr_clj/recorder.clj
  3. +0 −6 test/vcr_clj/core_test.clj
  4. +21 −0 test/vcr_clj/recorder_test.clj
View
@@ -1,5 +1,6 @@
(ns vcr-clj.core
- (:require [clj-yaml.core :as yaml]))
+ (:require [clj-yaml.core :as yaml]
+ [vcr-clj.recorder :as recorder]))
(defn wrap-fn
"Wrap or replace some function with your own function"
@@ -10,24 +11,13 @@
(fn [& caller-arguments]
(wrapper caller-arguments original-fn)))))
-(defonce recorded-requests (atom {}))
-
-(defn record-request
- ""
- [options response]
- (swap! recorded-requests assoc options response))
-
-(defn request-recorded?
- [options]
- (some #(= options %) (keys @recorded-requests)))
-
-(defn persist-recorded!
- []
- (io!
- (spit "/Users/alexp/p/vcr-clj/cassete.yml" (yaml/generate-string @recorded-requests))))
+;; (defn persist-recorded!
+;; []
+;; (io!
+;; (spit "cassete.yml" (yaml/generate-string @recorded-requests))))
(defn recorder
[caller-arguments original-fn]
(let [original-result (apply original-fn caller-arguments)]
- (record-request (first caller-arguments) original-result)
+ ;; (recorder/record-request! (first caller-arguments) original-result)
original-result))
View
@@ -0,0 +1,36 @@
+(ns vcr-clj.recorder)
+
+(declare ^:dynamic *request-store*)
+
+(defprotocol VcrStore
+ (record-request! [store key value] "")
+ (get-request [store key] "")
+ (get-all [store] "")
+ (request-recorded? [store key] "")
+ (clear-recorded! [store]))
+
+(deftype VcrMemoryStore [store-map]
+ VcrStore
+ (record-request!
+ [_ options response]
+ (clojure.core/swap! store-map assoc options response))
+
+ (get-request
+ [_ key]
+ (clojure.core/get @store-map key))
+
+ (request-recorded?
+ [_ options]
+ (some #(= options %) (keys @store-map)))
+
+ (clear-recorded!
+ [_]
+ (reset! store-map {}))
+
+ (get-all
+ [_]
+ store-map))
+
+(defn vcr-memory-store
+ ([] (vcr-memory-store (atom {})))
+ ([s] (VcrMemoryStore. s)))
@@ -88,9 +88,3 @@
(is (= "original-values-for-a-aaa-b-1-c-2-d-3 chained-values-for-a-aaa-b-1-c-2-d-3" (wrap-fn-original-arity-1-and-optional-keys "aaa" :b 1 :c 2 :d 3))))
)
-(deftest test-recorder
- (wrap-fn #'client/request #'recorder)
- (client/get "http://google.com" {:accept :json :throw-exceptions false})
- (is (request-recorded? {:url "http://google.com" :method :get :accept :json :throw-exceptions false}))
- )
-
@@ -0,0 +1,21 @@
+(ns vcr-clj.recorder-test
+ (:use clojure.test
+ vcr-clj.recorder))
+
+(deftest record-request-test
+ (let [store (vcr-memory-store)]
+ (record-request! store :k "value")
+ (is (= (get-request store :k) "value"))))
+
+(deftest request-recorded-test
+ (let [store (vcr-memory-store)]
+ (record-request! store :k "value")
+ (is (request-recorded? store :k))))
+
+
+(deftest request-recorded-test
+ (let [store (vcr-memory-store)]
+ (record-request! store :k "value")
+ (clear-recorded! store)
+ (is (= @(get-all store) {}))))
+

0 comments on commit 501f1ec

Please sign in to comment.