From 1c269719d83b57d0f6080c34092da6c0cbf7905f Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 26 Jul 2021 14:18:54 -0500 Subject: [PATCH] Show current balance in alt top up form --- lib/alt_top_up_form.rb | 13 +++++++++++-- test/test_add_bitcoin_address.rb | 6 ++++-- test/test_alt_top_up_form.rb | 14 +++++++++----- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/alt_top_up_form.rb b/lib/alt_top_up_form.rb index 743aff7..45eabb1 100644 --- a/lib/alt_top_up_form.rb +++ b/lib/alt_top_up_form.rb @@ -3,7 +3,7 @@ class AltTopUpForm def self.for(customer) customer.btc_addresses.then do |addrs| - AltTopUpForm.new(*[ + AltTopUpForm.new(customer, *[ (IS_CAD if customer.currency == :CAD), (HasBitcoinAddresses.new(addrs) unless addrs.empty?), AddBtcAddressField.for(addrs) @@ -11,8 +11,9 @@ class AltTopUpForm end end - def initialize(*fields) + def initialize(customer, *fields) @fields = fields + @balance = customer.balance end def form @@ -35,6 +36,13 @@ class AltTopUpForm } end + def balance + { + type: "fixed", + value: "Current balance: $#{'%.4f' % @balance}" + } + end + MAILING_ADDRESS = { var: "adr", type: "fixed", @@ -47,6 +55,7 @@ class AltTopUpForm def fields Enumerator.new do |y| + y << balance y << MAILING_ADDRESS @fields.each do |fs| fs.each { |f| y << f } diff --git a/test/test_add_bitcoin_address.rb b/test/test_add_bitcoin_address.rb index 8690d14..c26b19b 100644 --- a/test/test_add_bitcoin_address.rb +++ b/test/test_add_bitcoin_address.rb @@ -7,13 +7,15 @@ require "add_bitcoin_address" class AddBitcoinAddressTest < Minitest::Test def test_for iq = Blather::Stanza::Iq::Command.new - AddBitcoinAddress.for(iq, AltTopUpForm.new, Customer.new("test")) + customer = Customer.new("test") + AddBitcoinAddress.for(iq, AltTopUpForm.new(customer), customer) end def test_for_add_bitcoin iq = Blather::Stanza::Iq::Command.new iq.form.fields = [{ var: "add_btc_address", value: "true" }] - AddBitcoinAddress.for(iq, AltTopUpForm.new, Customer.new("test")) + customer = Customer.new("test") + AddBitcoinAddress.for(iq, AltTopUpForm.new(customer), customer) end def test_write diff --git a/test/test_alt_top_up_form.rb b/test/test_alt_top_up_form.rb index 4a844af..1bfbadd 100644 --- a/test/test_alt_top_up_form.rb +++ b/test/test_alt_top_up_form.rb @@ -47,7 +47,10 @@ class AltTopUpFormTest < Minitest::Test def test_form_addrs assert_kind_of( Blather::Stanza::X, - AltTopUpForm.new(AltTopUpForm::AddBtcAddressField.new).form + AltTopUpForm.new( + Customer.new("test"), + AltTopUpForm::AddBtcAddressField.new + ).form ) end @@ -55,6 +58,7 @@ class AltTopUpFormTest < Minitest::Test assert_kind_of( Blather::Stanza::X, AltTopUpForm.new( + Customer.new("test"), AltTopUpForm::AddBtcAddressField::AddNewBtcAddressField.new ).form ) @@ -65,7 +69,7 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "true" } ] - assert AltTopUpForm.new.parse(iq_form)[:add_btc_address] + assert AltTopUpForm.new(Customer.new("t")).parse(iq_form)[:add_btc_address] end def test_parse_1 @@ -73,7 +77,7 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "1" } ] - assert AltTopUpForm.new.parse(iq_form)[:add_btc_address] + assert AltTopUpForm.new(Customer.new("t")).parse(iq_form)[:add_btc_address] end def test_parse_false @@ -81,11 +85,11 @@ class AltTopUpFormTest < Minitest::Test iq_form.fields = [ { var: "add_btc_address", value: "false" } ] - refute AltTopUpForm.new.parse(iq_form)[:add_btc_address] + refute AltTopUpForm.new(Customer.new("t")).parse(iq_form)[:add_btc_address] end def test_parse_not_presend iq_form = Blather::Stanza::X.new - refute AltTopUpForm.new.parse(iq_form)[:add_btc_address] + refute AltTopUpForm.new(Customer.new("t")).parse(iq_form)[:add_btc_address] end end -- 2.45.2