~singpolyma/jmp-pay

2ac3560afa1cfd5ebceee2b5f38d4c8b9679f9e8 — Christopher Vollick 1 year, 5 months ago 9ab992d
Add PubSub Helpers to BlatherNotify

Since we think we may end up struturing a few scripts to push things
into a PubSub node, I wanted to make that as easy as possible.

So the way this works is that at startup we can give it a pubsub node
and server, and then we can call `BlatherNotify.publish` from then on
giving it the thing we want to publish and it'll just push it to the
node we configured.

Easy.

But if we run into a situation where we want to publish to 2 or more
different pubsub nodes in different situations, we can instead call
`BlatherNotify.pubsub(node, server)` and it'll return us a non-global
thing we can call publish on which will publish to that node
specifically.
1 files changed, 47 insertions(+), 1 deletions(-)

M lib/blather_notify.rb
M lib/blather_notify.rb => lib/blather_notify.rb +47 -1
@@ 7,13 7,45 @@ require "timeout"
module BlatherNotify
	extend Blather::DSL

	class PubSub
		class Address
			attr_reader :node, :server

			def initialize(node:, server:)
				@node = node
				@server = server
			end

			def to_uri
				"xmpp:#{@server}?;node=#{@node}"
			end
		end

		def initialize(blather, addr)
			@blather = blather
			@addr = addr
		end

		def publish(xml)
			@blather.write_with_promise(
				Blather::Stanza::PubSub::Publish.new(
					@addr.server,
					@addr.node,
					:set,
					xml
				)
			)
		end
	end

	@ready = Queue.new

	when_ready { @ready << :ready }

	def self.start(jid, password)
	def self.start(jid, password, default_pubsub_addr: nil)
		# workqueue_count MUST be 0 or else Blather uses threads!
		setup(jid, password, nil, nil, nil, nil, workqueue_count: 0)
		set_default_pubsub(default_pubsub_addr)

		EM.error_handler(&method(:panic))



@@ 81,4 113,18 @@ module BlatherNotify
			write_with_promise(command(command_node, iq.sessionid, form: form))
		end
	end

	def self.pubsub(addr)
		PubSub.new(self, addr)
	end

	def self.set_default_pubsub(addr)
		@default_pubsub = addr && pubsub(addr)
	end

	def self.publish(xml)
		raise "No default pubsub set!" unless @default_pubsub

		@default_pubsub.publish(xml)
	end
end