Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
added rpc method
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 14, 2013
1 parent e53cb7c commit d088b8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/Client.coffee
Expand Up @@ -92,6 +92,25 @@ parseSearch = (iq) ->
items = iq.getChildren("item")
(new JID(i.text()).toString() for i in items)

addRPCElements = (iq, method, params=[]) ->
throw new TypeError unless typeof method is "string"
iq.c("methodCall").c("methodName").t(method).up()
if not (params instanceof Array)
console?.warn? "No parameters added: parameter is not an array"
return
if params.length > 0
iq.c("params")
for p in params
iq.c("param")
.cnode(joap.Serializer.serialize p).up().up()

parseRPCParams = (iq) ->
joap.Parser.parse iq
.getChild("methodResponse")
.getChild("params")
.getChild("param")
.getChild("value")

class JOAPClient

constructor: (@xmpp) ->
Expand Down Expand Up @@ -128,4 +147,9 @@ class JOAPClient
beforeSend: (iq) -> addXMLAttributes iq, attrs
onResult: parseSearch

methodCall: (method, address, params, cb) ->
sendRequest.call @, "query", address, cb,
beforeSend: (iq) -> addRPCElements iq, method, params
onResult: parseRPCParams

module.exports = JOAPClient
24 changes: 24 additions & 0 deletions spec/Client.spec.coffee
Expand Up @@ -158,3 +158,27 @@ describe "Client", ->
done = true

waitsFor -> done

describe "rpc method", ->
it "sends a correct iq", ->

done = false
iq = null
xmppComp.send = (req) ->
iq = req.tree()
(expect iq.getChild("query")
.getChild("methodCall")
.getChild("methodName").text()).toEqual "myMethod"
res = new ltx.Element "iq", type: 'result', id: iq.tree().attrs.id
res.c("query", xmlns: RPC_NS)
.c("methodResponse")
.c("params")
.c("param")
.c("value").c("int").t(7)
xmppComp.channels.stanza res.tree()

@c.methodCall "myMethod", "class@c.domain.tld", ["avalue"] , (err, s, res) ->
(expect res).toEqual 7
done = true

waitsFor -> done

0 comments on commit d088b8e

Please sign in to comment.