Skip to content

Commit

Permalink
Merge pull request #11 from andela-osogunle/feature/heritage-bank
Browse files Browse the repository at this point in the history
Added Heritage Bank Parser
  • Loading branch information
fathermerry committed Sep 21, 2015
2 parents 59819c4 + 624d983 commit 112375c
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/ng-bank-parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "ng-bank-parser/pdf-unlocker"
require "ng-bank-parser/parsers/gtb-excel-parser"
require "ng-bank-parser/parsers/uba-pdf-parser"
require "ng-bank-parser/parsers/hb-pdf-parser"
require "ng-bank-parser/parsers/firstbank-pdf-parser"

module NgBankParser
Expand Down
41 changes: 25 additions & 16 deletions lib/ng-bank-parser/banks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,38 @@ class Banks
key: "gtb",
name: "Guaranty Trust Bank",
parsers: [{
format: "excel",
valid: "lib/ng-bank-parser/fixtures/gtb-excel-valid.xlsx",
invalid: "lib/ng-bank-parser/fixtures/gtb-excel-invalid.pdf",
extensions: ["xls","xlsx"]
format: "excel",
valid: "lib/ng-bank-parser/fixtures/gtb-excel-valid.xlsx",
invalid: "lib/ng-bank-parser/fixtures/gtb-excel-invalid.pdf",
extensions: ["xls","xlsx"]
}]
},{
key: "hb",
name: "Heritage Bank",
parsers: [{
format: "pdf",
valid: "lib/ng-bank-parser/fixtures/hb-pdf-valid.pdf",
invalid: "lib/ng-bank-parser/fixtures/hb-pdf-invalid.pdf",
extensions: ["pdf"]
}]
},{
key: "uba",
name: "United Bank for Africa",
parsers: [{
format: "pdf",
valid: "lib/ng-bank-parser/fixtures/uba-pdf-valid.pdf",
invalid: "lib/ng-bank-parser/fixtures/uba-pdf-invalid.pdf",
extensions: ["pdf"]
format: "pdf",
valid: "lib/ng-bank-parser/fixtures/uba-pdf-valid.pdf",
invalid: "lib/ng-bank-parser/fixtures/uba-pdf-invalid.pdf",
extensions: ["pdf"]
}]
},{
key: "firstbank",
name: "First Bank",
parsers: [{
format: "pdf",
valid: "lib/ng-bank-parser/fixtures/firstbank-pdf-valid.pdf",
fixture_password: 19856,
invalid: "lib/ng-bank-parser/fixtures/firstbank-pdf-invalid.pdf",
extensions: ["pdf"]
key: "firstbank",
name: "First Bank",
parsers: [{
format: "pdf",
valid: "lib/ng-bank-parser/fixtures/firstbank-pdf-valid.pdf",
fixture_password: 19856,
invalid: "lib/ng-bank-parser/fixtures/firstbank-pdf-invalid.pdf",
extensions: ["pdf"]
}]
}]
end
Expand Down
Binary file added lib/ng-bank-parser/fixtures/hb-pdf-invalid.pdf
Binary file not shown.
Binary file added lib/ng-bank-parser/fixtures/hb-pdf-valid.pdf
Binary file not shown.
77 changes: 77 additions & 0 deletions lib/ng-bank-parser/parsers/hb-pdf-parser.rb
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
44 changes: 44 additions & 0 deletions lib/ng-bank-parser/parsers/hb-pdf-parser/hb_constants.rb
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 lib/ng-bank-parser/parsers/hb-pdf-parser/hb_transaction_helpers.rb
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
4 changes: 4 additions & 0 deletions lib/ng-bank-parser/parsers/uba-pdf-parser/classes/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 112375c

Please sign in to comment.