lib/blather/client/client.rb in blather-0.4.2 vs lib/blather/client/client.rb in blather-0.4.3
- old
+ new
@@ -76,11 +76,11 @@
##
# Start the connection.
def run
raise 'not setup!' unless setup?
klass = @setup[0].node ? Blather::Stream::Client : Blather::Stream::Component
- @stream = klass.start self, *@setup
+ klass.start self, *@setup
end
##
# Register a filter to be run before or after the handler chain is run.
# * +type+ - the type of filter. Must be +:before+ or +:after+
@@ -109,11 +109,11 @@
end
##
# Write data to the stream
def write(stanza)
- @stream.send(stanza) if @stream
+ self.stream.send(stanza)
end
##
# Helper that will create a temporary handler for the stanza being sent before writing it to the stream.
#
@@ -129,14 +129,16 @@
end
##
# Close the connection
def close
- @stream.close_connection_after_writing
+ self.stream.close_connection_after_writing
end
- def post_init # :nodoc:
+ def post_init(stream, jid = nil) # :nodoc:
+ @stream = stream
+ @jid = JID.new(jid) if jid
self.jid.node ? client_post_init : ready!
end
def unbind # :nodoc:
EM.stop if EM.reactor_running?
@@ -148,14 +150,10 @@
handle_stanza stanza
run_filters :after, stanza
end
end
- def jid=(new_jid) # :nodoc :
- @jid = JID.new new_jid
- end
-
def setup? # :nodoc:
@setup.is_a? Array
end
def setup(jid, password, host = nil, port = nil) # :nodoc:
@@ -165,10 +163,15 @@
@setup << port if port
self
end
protected
+ def stream
+ raise 'Stream not ready!' unless @stream
+ @stream
+ end
+
def check_handler(type, guards)
Blather.logger.warn "Handler for type \"#{type}\" will never be called as it's not a registered type" unless current_handlers.include?(type)
check_guards guards
end
@@ -231,14 +234,15 @@
catch(:pass) { call_handler handler, guards, stanza }
end
end
def call_handler(handler, guards, stanza) # :nodoc:
- if guards.first.respond_to?(:to_str) && !(result = stanza.find(*guards)).empty?
- handler.call(stanza, result)
- elsif !guarded?(guards, stanza)
- handler.call(stanza)
+ if guards.first.respond_to?(:to_str)
+ result = stanza.find(*guards)
+ handler.call(stanza, result) unless result.empty?
+ else
+ handler.call(stanza) unless guarded?(guards, stanza)
end
end
##
# If any of the guards returns FALSE this returns true
@@ -254,11 +258,12 @@
!guard.detect { |condition| !guarded?([condition], stanza) }
when Hash
# return FALSE unless any inequality is found
guard.find do |method, test|
value = stanza.__send__(method)
+ # last_match is the only method found unique to Regexp classes
if test.class.respond_to?(:last_match)
- !(test =~ value)
+ !(test =~ value.to_s)
elsif test.is_a?(Array)
!test.include? value
else
test != value
end