~singpolyma/sgx-jmp

sgx-jmp/lib/snikket.rb -rw-r--r-- 2.5 KiB
4ace3417Stephen Paul Weber Log errors from rev.ai 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

require "blather"

module Snikket
	class Launch < Blather::Stanza::Iq
		register nil, "launch", "xmpp:snikket.org/hosting/v1"

		def self.new(type=nil, to=nil, id=nil, domain: nil)
			stanza = super(type || :set, to, id)
			node = Nokogiri::XML::Node.new("launch", stanza.document)
			node.default_namespace = registered_ns
			stanza << node
			stanza.domain = domain if domain
			stanza
		end

		def domain=(domain)
			query.at_xpath("./ns:domain", ns: self.class.registered_ns)&.remove
			node = Nokogiri::XML::Node.new("domain", document)
			node.default_namespace = self.class.registered_ns
			node.content = domain
			query << node
		end

		def query
			at_xpath("./ns:launch", ns: self.class.registered_ns)
		end
	end

	class Launched < Blather::Stanza::Iq
		register :snikket_launched, "launched", "xmpp:snikket.org/hosting/v1"

		def instance_id
			query
				.at_xpath("./ns:instance-id", ns: self.class.registered_ns)
				&.content
		end

		def bootstrap_token
			query
				.at_xpath("./ns:bootstrap/ns:token", ns: self.class.registered_ns)
				&.content
		end

		def bootstrap_uri(instance_domain)
			"https://#{instance_domain}/invites_bootstrap?token=#{bootstrap_token}"
		end

		def query
			at_xpath("./ns:launched", ns: self.class.registered_ns)
		end
	end

	class Instance < Blather::Stanza::Iq
		register :snikket_instance, "instance", "xmpp:snikket.org/hosting/v1"

		def self.new(type=nil, to=nil, id=nil, instance_id: nil)
			stanza = super(type || :get, to, id)
			node = Nokogiri::XML::Node.new("instance", stanza.document)
			node.default_namespace = registered_ns
			stanza << node
			stanza.instance_id = instance_id if instance_id
			stanza
		end

		def inherit(*)
			query.remove
			super
		end

		def instance_id=(instance_id)
			query.at_xpath("./ns:instance-id", ns: self.class.registered_ns)&.remove
			node = Nokogiri::XML::Node.new("instance-id", document)
			node.default_namespace = self.class.registered_ns
			node.content = instance_id
			query << node
		end

		def instance_id
			query
				.at_xpath("./ns:instance-id", ns: self.class.registered_ns)
				&.content
		end

		def update_needed?
			!!query.at_xpath("./ns:update-needed", ns: self.class.registered_ns)
		end

		def operation
			query.at_xpath("./ns:operation", ns: self.class.registered_ns)
		end

		def status
			query
				.at_xpath("./ns:status", ns: self.class.registered_ns)
				&.content&.to_sym
		end

		def query
			at_xpath("./ns:instance", ns: self.class.registered_ns)
		end
	end
end