-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from andela-osogunle/feature/heritage-bank
Added Heritage Bank Parser
- Loading branch information
Showing
8 changed files
with
263 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'pdf-reader' | ||
require 'open-uri' | ||
require_relative 'hb-pdf-parser/hb_transaction_helpers' | ||
|
||
module NgBankParser | ||
class HbPdf | ||
|
||
extend HbTransactionHelpers | ||
|
||
class << self | ||
def parse(url, password = nil) | ||
unless ACCEPTED_FORMATS.include? File.extname(url) | ||
return INVALID_FILE_FORMAT_STRING | ||
end | ||
|
||
file = open(url) | ||
|
||
@reader = PDF::Reader.new(file) | ||
|
||
if pdf_is_valid? file | ||
generate_parse | ||
|
||
return account_statement_payload | ||
else | ||
return invalid_payload | ||
end | ||
end | ||
|
||
private | ||
|
||
def generate_parse | ||
set_account_name | ||
set_account_number | ||
set_end_date | ||
set_start_date | ||
extract_transactions | ||
end | ||
|
||
def extract_transactions | ||
@transactions = [] | ||
|
||
@reader.pages.each do |page| | ||
page.text.remove_empty_lines.lines.each do |line| | ||
transaction_items_list = transaction_items(line) | ||
if transaction_items_list[TRANSACTION_DATE_INDEX].is_date? | ||
if transaction_is_a_debit?(transaction_items_list, line) | ||
create_transaction(line, 'debit') | ||
elsif transaction_is_a_credit?(transaction_items_list, line) | ||
create_transaction(line, 'credit') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def account_statement_payload | ||
return { | ||
status: VALID_ACCOUNT_STATUS, | ||
data: { | ||
bank_name: $Banks[1][:name], | ||
account_number: @account_number, | ||
account_name: @account_name, | ||
from_date: @start_date, | ||
to_date: @end_date, | ||
transactions: @transactions | ||
# reader: @reader | ||
} | ||
} | ||
end | ||
|
||
def invalid_payload | ||
{status: INVALID_ACCOUNT_STATUS, | ||
message: INVALID_ACCOUNT_STATEMENT} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module NgBankParser | ||
module HbConstants | ||
ACCEPTED_FORMATS = ['.pdf'] | ||
|
||
INVALID_ACCOUNT_STATUS = 0 | ||
VALID_ACCOUNT_STATUS = 1 | ||
|
||
SECOND_PAGE_INDEX = 1 | ||
DURATION_DATE_LINE_INDEX = 3 | ||
ACCOUNT_NAME_LINE_INDEX = 5 | ||
ACCOUNT_NUMBER_LINE_INDEX = 6 | ||
TRANSACTION_HEADER_INDEX = 11 | ||
TRANSACTION_DATE_INDEX = 0 | ||
TRANSACTION_DEBIT_INDEX = -3 | ||
TRANSACTION_CREDIT_INDEX = -2 | ||
TRANSACTION_BALANCE = 131 | ||
TRANSACTION_BALANCE_END = 155 | ||
TRANSACTION_REMARKS = 32 | ||
TRANSACTION_REMARKS_END = 92 | ||
TRANSACTION_REFERENCE = 20 | ||
TRANSACTION_REFERENCE_END = 30 | ||
TRANSACTION_DEBIT = 92 | ||
TRANSACTION_DEBIT_END = 105 | ||
TRANSACTION_CREDIT = 110 | ||
TRANSACTION_CREDIT_END = 130 | ||
|
||
|
||
ACCOUNT_NAME_FIRST_MAKER = 'name' | ||
ACCOUNT_NAME_SECOND_MAKER = 'debit turnover' | ||
ACCOUNT_NUMBER_MAKER = 'account number' | ||
END_DATE_MAKER = '-' | ||
|
||
INVALID_FILE_FORMAT_STRING = 'File Format Not Valid' | ||
INVALID_ACCOUNT_STATEMENT = 'File Is Not A Valid Heritage Bank Account Statement' | ||
|
||
CORRECT_RC_NUMBER = 'RC No 9868' | ||
|
||
COLUMN_ARRANGEMENT = 'valuedatereferencenarrationdebitcreditbalance' | ||
|
||
|
||
|
||
|
||
end | ||
end |
112 changes: 112 additions & 0 deletions
112
lib/ng-bank-parser/parsers/hb-pdf-parser/hb_transaction_helpers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
require_relative 'hb_constants' | ||
|
||
module NgBankParser | ||
module HbTransactionHelpers | ||
include HbConstants | ||
def pdf_is_valid?(file) | ||
has_transaction_page? && has_transaction_columns? && has_correct_rc_number? && !has_encryption?(file) | ||
end | ||
|
||
private | ||
|
||
def has_transaction_page? | ||
@reader.pages[SECOND_PAGE_INDEX].present? | ||
end | ||
|
||
def has_correct_rc_number? | ||
@reader.pages.first.text.remove_empty_lines.lines.first.strip == CORRECT_RC_NUMBER | ||
end | ||
|
||
def has_transaction_columns? | ||
@reader.pages[SECOND_PAGE_INDEX].text.remove_empty_lines.lines[TRANSACTION_HEADER_INDEX].remove_white_spaces == COLUMN_ARRANGEMENT | ||
end | ||
|
||
def has_encryption?(file) | ||
begin | ||
@reader = PDF::Reader.new(file) | ||
false | ||
rescue PDF::Reader::EncryptedPDFError | ||
true | ||
end | ||
end | ||
|
||
def set_account_name | ||
@account_name = @reader.pages[SECOND_PAGE_INDEX].text.remove_empty_lines.lines[ACCOUNT_NAME_LINE_INDEX].get_text_between_markers(ACCOUNT_NAME_FIRST_MAKER, ACCOUNT_NAME_SECOND_MAKER).remove_white_spaces | ||
end | ||
|
||
def set_account_number | ||
@account_number = @reader.pages[SECOND_PAGE_INDEX].text.remove_empty_lines.lines[ACCOUNT_NUMBER_LINE_INDEX].strip.get_text_after_marker(ACCOUNT_NUMBER_MAKER).remove_white_spaces | ||
end | ||
|
||
def set_end_date | ||
@end_date = Date.parse(@reader.pages[SECOND_PAGE_INDEX].text.remove_empty_lines.lines[DURATION_DATE_LINE_INDEX].get_text_after_marker(END_DATE_MAKER).remove_white_spaces) | ||
end | ||
|
||
def set_start_date | ||
@start_date = Date.parse(@reader.pages[SECOND_PAGE_INDEX].text.remove_empty_lines.lines[DURATION_DATE_LINE_INDEX].get_text_between_markers(' ', END_DATE_MAKER).remove_white_spaces) | ||
end | ||
|
||
def set_transaction_attr(line) | ||
transaction_object = {} | ||
transaction_object[:date] = transaction_date(line) | ||
|
||
transaction_object[:balance] = transaction_balance(line) | ||
transaction_object[:remarks] = transaction_remarks(line) | ||
transaction_object[:ref] = transaction_reference(line) | ||
transaction_object | ||
end | ||
|
||
def transaction_is_a_debit?(transaction_items_list, line) | ||
transaction_items_list[TRANSACTION_DEBIT_INDEX] && debit_amount(line) > 0.00 | ||
end | ||
|
||
def transaction_is_a_credit?(transaction_items_list, line) | ||
transaction_items_list[TRANSACTION_CREDIT_INDEX] && credit_amount(line) > 0.00 | ||
end | ||
|
||
def create_transaction(line, type) | ||
transaction = set_transaction_attr(line) | ||
set_transaction_type(transaction, type, line) | ||
@transactions << transaction | ||
end | ||
|
||
def set_transaction_type(transaction, type, line) | ||
if type == 'debit' | ||
transaction[:amount] = debit_amount(line) | ||
transaction[:type] = type | ||
elsif type == 'credit' | ||
transaction[:amount] = credit_amount(line) | ||
transaction[:type] = type | ||
end | ||
end | ||
|
||
def transaction_date(line) | ||
Date.parse(line[0..19].remove_white_spaces) | ||
end | ||
|
||
def transaction_balance(line) | ||
line[TRANSACTION_BALANCE..TRANSACTION_BALANCE_END].get_numbers | ||
end | ||
|
||
def transaction_remarks(line) | ||
line[TRANSACTION_REMARKS..TRANSACTION_REMARKS_END].remove_white_spaces | ||
end | ||
|
||
def transaction_reference(line) | ||
line[TRANSACTION_REFERENCE..TRANSACTION_REFERENCE_END].remove_white_spaces | ||
end | ||
|
||
def debit_amount(line) | ||
line[TRANSACTION_DEBIT..TRANSACTION_DEBIT_END].get_numbers | ||
end | ||
|
||
def credit_amount(line) | ||
line[TRANSACTION_CREDIT..TRANSACTION_CREDIT_END].get_numbers | ||
end | ||
|
||
|
||
def transaction_items(line) | ||
line.strip.split(" ").reject(&:empty?) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,8 @@ def convert_to_date | |
Date.strptime(self, '%d-%b-%Y') | ||
end | ||
|
||
def get_numbers | ||
self.remove_commas.to_f | ||
end | ||
|
||
end |