~singpolyma/sgx-jmp

sgx-jmp/lib/customer_ogm.rb -rw-r--r-- 953 bytes
94298f5dStephen Paul Weber Refactor alt top up to use FormTemplate 2 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
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module CustomerOGM
	def self.for(url, tel, fetch_vcard)
		return Media.new(url) if url

		TTS.for(tel, fetch_vcard)
	end

	class Media
		def initialize(url)
			@url = url
		end

		def to_render
			[:voicemail_ogm_media, { locals: { url: @url } }]
		end
	end

	class TTS
		def self.for(tel, fetch_vcard)
			fetch_vcard.call.then { |vcard|
				new(tel, vcard.first.payload_node)
			}.catch { new(tel, Nokogiri::XML::Document.new) }
		end

		def initialize(tel, vcard)
			@tel = tel
			@vcard = vcard
		end

		def [](k)
			value = @vcard.find_first(
				"./ns:#{k.downcase}/ns:text",
				ns: "urn:ietf:params:xml:ns:vcard-4.0"
			)
			return if !value || value.content.empty?

			value.content
		end

		def fn
			self["FN"] || self["NICKNAME"] || formatted_tel
		end

		def formatted_tel
			@tel.sub(/\A\+?1?(\d{3})(\d{3})/, "(\\1) \\2-")
		end

		def to_render
			[:voicemail_ogm_tts, { locals: { fn: fn } }]
		end
	end
end