From bbc0bb6a11c1d459a1511e88bd80997d158e2603 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 30 Jun 2021 21:38:56 -0500 Subject: [PATCH] Refactor command list to use composition Also rename buy credit to top up --- lib/command_list.rb | 53 +++++++++++++++++++++------------------ sgx_jmp.rb | 2 +- test/test_command_list.rb | 16 ++++++------ 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/lib/command_list.rb b/lib/command_list.rb index 4e69b47..fc75baa 100644 --- a/lib/command_list.rb +++ b/lib/command_list.rb @@ -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 diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 2272983..bc91715 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -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] diff --git a/test/test_command_list.rb b/test/test_command_list.rb index 7741a3f..3281a15 100644 --- a/test/test_command_list.rb +++ b/test/test_command_list.rb @@ -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( -- 2.34.5