~singpolyma/sgx-jmp

bbc0bb6a11c1d459a1511e88bd80997d158e2603 — Stephen Paul Weber 2 years ago 847be4f
Refactor command list to use composition

Also rename buy credit to top up
3 files changed, 37 insertions(+), 34 deletions(-)

M lib/command_list.rb
M sgx_jmp.rb
M test/test_command_list.rb
M lib/command_list.rb => lib/command_list.rb +28 -25
@@ 22,40 22,43 @@ class CommandList
				REDIS.get("catapult_fwd-#{tel}"),
				customer.plan_name ? customer.payment_methods : []
			]).then do |(fwd, payment_methods)|
				klass = Class.new(Registered)
				klass.include(HasBilling) unless payment_methods.empty?
				klass.include(HasForwarding) if fwd
				klass.new
				Registered.new(*[
					(HAS_CREDIT_CARD unless payment_methods.empty?),
					(HAS_FORWARDING if fwd)
				].compact)
			end
		end

		def each
			super
			yield node: "number-display", name: "Display JMP Number"
			yield node: "configure-calls", name: "Configure Calls"
			yield node: "usage", name: "Show Monthly Usage"
			yield node: "reset sip account", name: "Create or Reset SIP Account"
			yield(
		def initialize(*args)
			@extra = args
		end

		ALWAYS = [
			{ node: "number-display", name: "Display JMP Number" },
			{ node: "configure-calls", name: "Configure Calls" },
			{ node: "usage", name: "Show Monthly Usage" },
			{ node: "reset sip account", name: "Create or Reset SIP Account" },
			{
				node: "credit cards",
				name: "Credit Card Settings and Management"
			)
		end
	end
			}
		].freeze

	module HasForwarding
		def each
			super
			yield(
				node: "record-voicemail-greeting",
				name: "Record Voicemail Greeting"
			)
			([ALWAYS] + @extra).each do |commands|
				commands.each { |x| yield x }
			end
		end
	end

	module HasBilling
		def each
			super
			yield node: "buy credit", name: "Buy account credit"
		end
	end

	HAS_FORWARDING = [
		node: "record-voicemail-greeting",
		name: "Record Voicemail Greeting"
	].freeze

	HAS_CREDIT_CARD = [
		node: "top up", name: "Buy Account Credit by Credit Card"
	].freeze
end

M sgx_jmp.rb => sgx_jmp.rb +1 -1
@@ 344,7 344,7 @@ command :execute?, node: "credit cards", sessionid: nil do |iq|
	}.catch { |e| panic(e, sentry_hub) }
end

command :execute?, node: "buy credit", sessionid: nil do |iq|
command :execute?, node: "top up", sessionid: nil do |iq|
	sentry_hub = new_sentry_hub(iq, name: iq.node)
	reply = iq.reply
	reply.allowed_actions = [:complete]

M test/test_command_list.rb => test/test_command_list.rb +8 -8
@@ 63,14 63,14 @@ class CommandListTest < Minitest::Test
			)),
			["registered"]
		)
		assert_kind_of(
			CommandList::HasForwarding,
			CommandList.for("registered").sync
		assert_equal(
			CommandList::HAS_FORWARDING,
			CommandList::HAS_FORWARDING & CommandList.for("registered").sync.to_a
		)
	end
	em :test_for_registered_with_fwd

	def test_for_registered_with_billing
	def test_for_registered_with_credit_card
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),


@@ 85,12 85,12 @@ class CommandListTest < Minitest::Test
			)),
			["registered"]
		)
		assert_kind_of(
			CommandList::HasBilling,
			CommandList.for("registered").sync
		assert_equal(
			CommandList::HAS_CREDIT_CARD,
			CommandList::HAS_CREDIT_CARD & CommandList.for("registered").sync.to_a
		)
	end
	em :test_for_registered_with_billing
	em :test_for_registered_with_credit_card

	def test_for_registered_with_forwarding_and_billing
		CommandList::REDIS.expect(