~singpolyma/sgx-jmp

e8c2919d77d49b35b3f0ac8bd1ca1a5c0305cef9 — Stephen Paul Weber 3 years ago 15ca96c
Add reference to option to show where the tel is
2 files changed, 34 insertions(+), 4 deletions(-)

M lib/tel_selections.rb
M test/test_tel_selections.rb
M lib/tel_selections.rb => lib/tel_selections.rb +21 -3
@@ 110,13 110,31 @@ class TelSelections
				@region = state
			end

			def formatted_tel
				@tel =~ /\A\+1(\d{3})(\d{3})(\d+)\Z/
				"(#{$1}) #{$2}-#{$3}"
			end

			def option
				{ value: tel, label: to_s }
				op = Blather::Stanza::X::Field::Option.new(value: tel, label: to_s)
				op << reference
				op
			end

			def reference
				Nokogiri::XML::Builder.new { |xml|
					xml.reference(
						xmlns: "urn:xmpp:reference:0",
						begin: 0,
						end: formatted_tel.length - 1,
						type: "data",
						uri: "tel:#{tel}"
					)
				}.doc.root
			end

			def to_s
				@tel =~ /\A\+1(\d{3})(\d{3})(\d+)\Z/
				"(#{$1}) #{$2}-#{$3} (#{@locality}, #{@region})"
				"#{formatted_tel} (#{@locality}, #{@region})"
			end
		end


M test/test_tel_selections.rb => test/test_tel_selections.rb +13 -1
@@ 75,10 75,22 @@ class TelSelectionsTest < Minitest::Test

		def test_option
			assert_equal(
				{ label: "(555) 123-4567 (Toronto, ON)", value: "+15551234567" },
				Blather::Stanza::X::Field::Option.new(
					label: "(555) 123-4567 (Toronto, ON)",
					value: "+15551234567"
				),
				@tn.option
			)
		end

		def test_option_reference
			ref = @tn.option.find("ns:reference", ns: "urn:xmpp:reference:0").first
			assert_equal(
				@tn.formatted_tel,
				@tn.option.label[ref["begin"].to_i..ref["end"].to_i]
			)
			assert_equal "tel:+15551234567", ref["uri"]
		end
	end

	class QTest < Minitest::Test