From e8c2919d77d49b35b3f0ac8bd1ca1a5c0305cef9 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 31 Aug 2021 14:09:40 -0500 Subject: [PATCH] Add reference to option to show where the tel is --- lib/tel_selections.rb | 24 +++++++++++++++++++++--- test/test_tel_selections.rb | 14 +++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/tel_selections.rb b/lib/tel_selections.rb index 8cc558c..d297c0a 100644 --- a/lib/tel_selections.rb +++ b/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 diff --git a/test/test_tel_selections.rb b/test/test_tel_selections.rb index 78cae2e..69e6ecc 100644 --- a/test/test_tel_selections.rb +++ b/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 -- 2.38.4