Permalink
Browse files

Rename device to subscriber to comply with future evolutions of pushd…

… *teaser*
  • Loading branch information...
1 parent 9ae94e2 commit c5ce15f5a762801097f8d8e2ddf523be510d3a1f @rs committed Apr 5, 2012
Showing with 416 additions and 419 deletions.
  1. +83 −85 README.md
  2. +29 −29 lib/api.coffee
  3. +15 −15 lib/event.coffee
  4. +16 −16 lib/pushservices.coffee
  5. +82 −83 lib/{device.coffee → subscriber.coffee}
  6. +11 −11 pushd.coffee
  7. +0 −166 tests/device.coffee
  8. +14 −14 tests/event.coffee
  9. +166 −0 tests/subscriber.coffee
View
168 README.md

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -3,40 +3,40 @@ filterFields = (params) ->
fields[key] = val for own key, val of params when key in ['proto', 'regid', 'lang', 'badge', 'version']
return fields
-exports.setupRestApi = (app, createDevice, authorize) ->
+exports.setupRestApi = (app, createSubscriber, authorize) ->
authorize ?= (realm) ->
- # Device registration
- app.post '/devices', authorize('register'), (req, res) ->
+ # subscriber registration
+ app.post '/subscribers', authorize('register'), (req, res) ->
try
fields = filterFields(req.body)
- createDevice fields, (device, created) ->
- device.get (info) ->
- info.id = device.id
- res.header 'Location', "/device/#{device.id}"
+ createSubscriber fields, (subscriber, created) ->
+ subscriber.get (info) ->
+ info.id = subscriber.id
+ res.header 'Location', "/subscriber/#{subscriber.id}"
res.json info, if created then 201 else 200
catch error
res.json error: error.message, 400
- # Get device info
- app.get '/device/:device_id', authorize('register'), (req, res) ->
- req.device.get (fields) ->
+ # Get subscriber info
+ app.get '/subscriber/:subscriber_id', authorize('register'), (req, res) ->
+ req.subscriber.get (fields) ->
res.json fields, if fields? then 200 else 404
- # Edit device info
- app.post '/device/:device_id', authorize('register'), (req, res) ->
+ # Edit subscriber info
+ app.post '/subscriber/:subscriber_id', authorize('register'), (req, res) ->
fields = filterFields(req.body)
- req.device.set fields, (edited) ->
+ req.subscriber.set fields, (edited) ->
res.send if edited then 204 else 404
- # Unregister device
- app.delete '/device/:device_id', authorize('register'), (req, res) ->
- req.device.delete (deleted) ->
+ # Unregister subscriber
+ app.delete '/subscriber/:subscriber_id', authorize('register'), (req, res) ->
+ req.subscriber.delete (deleted) ->
res.send if deleted then 204 else 404
- # Get device subscriptions
- app.get '/device/:device_id/subscriptions', authorize('register'), (req, res) ->
- req.device.getSubscriptions (subs) ->
+ # Get subscriber subscriptions
+ app.get '/subscriber/:subscriber_id/subscriptions', authorize('register'), (req, res) ->
+ req.subscriber.getSubscriptions (subs) ->
if subs?
subsAndOptions = {}
for sub in subs
@@ -45,28 +45,28 @@ exports.setupRestApi = (app, createDevice, authorize) ->
else
res.send 404
- # Get device subscription options
- app.get '/device/:device_id/subscriptions/:event_id', authorize('register'), (req, res) ->
- req.device.getSubscription req.event, (options) ->
+ # Get subscriber subscription options
+ app.get '/subscriber/:subscriber_id/subscriptions/:event_id', authorize('register'), (req, res) ->
+ req.subscriber.getSubscription req.event, (options) ->
if options?
res.json {ignore_message: (options & req.event.OPTION_IGNORE_MESSAGE) isnt 0}
else
res.send 404
- # Subscribe a device to an event
- app.post '/device/:device_id/subscriptions/:event_id', authorize('register'), (req, res) ->
+ # Subscribe a subscriber to an event
+ app.post '/subscriber/:subscriber_id/subscriptions/:event_id', authorize('register'), (req, res) ->
options = 0
if req.body.ignore_message
options |= event.OPTION_IGNORE_MESSAGE
- req.device.addSubscription req.event, options, (added) ->
- if added? # added is null if device doesn't exist
+ req.subscriber.addSubscription req.event, options, (added) ->
+ if added? # added is null if subscriber doesn't exist
res.send if added then 201 else 204
else
res.send 404
- # Unsubscribe a device from an event
- app.delete '/device/:device_id/subscriptions/:event_id', authorize('register'), (req, res) ->
- res.device.removeSubscription req.event, (deleted) ->
+ # Unsubscribe a subscriber from an event
+ app.delete '/subscriber/:subscriber_id/subscriptions/:event_id', authorize('register'), (req, res) ->
+ res.subscriber.removeSubscription req.event, (deleted) ->
res.send if deleted then 204 else 404
# Event stats
View
@@ -1,4 +1,4 @@
-device = require './device'
+subscriber = require './subscriber'
async = require 'async'
Payload = require('./payload').Payload
@@ -52,9 +52,9 @@ class Event
cb(-1) if cb
return
- @forEachSubscribers (device, subOptions, done) =>
+ @forEachSubscribers (subscriber, subOptions, done) =>
# action
- @pushservices.push(device, subOptions, payload, done)
+ @pushservices.push(subscriber, subOptions, payload, done)
, (totalSubscribers) =>
# finished
if totalSubscribers > 0
@@ -72,9 +72,9 @@ class Event
cb(0) if cb
delete: (cb) ->
- @forEachSubscribers (device, subOptions, done) =>
+ @forEachSubscribers (subscriber, subOptions, done) =>
# action
- device.removeSubscription(@, done)
+ subscriber.removeSubscription(@, done)
, =>
# finished
@redis.multi()
@@ -85,19 +85,19 @@ class Event
.exec ->
cb() if cb
- # Performs an action on each device subsribed to this event
+ # Performs an action on each subscriber subsribed to this event
forEachSubscribers: (action, finished) ->
if @name is 'broadcast'
# if event is broadcast, do not treat score as subscription option, ignore it
- performAction = (deviceId, subOptions) =>
+ performAction = (subscriberId, subOptions) =>
return (done) =>
- action(device.getDevice(@redis, deviceId), 0, done)
+ action(subscriber.getSubscriber(@redis, subscriberId), 0, done)
else
- performAction = (deviceId, subOptions) =>
+ performAction = (subscriberId, subOptions) =>
return (done) =>
- action(device.getDevice(@redis, deviceId), subOptions, done)
+ action(subscriber.getSubscriber(@redis, subscriberId), subOptions, done)
- subscribersKey = if @name is 'boardcast' then 'devices' else "#{@key}:devs"
+ subscribersKey = if @name is 'boardcast' then 'subscribers' else "#{@key}:devs"
page = 0
perPage = 100
total = 0
@@ -108,12 +108,12 @@ class Event
, (done) =>
# treat subscribers by packs of 100 with async to prevent from blocking the event loop
# for too long on large subscribers lists
- @redis.zrange subscribersKey, (page++ * perPage), (page * perPage + perPage), 'WITHSCORES', (err, deviceIdsAndOptions) =>
+ @redis.zrange subscribersKey, (page++ * perPage), (page * perPage + perPage), 'WITHSCORES', (err, subscriberIdsAndOptions) =>
tasks = []
- for id, i in deviceIdsAndOptions by 2
- tasks.push performAction(id, deviceIdsAndOptions[i + 1])
+ for id, i in subscriberIdsAndOptions by 2
+ tasks.push performAction(id, subscriberIdsAndOptions[i + 1])
async.series tasks, =>
- total += deviceIdsAndOptions.length / 2
+ total += subscriberIdsAndOptions.length / 2
done()
, =>
# all done
View
@@ -7,22 +7,22 @@ c2dm = require 'c2dm'
class PushServiceAPNS
constructor: (conf, @logger) ->
conf.errorCallback = (errCode, note) =>
- @logger?.error("APNS Error #{errCode} for device #{note?.device?.id}")
+ @logger?.error("APNS Error #{errCode} for Subscriber #{note?.Subscriber?.id}")
@driver = new apns.Connection(conf)
- push: (device, subOptions, payload) ->
- device.get (info) =>
+ push: (Subscriber, subOptions, payload) ->
+ Subscriber.get (info) =>
note = new apns.Notification()
- note.device = new apns.Device(info.regid)
- note.device.id = device.id # used for error logging
+ note.Subscriber = new apns.Subscriber(info.regid)
+ note.Subscriber.id = Subscriber.id # used for error logging
if not (subOptions & event.OPTION_IGNORE_MESSAGE) and alert = payload.localizedMessage(info.lang)
note.alert = alert
note.badge = badge if not isNaN(badge = parseInt(info.badge) + 1)
note.sound = payload.sound
note.payload = payload.data
@driver.sendNotification note
# On iOS we have to maintain the badge counter on the server
- device.incr 'badge'
+ Subscriber.incr 'badge'
class PushServiceC2DM
@@ -38,14 +38,14 @@ class PushServiceC2DM
# Queue into an array waiting for C2DM login to complete
@queue = []
- push: (device, subOptions, payload) ->
+ push: (Subscriber, subOptions, payload) ->
@queue.push
- device: device,
+ Subscriber: Subscriber,
subOptions: subOptions,
payload: payload
_pushTask: (task, done) ->
- task.device.get (info) =>
+ task.Subscriber.get (info) =>
note =
registration_id: info.regid
collapse_key: task.payload.event.name
@@ -59,16 +59,16 @@ class PushServiceC2DM
done()
if err in ['InvalidRegistration', 'NotRegistered']
# Handle C2DM API feedback about no longer or invalid registrations
- @logger?.warn("C2DM Automatic unregistration for device #{task.device.id}")
- task.device.delete()
+ @logger?.warn("C2DM Automatic unregistration for Subscriber #{task.Subscriber.id}")
+ task.Subscriber.delete()
else if err
- @logger?.error("C2DM Error #{err} for device #{task.device.id}")
+ @logger?.error("C2DM Error #{err} for Subscriber #{task.Subscriber.id}")
class PushServiceMPNS
constructor: (@conf, @logger) ->
- push: (device, subOptions, payload) ->
+ push: (Subscriber, subOptions, payload) ->
# TO BE IMPLEMENTED
@@ -78,9 +78,9 @@ class PushServices
addService: (protocol, service) ->
@services[protocol] = service
- push: (device, subOptions, payload, cb) ->
- device.get (info) =>
- if info then @services[info.proto]?.push(device, subOptions, payload)
+ push: (Subscriber, subOptions, payload, cb) ->
+ Subscriber.get (info) =>
+ if info then @services[info.proto]?.push(Subscriber, subOptions, payload)
cb() if cb
exports.PushServices = PushServices
Oops, something went wrong.

0 comments on commit c5ce15f

Please sign in to comment.