lib/blather/client/client.rb in sprsquish-blather-0.3.4 vs lib/blather/client/client.rb in sprsquish-blather-0.4.0
- old
+ new
@@ -38,11 +38,12 @@
def setup?
@setup.is_a? Array
end
def setup(jid, password, host = nil, port = nil)
- @setup = [JID.new(jid), password]
+ @jid = JID.new(jid)
+ @setup = [@jid, password]
@setup << host if host
@setup << port if port
self
end
@@ -61,25 +62,20 @@
@handlers[type] ||= []
@handlers[type] << [guards, handler]
end
def write(stanza)
- stanza.from ||= jid if stanza.respond_to?(:from)
@stream.send(stanza) if @stream
end
def write_with_handler(stanza, &handler)
register_tmp_handler stanza.id, &handler
write stanza
end
def post_init
- case @stream
- when Stream::Component then ready!
- when Stream::Client then client_post_init
- else raise "Don't know #{@stream.class} stream type. How the hell did this happen!?"
- end
+ self.jid.node ? client_post_init : ready!
end
def close
@stream.close_connection_after_writing
end
@@ -91,11 +87,11 @@
def receive_data(stanza)
if handler = @tmp_handlers.delete(stanza.id)
handler.call stanza
else
stanza.handler_heirarchy.each do |type|
- break if call_handler_for(type, stanza) && (stanza.is_a?(BlatherError) || stanza.type == :iq)
+ break if call_handler_for(type, stanza)# && (stanza.is_a?(BlatherError) || stanza.type == :iq)
end
end
end
protected
@@ -130,12 +126,19 @@
end
end
def call_handler_for(type, stanza)
if @handlers[type]
- @handlers[type].find { |guards, handler| handler.call(stanza) unless guarded?(guards, stanza) }
- true
+ @handlers[type].find do |guards, handler|
+ if guards.first.is_a?(String)
+ unless (result = stanza.find(*guards)).empty?
+ handler.call(stanza, result)
+ end
+ elsif !guarded?(guards, stanza)
+ handler.call(stanza)
+ end
+ end
end
end
##
# If any of the guards returns FALSE this returns true
@@ -169,12 +172,15 @@
end
def check_guards(guards)
guards.each do |guard|
case guard
- when Array then guard.each { |g| check_guards([g]) }
- when Symbol, Proc, Hash then nil
- else raise "Bad guard: #{guard.inspect}"
+ when Array
+ guard.each { |g| check_guards([g]) }
+ when Symbol, Proc, Hash, String
+ nil
+ else
+ raise "Bad guard: #{guard.inspect}"
end
end
end
end #Client