Skip to content
Browse files

Correct path segment encoding

We take a fairly primitive but good enough approach
that Neography uses. It is also more efficient, too.
  • Loading branch information...
1 parent c185ed5 commit a785b41a95de5515cdef5c01c030d12ab8fc4d46 Michael Klishin committed Feb 6, 2013
View
2 project.clj
@@ -24,6 +24,8 @@
:examples :examples
:batching :batching
:traversal :traversal
+ :uri-encoding (fn [m] (or (:examples m)
+ (:indexing m)))
:all (constantly true)}
:source-paths ["src/clojure"]
:profiles {:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
View
6 src/clojure/clojurewerkz/neocons/rest/helpers.clj
@@ -1,5 +1,5 @@
(ns clojurewerkz.neocons.rest.helpers
- (:import [java.net URI URL URLEncoder]))
+ (:import [java.net URI URL]))
;;
@@ -11,10 +11,6 @@
(let [url (URL. location)]
(Long/valueOf ^String (first (re-seq #"\d+$" (.getPath url))))))
-(defn encode-segment
- [^String s]
- (URLEncoder/encode s))
-
(defn maybe-append
[^String s ^String prefix]
(.toLowerCase (if (.endsWith (.toLowerCase s) (.toLowerCase prefix))
View
17 src/clojure/clojurewerkz/neocons/rest/nodes.clj
@@ -65,9 +65,9 @@
This function returns a lazy sequence of results, so you may need to force it using clojure.core/doall"
[xs]
(let [batched (doall (reduce (fn [acc x]
- (conj acc {:body x
- :to "/node"
- :method "POST"})) [] xs))
+ (conj acc {:body x
+ :to "/node"
+ :method "POST"})) [] xs))
{:keys [status headers body]} (rest/POST (:batch-uri rest/*endpoint*) :body (json/encode batched))
payload (map :body (json/decode body true))]
(map instantiate-node-from payload)))
@@ -192,14 +192,13 @@
([node idx ^String key value]
(add-to-index node idx key value false))
([node idx ^String key value unique?]
- (println "During indexing:" key value (node-location-for rest/*endpoint* (to-id node)))
(let [id (to-id node)
req-body (json/encode {:key (name key) :value value :uri (node-location-for rest/*endpoint* (to-id node))})
{:keys [status body]} (rest/POST (node-index-location-for rest/*endpoint* idx) :body req-body :query-string (if unique?
- {"unique" "true"}
- {}))
- payload (json/decode body true)]
- (instantiate-node-from payload id))))
+ {"unique" "true"}
+ {}))
+ payload (json/decode body true)]
+ (instantiate-node-from payload id))))
(defn delete-from-index
@@ -230,12 +229,10 @@
(defn find
"Finds nodes using the index"
([^String key value]
- (println "During lookup:" key value (auto-node-index-lookup-location-for rest/*endpoint* key value))
(let [{:keys [status body]} (rest/GET (auto-node-index-lookup-location-for rest/*endpoint* key value))
xs (json/decode body true)]
(map (fn [doc] (fetch-from (:indexed doc))) xs)))
([^String idx ^String key value]
- (println "During lookup:" key value (node-index-lookup-location-for rest/*endpoint* idx key value))
(let [{:keys [status body]} (rest/GET (node-index-lookup-location-for rest/*endpoint* idx key value))
xs (json/decode body true)]
(map (fn [doc] (fetch-from (:indexed doc))) xs))))
View
20 src/clojure/clojurewerkz/neocons/rest/records.clj
@@ -22,15 +22,27 @@
(defrecord CypherQueryResponse
[data columns])
+
+
(def ^{:const true} slash "/")
-(defn url-with-path
+(defn ^String encode-slashes
+ [^String s]
+ (.replaceAll s "/" "%2F"))
+
+(defn ^String encode-segment
+ [^String s]
+ (encode-slashes (u/encode-path s)))
+
+
+
+(defn ^String url-with-path
[^String root & segments]
- (str root slash (u/encode-path (s/join slash segments))))
+ (str root slash (s/join slash segments)))
-(defn root-with-path
+(defn ^String root-with-path
[^Neo4JEndpoint endpoint & segments]
- (str (:uri endpoint) slash (URLEncoder/encode (u/encode-path (s/join slash segments)))))
+ (str (:uri endpoint) slash (s/join slash segments)))
(defn node-location-for
[^Neo4JEndpoint endpoint ^long id]
View
12 test/clojurewerkz/neocons/rest/test/indexing_test.clj
@@ -30,7 +30,7 @@
(nodes/create-index name conf)
(nodes/delete-index name)))
-(deftest ^{:indexing true :focus true} test-create-a-new-rel-index-with-explicit-configuration
+(deftest ^{:indexing true} test-create-a-new-rel-index-with-explicit-configuration
(let [name "rel-index-2"
conf {:type "fulltext" :provider "lucene"}]
(rels/create-index name conf)))
@@ -73,14 +73,14 @@
home (nodes/create {:uri uri})]
(nodes/add-to-index (:id home) (:name idx) "uri" uri)))
-(deftest ^{:indexing true :focus true} test-adding-a-node-to-index-with-value-with-spaces
+(deftest ^{:indexing true} test-adding-a-node-to-index-with-value-with-spaces
(let [idx (nodes/create-index "things")
s "a value with spaces"
k "a key with spaces"
n (nodes/create {:value s})
_ (nodes/add-to-index (:id n) (:name idx) k s)
n' (nodes/find-one (:name idx) k s)]
- (println n')))
+ (is (= "a value with spaces" (-> n' :data :value)))))
(deftest ^{:indexing true} test-adding-a-node-to-index-as-unique
(let [idx (nodes/create-index "uris")
@@ -147,7 +147,7 @@
(nodes/add-to-index (:id home) (:name idx) "lat" 20.0)
(nodes/delete-from-index (:id home) (:name idx) "lat" 20.0)))
-(deftest ^{:indexing true :focus true} test-finding-nodes-using-an-index
+(deftest ^{:indexing true} test-finding-nodes-using-an-index
(let [node1 (nodes/create {:name "Wired"})
node2 (nodes/create {:name "Craigslist"})
url1 "http://wired.com"
@@ -157,13 +157,11 @@
(nodes/delete-from-index (:id node2) (:name idx) "url")
(nodes/add-to-index (:id node1) (:name idx) "url" url1)
(nodes/add-to-index (:id node2) (:name idx) "url" url2)
- (println (nodes/find (:name idx) "url" url1))
- (println (nodes/find (:name idx) "url" url1))
(let [ids (set (map :id (nodes/find (:name idx) "url" url1)))]
(is (ids (:id node1)))
(is (not (ids (:id node2)))))))
-(deftest ^{:indexing true :focus true} test-finding-rels-using-an-index
+(deftest ^{:indexing true} test-finding-rels-using-an-index
(let [node1 (nodes/create {:name "Wired" :url "http://wired.com"})
url "http://craigslist.org"
node2 (nodes/create {:name "Craigslist" :url url})
View
4 test/clojurewerkz/neocons/rest/test/integration_test.clj
@@ -13,7 +13,7 @@
;; kinds of features
;;
-(deftest ^{:examples true :focus true} test-example1
+(deftest ^{:examples true} test-example1
(let [homepage (nn/create {:url "http://clojurewerkz.org/"})
community (nn/create {:url "http://clojurewerkz.org/articles/community.html"})
about (nn/create {:url "http://clojurewerkz.org/articles/about.html"})
@@ -32,7 +32,7 @@
(is (not (np/exists-between? (:id homepage) (:id about) :relationships [rt])))
(nn/destroy-many [homepage community about projects])))
-(deftest ^{:examples true :focus true} test-example2
+(deftest ^{:examples true} test-example2
(nn/create-index "by-url" {:type "exact"})
(nn/create-index "roots" {:type "exact"})
(let [homepage (nn/create {:url "http://clojurewerkz.org/"} {"roots" ["root" true]})

0 comments on commit a785b41

Please sign in to comment.
Something went wrong with that request. Please try again.