~singpolyma/sgx-jmp

ref: 023c87a11bbaa6bd3eaeb3977330b8ea80ded0cd sgx-jmp/lib/xep0122_field.rb -rw-r--r-- 996 bytes
023c87a1Stephen Paul Weber Quieter voicemail beep 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
# frozen_string_literal: true

require "blather"
require "nokogiri"

class XEP0122Field
	attr_reader :field

	def initialize(type, range: nil, **field)
		@type = type
		@range = range
		@field = Blather::Stanza::X::Field.new(**field)
		@field.add_child(validate)
	end

protected

	def validate
		validate = Nokogiri::XML::Node.new("validate", field.document)
		validate.default_namespace = "http://jabber.org/protocol/xdata-validate"
		validate["datatype"] = @type
		validate.add_child(validation)
		validate
	end

	def validation
		range_node || Nokogiri::XML::Node.new(
			"basic",
			field.document
		).tap do |basic|
			basic.default_namespace = "http://jabber.org/protocol/xdata-validate"
		end
	end

	def range_node
		return unless @range

		Nokogiri::XML::Node.new("range", field.document).tap do |range|
			range.default_namespace = "http://jabber.org/protocol/xdata-validate"
			range["min"] = @range.min.to_s if @range.min
			range["max"] = @range.max.to_s if @range.max
		end
	end
end