Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rating-area-parser/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:license {:name "MIT"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
;[org.clojure/clojure "1.9.0-beta2"]
[enlive "1.1.6"]
[http-kit "2.2.0"]
[org.clojure/data.xml "0.0.8"]]
Expand Down
56 changes: 32 additions & 24 deletions rating-area-parser/src/rating_area_parser/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,54 @@
(assoc new-map (keyword state) (partition-rating-areas state))) {} states))

(defn determine-type [value]
(cond
(clojure.string/blank? value) "String"
(number? (read-string value)) "Number"
:else "String"))
(if (re-find #"^[0-9]+$" value) "Number" "String"))

(defn build-xml-data-tag
[value]
(-> {:tag :Data :attrs {"ss:Type" (determine-type value)}}
(-> ((fnil determine-type " ") value)
(#(identity {:tag :Data :attrs {"ss:Type" %}}))
(cond-> (not (empty? value)) (assoc :content [value]))
(#(identity {:tag :Cell :content [%]}))))

(defn build-xml-cell
(defn build-xml-row
[collection]
(-> (into [] (mapv #(build-xml-data-tag %) collection))
(-> (into [] (map #(build-xml-data-tag %) collection))
(#(identity {:tag :Row :content %}))))

(defn build-xml-worksheet
[values-map]
(cons
{:tag :Worksheet :attrs {"ss:Name" "Options"}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [
(build-xml-data-tag "Effective Year")
(build-xml-data-tag "2018")]}]}]}]}
[]))
[state values]
(-> (into [] (map #(build-xml-row %) values))
(#(identity {:tag :Worksheet :attrs {"ss:Name" (name state)}
:content [{:tag :Table :content %}]}))))

(defn dump-everything
[values-map]
(doseq [[state values] values-map]
(spit "test3"
(with-out-str (xml/emit (build-xml-worksheet state values))))))
;; ==========================================
(comment
(reduce
(fn [result [key value]]
(conj {:tag :Worksheet :attrs {"ss:Name" (name key)}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-xml-cell value)}]}]}]}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-row value)}]}]}]}
result)) [] (sort values-map)))
(defn testheader
[values-map]
(xml/emit-element
{:tag :WorkBook :attrs {:xmlns "urn:schemas-microsoft-com:office:spreadsheet"
:xmlns:o "urn:schemas-microsoft-com:office:office"
:xmlns:x "urn:schemas-microsoft-com:office:excel"
:xmlns:ss "urn:schemas-microsoft-com:office:spreadsheet"
:xmlns:html "http://www.w3.org/TR/REC-html40"}
:content (build-xml-worksheet values-map)}))

;; ==========================================

(defn write-generator [filename]
#(spit filename (with-out-str %)))

(defn dump-to-xml
[values-map filename]
(let [writer (write-generator filename)]
(xml/emit-element
{:tag :WorkBook :attrs {:xmlns "urn:schemas-microsoft-com:office:spreadsheet"
:xmlns:o "urn:schemas-microsoft-com:office:office"
:xmlns:x "urn:schemas-microsoft-com:office:excel"
:xmlns:ss "urn:schemas-microsoft-com:office:spreadsheet"
:xmlns:html "http://www.w3.org/TR/REC-html40"}
:content (build-xml-worksheet values-map)})))

(defn -main
"My attempt at writing a rating-area parser in clojure"
Expand Down
42 changes: 34 additions & 8 deletions rating-area-parser/test/rating_area_parser/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,50 @@
(testing "sanitize-and-normalize fn"
(is (= ["Rating Area 3" " " "445"]
(sanitize-and-normalize-data ["Rating Area 3 " " " "" "445"]))))
(testing "determine-type"
(is (= "Number" (determine-type "018"))
(= "String" (determine-type "abc")))
(is (= "String" (determine-type "Rating Area 1"))))

(testing "build-xml-data-tag"
(is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"}}]}
(build-xml-data-tag nil)))
(is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]}
(build-xml-data-tag "Rating Area 1")))
(is (= {:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "Number"} :content ["995"]}]}
(build-xml-data-tag "995"))))

(testing "build-xml-cell"
(let [test-list '("Rating Area 1" "Boundary" " ")]
(testing "build-xml-row"
(let [test-list (lazy-seq '("Rating Area 1" "Boundary" "996" " "))]
(is (= {:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Boundary"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}
(build-xml-cell test-list))))))
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Boundary"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "Number"} :content ["996"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}
(build-xml-row test-list)))))

(testing "build-xml-worsheet"
(let [test-data {:WA '(("Rating Area 1" "King" " ")
("Rating Area 2" "Clallam" " ")
("Rating Area 2" "Cowlitz" " "))}
test-options {:Options '(("Effective Year" "2018"))}]
(= {:tag :Worksheet :attrs {"ss:Name" "Options"}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [(build-xml-data-tag "Effective Year")
(build-xml-data-tag "2018")]}]}]}]}
(build-xml-worksheet (ffirst test-options) (second (first test-options))))
(is (= {:tag :Worksheet :attrs {"ss:Name" "WA"}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 1"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["King"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}
{:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 2"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Clallam"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}
{:tag :Row :content [{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Rating Area 2"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content ["Cowlitz"]}]}
{:tag :Cell :content [{:tag :Data :attrs {"ss:Type" "String"} :content [" "]}]}]}]}]}
(build-xml-worksheet (ffirst test-data) (second (first test-data))))))))


(comment
testing "build-xml-worksheet"
(is (= {:tag :Worksheet :attrs {"ss:Name" "ID"}
:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-xml-cell state)}]}]}]})))

:content [{:tag :Table :content [{:tag :Row :content [{:tag :Cell :content (build-row state)}]}]}]})))