Class: CodaStandard::Parser
- Inherits:
-
Object
- Object
- CodaStandard::Parser
- Defined in:
- lib/coda_standard/parser.rb
Instance Attribute Summary collapse
-
#current_account ⇒ Object
readonly
Returns the value of attribute current_account.
-
#current_bic ⇒ Object
readonly
Returns the value of attribute current_bic.
-
#current_transaction ⇒ Object
readonly
Returns the value of attribute current_transaction.
-
#current_transaction_list ⇒ Object
readonly
Returns the value of attribute current_transaction_list.
-
#old_balance ⇒ Object
readonly
Returns the value of attribute old_balance.
-
#transactions ⇒ Object
readonly
Returns the value of attribute transactions.
Instance Method Summary collapse
- #create_transaction ⇒ Object
- #create_transaction_list ⇒ Object
- #extract_data_movement1(record) ⇒ Object
- #extract_data_movement2(record) ⇒ Object
- #extract_data_movement3(record) ⇒ Object
-
#initialize(filename) ⇒ Parser
constructor
A new instance of Parser.
- #parse(skip_validation: skip_validation = false) ⇒ Object
- #set_account(account) ⇒ Object
- #set_address(address) ⇒ Object
- #show(skip_validation: skip_validation = false) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(filename) ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 10 |
# File 'lib/coda_standard/parser.rb', line 5 def initialize(filename) @filename = filename @transactions = [] @current_transaction_list = TransactionList.new @current_transaction = Transaction.new end |
Instance Attribute Details
#current_account ⇒ Object (readonly)
Returns the value of attribute current_account.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def current_account @current_account end |
#current_bic ⇒ Object (readonly)
Returns the value of attribute current_bic.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def current_bic @current_bic end |
#current_transaction ⇒ Object (readonly)
Returns the value of attribute current_transaction.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def current_transaction @current_transaction end |
#current_transaction_list ⇒ Object (readonly)
Returns the value of attribute current_transaction_list.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def current_transaction_list @current_transaction_list end |
#old_balance ⇒ Object (readonly)
Returns the value of attribute old_balance.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def old_balance @old_balance end |
#transactions ⇒ Object (readonly)
Returns the value of attribute transactions.
3 4 5 |
# File 'lib/coda_standard/parser.rb', line 3 def transactions @transactions end |
Instance Method Details
#create_transaction ⇒ Object
57 58 59 |
# File 'lib/coda_standard/parser.rb', line 57 def create_transaction @current_transaction = @current_transaction_list.create_transaction end |
#create_transaction_list ⇒ Object
61 62 63 64 |
# File 'lib/coda_standard/parser.rb', line 61 def create_transaction_list @current_transaction_list = TransactionList.new @transactions << @current_transaction_list end |
#extract_data_movement1(record) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/coda_standard/parser.rb', line 66 def extract_data_movement1(record) @current_transaction.entry_date = Date.strptime(record.entry_date, '%d%m%y') @current_transaction.reference_number = record.reference_number @current_transaction.amount = record.amount @current_transaction.structured_communication = record.structured_communication end |
#extract_data_movement2(record) ⇒ Object
73 74 75 |
# File 'lib/coda_standard/parser.rb', line 73 def extract_data_movement2(record) @current_transaction.bic = record.bic end |
#extract_data_movement3(record) ⇒ Object
77 78 79 80 81 |
# File 'lib/coda_standard/parser.rb', line 77 def extract_data_movement3(record) @current_transaction.currency = record.currency @current_transaction.name = record.name @current_transaction.account = record.account end |
#parse(skip_validation: skip_validation = false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/coda_standard/parser.rb', line 20 def parse(skip_validation: skip_validation = false) return [] if !skip_validation && !valid? File.open(@filename).each do |line| record = Record.new(line) case when record.header? create_transaction_list @current_transaction_list.current_bic = record.current_bic when record.data_old_balance? set_account(record.current_account) @current_transaction_list.old_balance = record.old_balance when record.data_movement1? create_transaction extract_data_movement1(record) when record.data_movement2? extract_data_movement2(record) when record.data_movement3? extract_data_movement3(record) when record.data_information2? set_address(record.address) end end @transactions end |
#set_account(account) ⇒ Object
52 53 54 55 |
# File 'lib/coda_standard/parser.rb', line 52 def set_account(account) @current_transaction_list.current_account = account[:account_number] @current_transaction_list.current_account_type = account[:account_type] end |
#set_address(address) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/coda_standard/parser.rb', line 45 def set_address(address) @current_transaction.address = address[:address] @current_transaction.postcode = address[:postcode] @current_transaction.city = address[:city] @current_transaction.country = address[:country] end |
#show(skip_validation: skip_validation = false) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/coda_standard/parser.rb', line 83 def show(skip_validation: skip_validation = false) puts "The file is invalid" if !skip_validation && !valid? parse(skip_validation: skip_validation) @transactions.each_with_index do |transaction, index| puts "**--Transaction List #{ index + 1 }--**\n\n" puts "Account: #{transaction.current_account} Account type: #{transaction.current_account_type} BIC: #{transaction.current_bic}" puts "Old balance: #{transaction.old_balance} \n\n" transaction.each_with_index do |transaction, index| puts "-- Transaction n.#{index + 1} - number #{transaction.structured_communication} - in date #{transaction.entry_date}-- \n\n" puts " RN: #{transaction.reference_number} Account: #{transaction.account} BIC: #{transaction.bic}" puts " Amount: #{transaction.amount_money}" puts " Name: #{transaction.name}" puts " Address: #{transaction.address} #{transaction.postcode} #{transaction.city} #{transaction.country} \n\n" end end end |