~singpolyma/sgx-jmp

b4b54eb17c49f380d5b2285fc1576b892fe62f8a — Stephen Paul Weber 10 months ago c7ebad1
Get OGM for a customer
4 files changed, 104 insertions(+), 0 deletions(-)

M lib/backend_sgx.rb
M lib/customer.rb
A lib/customer_ogm.rb
A test/test_customer_ogm.rb
M lib/backend_sgx.rb => lib/backend_sgx.rb +4 -0
@@ 33,6 33,10 @@ class BackendSgx
		end
	end

	def ogm_url
		REDIS.get("catapult_ogm_url-#{from_jid}")
	end

	def set_fwd_timeout(timeout)
		REDIS.set("catapult_fwd_timeout-#{from_jid}", timeout)
	end

M lib/customer.rb => lib/customer.rb +7 -0
@@ 5,6 5,7 @@ require "forwardable"
require_relative "./api"
require_relative "./blather_ext"
require_relative "./customer_info"
require_relative "./customer_ogm"
require_relative "./customer_plan"
require_relative "./customer_usage"
require_relative "./backend_sgx"


@@ 80,6 81,12 @@ class Customer
		stanza_to(iq, &IQ_MANAGER.method(:write)).then(&:vcard)
	end

	def ogm(from_tel=nil)
		@sgx.ogm_url.then do |url|
			CustomerOGM.for(url, -> { fetch_vcard_temp(from_tel) })
		end
	end

	def sip_account
		SipAccount.find(customer_id)
	end

A lib/customer_ogm.rb => lib/customer_ogm.rb +44 -0
@@ 0,0 1,44 @@
# frozen_string_literal: true

module CustomerOGM
	def self.for(url, fetch_vcard_temp)
		return Media.new(url) if url
		TTS.for(fetch_vcard_temp)
	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(fetch_vcard_temp)
			fetch_vcard_temp.call.then { |vcard|
				new(vcard)
			}.catch { new(Blather::Stanza::Iq::Vcard::Vcard.new) }
		end

		def initialize(vcard)
			@vcard = vcard
		end

		def [](k)
			value = @vcard[k]
			return if value.to_s.empty?
			value
		end

		def fn
			self["FN"] || self["NICKNAME"] || "a user of JMP.chat"
		end

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

A test/test_customer_ogm.rb => test/test_customer_ogm.rb +49 -0
@@ 0,0 1,49 @@
# frozen_string_literal: true

require "test_helper"
require "customer_ogm"

class CustomerOGMTest < Minitest::Test
	def test_for_url
		assert_kind_of(
			CustomerOGM::Media,
			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
		)
	end
	em :test_for_no_url

	class TTSTest < Minitest::Test
		def test_to_render_empty_vcard
			vcard = Blather::Stanza::Iq::Vcard::Vcard.new
			assert_equal(
				[:voicemail_ogm_tts, locals: { fn: "a user of JMP.chat" }],
				CustomerOGM::TTS.new(vcard).to_render
			)
		end

		def test_to_render_fn
			vcard = Blather::Stanza::Iq::Vcard::Vcard.new
			vcard["FN"] = "name"
			assert_equal(
				[:voicemail_ogm_tts, locals: { fn: "name" }],
				CustomerOGM::TTS.new(vcard).to_render
			)
		end

		def test_to_render_nickname
			vcard = Blather::Stanza::Iq::Vcard::Vcard.new
			vcard["NICKNAME"] = "name"
			assert_equal(
				[:voicemail_ogm_tts, locals: { fn: "name" }],
				CustomerOGM::TTS.new(vcard).to_render
			)
		end
	end
end