diff --git a/lib/customer.rb b/lib/customer.rb index 1147106a469309e95eb555e1203f092c97d2be74..547c7023df426a443384127282908414109de7db 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -117,7 +117,8 @@ class Customer end def ogm(from_tel=nil) - CustomerOGM.for(@sgx.ogm_url, -> { fetch_pep("urn:xmpp:vcard4", from_tel) }) + fetch_vcard = -> { fetch_pep("urn:xmpp:vcard4", from_tel) } + CustomerOGM.for(@sgx.ogm_url, @sgx.registered?.phone, fetch_vcard) end def sip_account diff --git a/lib/customer_ogm.rb b/lib/customer_ogm.rb index 23008e1c8c287abbc6c42c3873acf741b765b068..991dfc46e89fd680cd5a66aeec6164ed64dc32cc 100644 --- a/lib/customer_ogm.rb +++ b/lib/customer_ogm.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true module CustomerOGM - def self.for(url, fetch_vcard) + def self.for(url, tel, fetch_vcard) return Media.new(url) if url - TTS.for(fetch_vcard) + TTS.for(tel, fetch_vcard) end class Media @@ -18,13 +18,14 @@ module CustomerOGM end class TTS - def self.for(fetch_vcard) + def self.for(tel, fetch_vcard) fetch_vcard.call.then { |vcard| - new(vcard.first.payload_node) - }.catch { new(Nokogiri::XML::Document.new) } + new(tel, vcard.first.payload_node) + }.catch { new(tel, Nokogiri::XML::Document.new) } end - def initialize(vcard) + def initialize(tel, vcard) + @tel = tel @vcard = vcard end @@ -39,7 +40,11 @@ module CustomerOGM end def fn - self["FN"] || self["NICKNAME"] || "a user of JMP.chat" + self["FN"] || self["NICKNAME"] || formatted_tel + end + + def formatted_tel + @tel.sub(/\A\+?1?(\d{3})(\d{3})/, "(\\1) \\2-") end def to_render diff --git a/test/test_customer_ogm.rb b/test/test_customer_ogm.rb index 2c4e8309f0b269e40221c80fdd324b694b81fe12..3093ad5436eb542f33b42532920f89a5aa32c583 100644 --- a/test/test_customer_ogm.rb +++ b/test/test_customer_ogm.rb @@ -7,14 +7,14 @@ class CustomerOGMTest < Minitest::Test def test_for_url assert_kind_of( CustomerOGM::Media, - CustomerOGM.for("https://example.com/test.mp3", -> {}) + CustomerOGM.for("https://example.com/test.mp3", "", -> {}) ) end def test_for_no_url assert_kind_of( CustomerOGM::TTS, - CustomerOGM.for(nil, -> { EMPromise.resolve(nil) }).sync + CustomerOGM.for(nil, "", -> { EMPromise.resolve(nil) }).sync ) end em :test_for_no_url @@ -26,8 +26,8 @@ class CustomerOGMTest < Minitest::Test XML assert_equal( - [:voicemail_ogm_tts, { locals: { fn: "a user of JMP.chat" } }], - CustomerOGM::TTS.new(vcard).to_render + [:voicemail_ogm_tts, { locals: { fn: "(555) 123-4567" } }], + CustomerOGM::TTS.new("+15551234567", vcard).to_render ) end @@ -39,7 +39,7 @@ class CustomerOGMTest < Minitest::Test XML assert_equal( [:voicemail_ogm_tts, { locals: { fn: "name" } }], - CustomerOGM::TTS.new(vcard).to_render + CustomerOGM::TTS.new("", vcard).to_render ) end @@ -51,7 +51,7 @@ class CustomerOGMTest < Minitest::Test XML assert_equal( [:voicemail_ogm_tts, { locals: { fn: "name" } }], - CustomerOGM::TTS.new(vcard).to_render + CustomerOGM::TTS.new("", vcard).to_render ) end end