Find file
14d69a3 Jan 9, 2012
@frenchy64 @swannodette
82 lines (69 sloc) 2.03 KB
(ns logic-introduction.fetch
(:refer-clojure :exclude [==])
(:use [ :only (json-str write-json read-json)])
(:use [clojure.core.logic]))
(defn url-to-process
"Output: url"
(== url ""))
(defn slurpo
"Input: url
Output: datastream"
[url datastream]
(project [url]
(== datastream (slurp url))))
(defn read-jsono
"Input: input
Output: output"
[input output]
(project [input]
(== output (read-json input))))
(defn fetch-data
"Output: fetched-data"
(fresh [url datastream]
(url-to-process url)
(slurpo url datastream)
(read-jsono datastream fetched-data)))
(defn process-data-header
"Input: header"
(matche [header]
(defn json-object-has-value
"Input: json-object, name
Output: value"
[json-object name value]
(project [json-object name]
(== [name value] (find json-object name))))
(defn process-json-object
"Input: json-object"
(fresh [date-value indicator-value temp]
(json-object-has-value json-object :date date-value)
(json-object-has-value json-object :value indicator-value)
(project [date-value indicator-value]
;; TODO probably have to wrap println
(== temp (println "Date: " date-value))
(== temp (println "Value: " indicator-value))
(== temp (println)))))
(defn process-data-contents
"Input: json"
(matche [json]
([[?json-object . ?rest]]
(process-json-object ?json-object)
(process-data-contents ?rest))))
(defn process-data
"Input: json"
(matche [json]
([[?header ?contents]]
(process-data-header ?header)
(process-data-contents ?contents))))
(defn fetch-and-process-data []
(run* [fetched-data]
(fetch-data fetched-data)
(process-data fetched-data)))