Reading and Writing to the Database

When we modified the Account earlier, we made it write out each transaction message as amount,account_number. The TransactionProcessor needs the account_number to find an account in the database and update its balance:

databases/01/lib/transaction_processor.rb
 
require_relative ​'transaction_queue'
 
require_relative ​'account'
 
 
transaction_queue = TransactionQueue.new
 
puts ​"transaction processor ready"
 
loop ​do
 
transaction_queue.read ​do​ |message|
 
sleep 1
*
transaction_amount, number = message.split(/,/)
*
account = Account.find_by_number!(number.strip)
*
new_balance = account.balance + transaction_amount.to_i
*
account.balance = new_balance
*
account.save
 
end
 
end

However, looking at the message ...

Get The Cucumber Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.