~singpolyma/sgx-jmp

ref: 4be555de103e992ee7e03feb48b45c1eca917c45 sgx-jmp/lib/ibr.rb -rw-r--r-- 1009 bytes
4be555deStephen Paul Weber Split logic out into testable objects 1 year, 7 months 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
# frozen_string_literal: true

require "blather"

class IBR < Blather::Stanza::Iq::Query
	register :ibr, nil, "jabber:iq:register"

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

	def registered?
		!!query.at_xpath("./ns:registered", ns: self.class.registered_ns)
	end

	[
		"instructions",
		"username",
		"nick",
		"password",
		"name",
		"first",
		"last",
		"email",
		"address",
		"city",
		"state",
		"zip",
		"phone",
		"url",
		"date"
	].each do |tag|
		define_method("#{tag}=") do |v|
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.remove
			node = Nokogiri::XML::Node.new(tag, document)
			node.default_namespace = self.class.registered_ns
			node.content = v
			query << node
		end

		define_method(tag) do
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.content
		end
	end
end