Permalink
Browse files

Fixed render macro. Major rewrite, mostly formating code for smaller …

…screen.
  • Loading branch information...
1 parent 461413f commit 045d9dabcd6d08f9aa413b63473b1a9075adefed @ashafa committed May 18, 2012
@@ -1,15 +1,17 @@
(ns renderer.macros)
+
(defmacro render
[& body]
(let [output (last body)
- body (butlast body)
+ render-fn? (= (and (list? output) (first output)) 'render)
+ template? (= (and (list? output) (first output)) '>>)
args (if (vector? (first body)) (first body) ['req 'res])
handler-args (if (= (count args) 1) (conj args 'res) args)
res-code (or (first (filter integer? body)) 200)
default-type {:Content-Type "text/html; charset=UTF-8"}
headers (merge default-type (or (first (filter map? body)) {}))]
- (cond (= (and (list? output) (first output)) '>>)
+ (cond template?
`(fn [~@handler-args]
(let [req# (first ~handler-args)
res# (second ~handler-args)
@@ -31,18 +33,20 @@
:else
(do (.writeHeader res# 500 (utils/clj->js ~default-type))
(.end res# "'map' to render template OR 'fn' required.")))))
+ render-fn?
+ `(fn [~@handler-args]
+ (let [req# (first ~handler-args)
+ res# (second ~handler-args)]
+ (~output req# res#)))
(list? output)
`(fn [~@handler-args]
- (let [req# (first ~handler-args)
- res# (second ~handler-args)
- context# ~output]
- (if (fn? context#)
- (context# req# res#)
- (doto (second ~handler-args)
- (.writeHeader ~res-code (utils/clj->js ~headers))
- (.end context#)))))
+ (let [req# (first ~handler-args)
+ res# (second ~handler-args)
+ output# ~output]
+ (if (fn? output#)
+ (output# req# res#))))
:else
`(fn [~@handler-args]
(doto (second ~handler-args)
(.writeHeader ~res-code (utils/clj->js ~headers))
- (.end (str ~output)))))))
+ (.end (if (string? ~output) ~output)))))))
@@ -71,7 +71,7 @@
(second (station :location))
(.. % -coords -longitude)
(.. % -coords -latitude))))))]
- (.html ($ "#nearby-stations") (templates/unordered-list-of-nearest-stations stations current-url))))))
+ (.html ($ "#nearby-stations") (templates/list-of-nearest-stations stations current-url))))))
(defn make-websocket
@@ -1,4 +1,4 @@
-(ns wheresthembta.real-time-feed
+(ns wheresthembta.mbta-feed
(:require [cljs.nodejs :as node]
[wheresthembta.shared.mbta-data :as mbta-data]))
@@ -11,12 +11,12 @@
(def waiters (atom []))
-(defn get-real-time-feed-data
+(defn get-feed-data
[callback]
(fn [req res]
(let [transit-id (.. req -params -transit)
line-id (.. req -params -line)]
- (if-let [real-time-feed-url (mbta-data/get-value transit-id :lines line-id :real-time-feed-url)]
+ (if-let [real-time-feed-url (mbta-data/get transit-id :lines line-id :real-time-feed-url)]
(let [{:keys [data time status]} (@feed-cache line-id)]
(if (and (= status ::not-fetching) (< (/ (- (.now js/Date) time) 1000) 15))
(callback req res data)
@@ -31,7 +31,7 @@
(doseq [waiter @waiters] (waiter %))
(reset! waiters []))
rest (-> (.get restler real-time-feed-url)
- (.on "2xx" #(set-cache (js->clj (.parse js/JSON %) :keywordize-keys true)))
+ (.on "complete" #(set-cache (js->clj (.parse js/JSON %) :keywordize-keys true)))
(.on "4xx" #(set-cache (or data [])))
(.on "error" #(set-cache (or data []))))]
(reset! timeout (js/setTimeout #(.. rest -request (abort "timeout")) 2000)))))))
@@ -0,0 +1,76 @@
+(ns wheresthembta.redis
+ (:refer-clojure :exclude [get set del keys])
+ (:require [cljs.nodejs :as node])
+ (:use [wheresthembta.shared.utils :only [clj->js]]))
+
+
+
+(def redis (.createClient (node/require "redis")))
+
+
+
+(defn json-encode [data]
+ (.stringify js/JSON (clj->js data)))
+
+(defn json-decode [data]
+ (js->clj (if (string? data) (.parse js/JSON data) data) :keywordize-keys true))
+
+
+(defn set
+ ([key value]
+ (set key value #()))
+ ([key value callback]
+ (.set redis key (json-encode value) callback)))
+
+(defn get
+ [key callback]
+ (.get redis key #(callback (if %2 (json-decode %2)))))
+
+(defn mget
+ [keys callback]
+ (.mget redis (clj->js keys) (fn [error values]
+ (callback
+ (if values
+ (map #(json-decode %) values))))))
+
+(defn lpush
+ ([key value]
+ (lpush key value #()))
+ ([key value callback]
+ (.lpush redis key (json-encode value) #(callback %2))))
+
+(defn lrange
+ [key start stop callback]
+ (.lrange redis key start stop (fn [error values]
+ (callback
+ (if values
+ (map #(json-decode %) values))))))
+
+(defn ltrim
+ ([key start stop]
+ (ltrim key start stop #()))
+ ([key start stop callback]
+ (.ltrim redis start stop callback)))
+
+(defn expire
+ ([key timeout]
+ (expire key timeout #()))
+ ([key timeout callback]
+ (.expire redis key timeout callback)))
+
+(defn keys
+ [pattern callback]
+ (.keys redis pattern #(callback (js->clj %2))))
+
+(defn del
+ ([keys]
+ (del keys #()))
+ ([keys callback]
+ (.del redis (clj->js keys) #(callback %2))))
+
+;(keys "*" #(del %))
+
+(lrange "/subway/orange-line" 0 20
+ (fn [tweet-ids]
+ (mget tweet-ids #(println (filter (fn [d] (number? (d :retweet_count))) %)))))
+
@@ -1,18 +0,0 @@
-(ns wheresthembta.redis-client
- (:require [cljs.nodejs :as node]))
-
-
-
-(def redis (.createClient (node/require "redis")))
-
-
-
-(defn save-tweet
- [key value callback]
- (.lpush redis key value callback))
-
-(defn get-related-tweets
- [callback]
- (fn [req res]
- (.lrange redis (.-url req) 0 2 #(callback req res (map (fn [tweet]
- (js->clj (.parse js/JSON tweet) :keywordize-keys true)) %2)))))
@@ -10,7 +10,7 @@
(defn send-to-room
[room message]
(.. @io -sockets (to room) (emit "new-tweet" message)))
-
+
(defn hook
[router]
(reset! io (.listen (node/require "socket.io") router))
@@ -1,39 +1,81 @@
(ns wheresthembta.twitter
(:require [cljs.nodejs :as node]
[wheresthembta.socket-io :as socket-io]
- [wheresthembta.redis-client :as redis-client]
+ [wheresthembta.redis :as redis]
[wheresthembta.shared.mbta-data :as mbta-data]
[wheresthembta.config :as config])
(:use [wheresthembta.shared.utils :only [clj->js]]))
-(def node-twitter (node/require config/path-to-patched-node-twitter))
+(def node-twitter
+ (node/require config/path-to-patched-node-twitter))
-(def keys-and-tokens (clj->js {:consumer_key config/consumer-key
- :consumer_secret config/consumer-secret
- :access_token_key config/access-token-key
- :access_token_secret config/access-token-secret}))
+(def keys-and-tokens
+ {:consumer_key config/consumer-key
+ :consumer_secret config/consumer-secret
+ :access_token_key config/access-token-key
+ :access_token_secret config/access-token-secret})
(def connect-time (atom 2000))
-(defn tag-save-send-tweet
+(defn get-related-tweets
+ [callback]
+ (fn [req res]
+ (redis/lrange (.-url req) 0 20
+ (fn [tweet-ids]
+ (redis/mget
+ tweet-ids
+ #(callback req res
+ (filter (complement nil?) %)))))))
+
+
+(defn tag-tweet
[tweet]
- (let [search-for-station (fn [station]
- (let [search-using (fn [search]
- (map #(.test % (.-text tweet))
- (search station)))]
- (and (not-every? #(= false %) (search-using :search-for))
- (not-any? #(= true %) (search-using :search-not)))))
- tagged-stations (filter search-for-station mbta-data/all-stations)
- tweet-stringified (.stringify js/JSON tweet)]
- (.log js/console (.-text tweet))
+ (let [tweet
+ (js->clj (.parse js/JSON
+ (.stringify js/JSON tweet))
+ :keywordize-keys true)
+ search-for-station
+ (fn [station]
+ (let [search-using
+ (fn [search]
+ (map #(.test % (:text tweet))
+ (search station)))]
+ (and (not-every? #(= false %)
+ (search-using :search-for))
+ (not-any? #(= true %)
+ (search-using :search-not)))))
+ tagged-stations (filter
+ search-for-station
+ mbta-data/all-stations)]
+ (println tweet)
(println tagged-stations)
(doseq [station tagged-stations]
- (when (not (.-retweeted-status tweet))
- (.log js/console "Saving...")
- (redis-client/save-tweet (station :url) tweet-stringified #(socket-io/send-to-room (station :url) tweet))))))
+ (if (not (:retweeted_status tweet))
+ (redis/set (:id_str tweet) tweet
+ (fn []
+ (redis/expire (:id_str tweet)
+ (* 6 60 60))
+ (redis/lpush
+ (station :line-url) (:id_str tweet)
+ #(redis/ltrim (station :line-url)
+ 0 60))
+ (redis/lpush
+ (station :url) (:id_str tweet)
+ #(redis/ltrim (station :url)
+ 0 20))))
+ (redis/get
+ (:id_str (:retweeted_status tweet))
+ #(if % (redis/set
+ (:id_str (:retweeted_status tweet))
+ (assoc %
+ :retweet_count
+ (if (string? (:retweet_count %))
+ 1 (inc (:retweet_count %)))))))))))
+
+;#(socket-io/send-to-room (station :url) tweet)
(defn reconnect
[reason]
@@ -45,15 +87,23 @@
(defn connect
[]
- (let [twitter (node-twitter. keys-and-tokens)
+ (let [twitter (node-twitter. (clj->js keys-and-tokens))
track-params (clj->js {:track "mbta"})]
(js/setTimeout
(fn []
(.stream twitter "statuses/filter" track-params
(fn [stream]
- (.on stream "data" #(if (.-id %) (tag-save-send-tweet %)))
- (.on stream "error" #(.log js/console (str "Callback error: " %)))
+ (.on stream "data"
+ #(if (.-id %)
+ (tag-tweet %)))
+ (.on stream "error"
+ #(.log js/console
+ (str "Callback error: " %)))
(.on stream "end" reconnect)
- (.on stream "connection-ok" #(reset! connect-time 0))
- (.on stream "connection-error" #(println (str "Stream connection error: " (.-statusCode %)))))))
+ (.on stream "connection-ok"
+ #(reset! connect-time 0))
+ (.on stream "connection-error"
+ #(println
+ (str "Connection error: "
+ (.-statusCode %)))))))
@connect-time)))
Oops, something went wrong.

0 comments on commit 045d9da

Please sign in to comment.