lib/blather/stream/resource.rb in blather-0.2.1 vs lib/blather/stream/resource.rb in blather-0.2.2

- old
+ new

@@ -1,50 +1,47 @@ module Blather # :nodoc: -module Stream # :nodoc: +class Stream # :nodoc: - class Resource # :nodoc: + class Resource < StreamHandler # :nodoc: def initialize(stream, jid) - @stream = stream + super stream @jid = jid - @callbacks = {} end - def success(&callback) - @callbacks[:success] = callback - end - - def failure(&callback) - @callbacks[:failure] = callback - end - - def receive(node) - @node = node - __send__(@node.element_name == 'iq' ? @node['type'] : @node.element_name) - end - + private + ## + # Respond to the bind request + # If @jid has a resource set already request it from the server def bind + response = Stanza::Iq.new :set + @id = response.id + binder = XMPPNode.new('bind') - binder.xmlns = 'urn:ietf:params:xml:ns:xmpp-bind' + binder.namespace = 'urn:ietf:params:xml:ns:xmpp-bind' binder << XMPPNode.new('resource', @jid.resource) if @jid.resource - response = Stanza::Iq.new :set - @id = response.id response << binder - @stream.send response end + ## + # Process the result from the server + # Sets the sends the JID (now bound to a resource) + # back to the stream def result LOG.debug "RESOURE NODE #{@node}" + # ensure this is a response to our original request if @id == @node['id'] - @jid = JID.new @node.find_first('bind').content_from(:jid) - @callbacks[:success].call(@jid) if @callbacks[:success] + @jid = JID.new @node.find_first('bind/jid').content + success @jid end end + ## + # Server returned an error def error - @callbacks[:failure].call if @callbacks[:failure] + failure StanzaError.import(@node) end end #Resource end #Stream end #Blather