~singpolyma/sgx-jmp

sgx-jmp/lib/oob.rb -rw-r--r-- 946 bytes
c4fc3de0Stephen Paul Weber Revert "Merge branch 'limit-top-up-freq'" a day 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
# frozen_string_literal: true

require "blather"

class OOB < Blather::XMPPNode
	register :oob, "jabber:x:oob"

	def self.new(url=nil, desc: nil)
		new_node = super :x

		case url
		when Nokogiri::XML::Node
			new_node.inherit url
		else
			new_node.url = url if url
			new_node.desc = desc if desc
		end

		new_node
	end

	def self.find_or_create(parent)
		if (found_x = parent.find("ns:x", ns: registered_ns).first)
			x = new(found_x)
			found_x.remove
		else
			x = new
		end
		parent << x
		x
	end

	def url
		find("ns:url", ns: self.class.registered_ns).first&.content
	end

	def url=(url)
		remove_children :url
		i = Niceogiri::XML::Node.new(:url, document, namespace)
		i.content = url
		self << i
	end

	def desc
		find("ns:desc", ns: self.class.registered_ns).first&.content
	end

	def desc=(description)
		remove_children :desc
		i = Niceogiri::XML::Node.new(:desc, document, namespace)
		i.content = description
		self << i
	end
end