From 0702410a9a946687f895f68a9a34d6eacf8aad67 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Sat, 27 May 2017 14:00:06 -0500 Subject: [PATCH] Allow inserting Blather handler before others Mostly useful for extending the existing SGXcatapult application from elsewhere. --- sgx-catapult.rb | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/sgx-catapult.rb b/sgx-catapult.rb index 57319d08f3adc2a053da73a85105527c4bcb848d..410afc1352ebcf19560277ddd020bcad520cd322 100755 --- a/sgx-catapult.rb +++ b/sgx-catapult.rb @@ -48,15 +48,25 @@ end class SGXClient < Blather::Client def register_handler(type, *guards, &block) - super(type, *guards) do |stanza| - begin - v = block.call(stanza) - v.catch(&method(:panic)) if v.is_a?(Promise) - true # Do not run other handlers unless throw :pass - rescue Exception => e - panic(e) - end - end + super(type, *guards) { |stanza| wrap_handler(stanza, &block) } + end + + def register_handler_before(type, *guards, &block) + check_handler(type, guards) + handler = lambda { |stanza| wrap_handler(stanza, &block) } + + @handlers[type] ||= [] + @handlers[type].unshift([guards, handler]) + end + +protected + + def wrap_handler(stanza, &block) + v = block.call(stanza) + v.catch(&method(:panic)) if v.is_a?(Promise) + true # Do not run other handlers unless throw :pass + rescue Exception => e + panic(e) end end @@ -81,6 +91,10 @@ module SGXcatapult client.write(stanza) end + def self.before_handler(type, *guards, &block) + client.register_handler_before(type, *guards, &block) + end + def self.send_media(from, to, media_url, desc=nil, subject=nil) # we assume media_url is of the form (always the case so far): # https://api.catapult.inetwork.com/v1/users/[uid]/media/[file]