Add reference to option to show where the tel is

Stephen Paul Weber created

Change summary

lib/tel_selections.rb       | 24 +++++++++++++++++++++---
test/test_tel_selections.rb | 14 +++++++++++++-
2 files changed, 34 insertions(+), 4 deletions(-)

Detailed changes

lib/tel_selections.rb 🔗

@@ -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
 

test/test_tel_selections.rb 🔗

@@ -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