Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for usersConnected, channel sending and channel listing

  • Loading branch information...
commit 8db525b63c2e0c8f6eaf0828724a0e6f426539cf 1 parent a2116b4
@cgbystrom authored
Showing with 65 additions and 16 deletions.
  1. +31 −0 spec/client-spec.coffee
  2. +34 −16 src/beaconpush/version1.coffee
View
31 spec/client-spec.coffee
@@ -2,6 +2,7 @@ require.paths.unshift __dirname + "/../node_modules"
vows = require('vows')
assert = require('assert')
beaconpush = require("../src/index")
+sys = require('sys')
Client = beaconpush.Client
API_KEY = 'c37c744dac99b19c9f5485d1967ace3f0d18914c'
@@ -19,6 +20,10 @@ vows.describe('Beaconpush REST API v1 Client').addBatch
'default to API version 1': ->
assert.equal new Client('abc37bfe', 'abe8efa42efdd2fe1a8c').version, '1'
+ 'can check number of users connected':
+ topic: -> new Client(API_KEY, SECRET_KEY).usersConnected @callback
+ 'after a successful': (err, numConnected) -> assert.equal numConnected, 0
+
'with a user can':
topic: new Client(API_KEY, SECRET_KEY).user('charlie')
'check if that user is online':
@@ -33,6 +38,32 @@ vows.describe('Beaconpush REST API v1 Client').addBatch
topic: (user) -> user.send {foo: "ZOMG"}, @callback
'after a successful response': (err, numDelivered) -> assert.isTrue numDelivered >= 1
+ 'with a selected channel can':
+ topic: new Client(API_KEY, SECRET_KEY).channel('chat')
+ 'send a message to all users in channel':
+ topic: (channel) -> channel.send {foo: "ZOMG"}, @callback
+ 'after a successful response': (err, numDelivered) -> assert.isTrue numDelivered >= 1
+
+ 'list all users in channel':
+ topic: -> new Client(API_KEY, SECRET_KEY).channel('chat').usersConnected @callback
+ 'after a successful response': (err, users) ->
+ assert.isArray users
+ assert.length users, 0
+
+ 'list all users in channel':
+ topic: -> new Client(API_KEY, SECRET_KEY).channel('chat').usersConnected @callback
+ 'after a succesful response': (err, users) ->
+ assert.isArray users
+ assert.length users, 0
+
+
+# 'disconnect that user':
+# topic: (user) -> user.disconnect @callback
+# 'after a successful response': (err) -> assert.isUndefined err
+
+# 'send a message to that user':
+# topic: (user) -> user.send {foo: "ZOMG"}, @callback
+# 'after a successful response': (err, numDelivered) -> assert.isTrue numDelivered >= 1
.export module
View
50 src/beaconpush/version1.coffee
@@ -2,11 +2,6 @@
#beaconPush.usersConnected
-#beaconpush.user("charlie").connected
-#beaconpush.user("charlie").send({"apa": 123})
-#beaconpush.user("charlie").disconnect()
-
-
#beaconpush.channel("forum").usersConnected
#beaconpush.channel("forum").send({"myMessage": "sdsdf})
@@ -17,14 +12,22 @@ clone = (o) ->
c = {}
for k, v of o
c[k] = v
- c
+ c
-class User
- constructor: (client, name) ->
+class Resource
+ constructor: (client, type, name) ->
@client = client
@name = name
- @url = @client.url + '/users/' + @name
+ @url = @client.url + '/' + type + 's/' + @name
+ send: (message, callback) ->
+ request {uri: @url, method: 'POST', headers: @client.headers, json: message}, (err, response, body) ->
+ numDelivered = undefined
+ if not err and response.statusCode is 200
+ numDelivered = JSON.parse(body).messages_sent
+ callback(err, numDelivered)
+
+class User extends Resource
isConnected: (callback) ->
request {uri: @url, headers: @client.headers}, (err, response, body) ->
connected = not err and response.statusCode == 200
@@ -37,13 +40,16 @@ class User
if not err and response.statusCode isnt 204
err = "Unexpected response code: " + response.statusCode
callback(err)
-
- send: (message, callback) ->
- request {uri: @url, method: 'POST', headers: @client.headers, json: message}, (err, response, body) ->
- numDelivered = undefined
+
+class Channel extends Resource
+ usersConnected: (callback) ->
+ request {uri: @url, headers: @headers}, (err, response, body) ->
+ users = undefined
if not err and response.statusCode is 200
- numDelivered = JSON.parse(body).messages_sent
- callback(err, numDelivered)
+ users = JSON.parse(body).users
+ else
+ err = "Unexpected response code: " + response.statusCode
+ callback(err, users)
class Client
version: '1'
@@ -53,9 +59,21 @@ class Client
@headers = {'X-Beacon-Secret-Key': @secretKey}
@url = 'http://api.beaconpush.com/1.0.0/' + @apiKey
+ channel: (name) ->
+ # TODO: Check valid channel name
+ new Channel(this, 'channel', name)
+
user: (name) ->
# TODO: Check valid username
- new User(this, name)
+ new User(this, 'user', name)
+
+ usersConnected: (callback) ->
+ request {uri: @url + '/users', headers: @headers}, (err, response, body) ->
+ numConnected = undefined
+ if not err and response.statusCode is 200
+ numConnected = JSON.parse(body).online
+ callback(err, numConnected)
+
module.exports = Client
Please sign in to comment.
Something went wrong with that request. Please try again.