Permalink
Browse files

improved manager and added tests

  • Loading branch information...
1 parent 031caef commit 1c438514a6594c1882c222985d1bf8b605b31aaf @flosse committed Jan 4, 2012
Showing with 325 additions and 30 deletions.
  1. +1 −2 README.markdown
  2. +19 −6 lib/Manager.coffee
  3. +5 −22 lib/Router.coffee
  4. +1 −0 lib/node-xmpp-joap.coffee
  5. +23 −0 lib/stanza.coffee
  6. +276 −0 spec/Manager.spec.coffee
View
@@ -25,8 +25,7 @@ comp = new xmpp.Component
port : "8888"
class User
- constructor: (params) ->
- { @name, @age } = params
+ constructor: (@name, @age) ->
# create a new manager instance
mgr = new joap.Manager comp
View
@@ -12,6 +12,10 @@ class Manager extends events.EventEmitter
@router.on "edit", @onEdit
@router.on "delete", @onDelete
+ @getArgNames: (fn) ->
+ args = fn.toString().match(/function\b[^(]*\(([^)]*)\)/)[1]
+ args.split /\s*,\s*/
+
addClass: (name, creator, required=[], protected=[]) ->
if typeof creator is "function" and not @classes[name]?
@classes[name] = { creator:creator, required:required, protected:protected }
@@ -21,14 +25,15 @@ class Manager extends events.EventEmitter
createClass: (a) ->
clazz = @classes[a.class]
- x = new clazz.creator a.attributes
+ argNames = Manager.getArgNames clazz.creator
+ x = new clazz.creator (a.attributes[n] for n in argNames when n isnt "")...
if not x.id or @objects[a.class][x.id]?
x.id = joap.uniqueId()
@objects[a.class][x.id] = x
"#{a.class}@#{@router.xmpp.jid}/#{x.id}"
onRead: (a) =>
- if @grant(a) and @classExists(a) and @instanceExists(a)
+ if @grant(a) and @classExists(a) and @instanceExists(a) and @areExistingAttributes(a)
res = {}
inst = @objects[a.class][a.instance]
if a.limits
@@ -63,19 +68,19 @@ class Manager extends events.EventEmitter
instanceExists: (a) ->
if not @objects[a.class]?[a.instance]?
- @router.sendError a, 404, "Object '#{a.instance}' does not exits."
+ @router.sendError a, 404, "Object '#{a.instance}' does not exists"
false
else true
classExists: (a) ->
if not @classes[a.class]?
- @router.sendError a, 404, "Class '#{a.class}' does not exits."
+ @router.sendError a, 404, "Class '#{a.class}' does not exists"
false
else true
isClassAddress: (a) ->
if not a.class? or a.instance?
- @router.sendError a, 405, "'#{a.iq.attrs.to}' is not a class"
+ @router.sendError a, 405, "'#{a.iq.attrs.to}' isn't a class"
false
else true
@@ -92,11 +97,19 @@ class Manager extends events.EventEmitter
return false
true
+ areExistingAttributes: (a) ->
+ if a.limits?
+ inst = @objects[a.class][a.instance]
+ for l in a.limits
+ if inst[l] is undefined
+ @router.sendError a, 406, "Requested attribute '#{l}' doesn't exists"
+ return false
+ true
areWritableAttributes: (a) ->
p = @classes[a.class].protected
for k,v of a.attributes
if k in p
- @router.sendError a, 406, "'#{k}' of '#{a.class}' is not a writeable attribute"
+ @router.sendError a, 406, "Attribute '#{k}' of class '#{a.class}' is not writeable"
return false
true
View
@@ -27,34 +27,17 @@ class Router extends events.EventEmitter
@emit "action", action
sendError: (a, code, msg) ->
-
- err = new ltx.Element "iq",
- id: a.iq.attrs.id
- type:'error'
- to: a.iq.attrs.from
+ @xmpp.send new joap.ErrorIq a.type, code, msg,
+ to: a.iq.attrs.from
from: a.iq.attrs.to
-
- if a.type isnt "rpc"
- if a.type in JOAP_STANZAS
- err.c(a.type, xmlns: JOAP_NS).up()
- err
- .c("error", code: code)
- .t(msg)
- else if a.type is "rpc"
- err
- .c("query", xmlns: RPC_NS)
- .c("methodResponse")
- .c("fault")
- .cnode(new joap.Value { faultCode: code, faultString: msg })
-
- @xmpp.send err
+ id: a.iq.attrs.id
sendResponse: (a, data) ->
res = new ltx.Element "iq",
- id: a.iq.attrs.id
- type:'result'
to: a.iq.attrs.from
from: a.iq.attrs.to
+ id: a.iq.attrs.id
+ type:'result'
res.cnode joap.serialize(data, a)
@xmpp.send res
@@ -8,6 +8,7 @@ exports.Member = stanza.Member
exports.Struct = stanza.Struct
exports.Array = stanza.Array
exports.Value = stanza.Value
+exports.ErrorIq = stanza.ErrorIq
Parser = require("./Parser").Parser
View
@@ -1,5 +1,7 @@
ltx = require "ltx"
joap = require "./node-xmpp-joap"
+JOAP_NS = "jabber:iq:joap"
+RPC_NS = "jabber:iq:rpc"
class KeyVal extends ltx.Element
constructor: (name, key, value) ->
@@ -36,8 +38,29 @@ class Value extends ltx.Element
super "value"
@cnode(joap.serialize val)
+class ErrorIq extends ltx.Element
+ constructor: (type, code, msg, attrs) ->
+ super "iq", attrs
+ @attrs.type = 'error'
+
+ if type isnt "rpc"
+ @c(type, xmlns: JOAP_NS).up()
+ @cnode new Error code, msg
+
+ else if type is "rpc"
+ @c("query", xmlns: RPC_NS)
+ @c("methodResponse")
+ @c("fault")
+ @cnode(new Value { faultCode: code, faultString: msg })
+
+class Error extends ltx.Element
+ constructor: (code, msg) ->
+ super "error", {code:code}
+ @t msg
+
exports.Attribute = Attribute
exports.Member = Member
exports.Struct = Struct
exports.Array = Array
exports.Value = Value
+exports.ErrorIq = ErrorIq
Oops, something went wrong.

0 comments on commit 1c43851

Please sign in to comment.