Skip to content

Commit 269591a

Browse files
committed
added @doo Clojure lib and reference.
1 parent 4dd015c commit 269591a

File tree

4 files changed

+498
-0
lines changed

4 files changed

+498
-0
lines changed

clojure/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# clj-pubnub
2+
3+
Original Author: [DOO.net](https://github.com/doo/clj-pubnub).
4+
5+
Clojure client for [PubNub](http://www.pubnub.com/).
6+
7+
## Usage
8+
9+
### Publishing
10+
11+
```clojure
12+
;; The client should always be required with an alias
13+
(require '[clj-pubnub.client :as pubnub])
14+
15+
;; Configuration can be passed directly
16+
(pubnub/publish {:pub-key "demo" :sub-key "demo"} "my_channel" {:hello "world"})
17+
18+
;; ... or through a binding
19+
(binding [pubnub/config {:pub-key "demo" :sub-key "demo"}]
20+
(pubnub/publish "my_channel" {:hello "world"}))
21+
22+
;; ... or by setting it globally
23+
(pubnub/set-config! {:pub-key "demo" :sub-key "demo"})
24+
(pubnub/publish "my_channel" {:hello "world"})
25+
26+
;; SSL and signing is also supported
27+
(pubnub/publish {:pub-key "demo"
28+
:sub-key "demo"
29+
:secret-key "demo"
30+
:ssl true}
31+
"my_channel"
32+
{:hello "world"})
33+
```
34+
35+
## License
36+
37+
Copyright (C) 2012 Moritz Heidkamp, doo GmbH
38+
39+
Distributed under the Eclipse Public License, the same as Clojure.

clojure/project.clj

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(defproject clj-pubnub "0.0.1-SNAPSHOT"
2+
:description "Clojure client for PubNub"
3+
:dependencies [[org.clojure/clojure "1.3.0"]
4+
[clj-http "0.3.2"]
5+
[cheshire "2.2.2"]
6+
[digest "1.4.0"]]
7+
:dev-dependencies [[swank-clojure "1.4.0"]])

clojure/src/clj_pubnub/client.clj

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(ns clj-pubnub.client
2+
(:use [digest :only [digest]])
3+
(:require [clj-http.client :as http]
4+
[cheshire.core :as json]
5+
[clojure.contrib.string :as str]))
6+
7+
(defonce ^{:dynamic true} config
8+
{})
9+
10+
(defn set-config! [settings]
11+
(alter-var-root (var config) (constantly settings)))
12+
13+
(def default-origin
14+
"pubsub.pubnub.com")
15+
16+
(defn- sign [channel message pub-key sub-key secret-key]
17+
(if secret-key
18+
(->> [pub-key sub-key secret-key channel]
19+
(str/join "/")
20+
(digest "md5"))
21+
"0"))
22+
23+
;; What a terrible hack ... gotta love the Java API
24+
(defn- encode-path-segment [segment]
25+
(-> (java.net.URI. nil nil (str "/" segment) nil)
26+
(str)
27+
(.substring 1)))
28+
29+
(defn- build-publish-uri [config channel message]
30+
(let [message (json/generate-string message)
31+
{:keys [pub-key sub-key secret-key origin ssl]} config]
32+
(str (if ssl "https" "http") "://"
33+
(->> [(or origin default-origin)
34+
"publish"
35+
pub-key
36+
sub-key
37+
(sign channel message pub-key sub-key secret-key)
38+
channel
39+
"0"
40+
message]
41+
(map encode-path-segment)
42+
(str/join "/")))))
43+
44+
(defn publish
45+
([channel message]
46+
(publish config channel message))
47+
([config channel message]
48+
({:pre [(string? channel)
49+
(every? config [:pub-key :sub-key])]}
50+
(let [uri (build-publish-uri config channel message)]
51+
(println uri)
52+
(http/get uri)))))

0 commit comments

Comments
 (0)