Skip to content

Commit

Permalink
fixed issue where global transactions array was appending old transac…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
lolu committed Dec 16, 2015
1 parent 1777c70 commit 5bf5010
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
16 changes: 8 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ng-bank-parser (0.1.4)
ng-bank-parser (0.1.5)
activesupport
nokogiri
pdf-reader
Expand All @@ -20,19 +20,19 @@ GEM
tzinfo (~> 1.1)
afm (0.2.2)
diff-lcs (1.2.5)
domain_name (0.5.24)
domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0)
hashery (2.1.1)
http-cookie (1.0.2)
domain_name (~> 0.5)
i18n (0.7.0)
json (1.8.3)
mime-types (2.6.1)
mini_portile (0.6.2)
minitest (5.8.0)
netrc (0.10.3)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
mime-types (2.99)
mini_portile2 (2.0.0)
minitest (5.8.3)
netrc (0.11.0)
nokogiri (1.6.7)
mini_portile2 (~> 2.0.0.rc2)
pdf-reader (1.3.3)
Ascii85 (~> 1.0.0)
afm (~> 0.2.0)
Expand Down
16 changes: 9 additions & 7 deletions lib/ng-bank-parser/parsers/firstbank-pdf-parser.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
require 'pdf-reader'
require_relative 'firstbank-pdf-parser/helpers'


module NgBankParser
class FirstbankPdf
extend FirstbankPdfHelpers

@@transactions = []

def self.parse(path, password = nil)
accepted_formats = [".pdf"];
unless accepted_formats.include? File.extname(path)
Expand All @@ -29,15 +28,16 @@ def self.parse(path, password = nil)
return error_message 'Unable to read account details'
end

if contains_transactions_table?
extract_transactions(clean(get_raw_transactions))
raw_transactions = contains_transactions_table?
if raw_transactions
bdd = extract_transactions(clean(raw_transactions))
data = {}
data[:bank_name] = 'First Bank'
data[:account_number] = get_account_number
data[:account_name] = get_account_name
data[:from_date] = get_from_date
data[:to_date] = get_to_date
data[:transactions] = @@transactions
data[:transactions] = bdd
send_response data
else
return error_message 'Could not find any transactions'
Expand All @@ -47,6 +47,7 @@ def self.parse(path, password = nil)
private

def self.extract_transactions(jagged_array = [[]])
arr = []
jagged_array.each do |array|
if is_transaction_row? array
transaction = {}
Expand All @@ -62,11 +63,12 @@ def self.extract_transactions(jagged_array = [[]])
transaction[:type] = 'debit'
update_last_balance transaction[:balance]
end
@@transactions << transaction
arr << transaction
else
@@transactions.last[:remarks] += array[0] if @@transactions
arr.last[:remarks] += array[0]
end
end
arr
end


Expand Down
36 changes: 15 additions & 21 deletions lib/ng-bank-parser/parsers/firstbank-pdf-parser/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
require 'pdf-reader'
require 'date'
require 'open-uri'
require 'securerandom'
require_relative 'statement_utils'
require_relative '../../pdf-unlocker.rb'

module NgBankParser
module FirstbankPdfHelpers
include StatementUtils

@@pdf_reader = nil
@@raw_transactions = [[]]
@@account_name = nil
@@account_number = nil
@@last_balance = nil
Expand All @@ -29,7 +27,6 @@ def has_encryption? file

def get_unlocked_pdf? path, password
response = PDFUnlocker.unlock(path, password)

return false unless response
if response.include? 'Unlock Failed'
return false
Expand All @@ -40,26 +37,15 @@ def get_unlocked_pdf? path, password
@@pdf_reader = PDF::Reader.new(pseudo_file)
return true
rescue
return false
return false
end
end
end


def get_raw_transactions
@@raw_transactions
end


def get_transaction_data
pages = get_pages @@pdf_reader
pages.each do |page|
page_text = get_page_text page
index = get_transaction_table_index page_text
unless index == -1
add_to_transactions page_text[index..-1]
end
end
get_all_transactions(pages)
end


Expand Down Expand Up @@ -104,7 +90,6 @@ def get_to_date

def contains_transactions_table?
get_transaction_data
@@raw_transactions
end


Expand Down Expand Up @@ -173,10 +158,19 @@ def send_response data

private

def add_to_transactions lines
lines.each do |line|
@@raw_transactions << line.strip.split(/\s\s+/)
def get_all_transactions pages
raw_transactions = [[]]
pages.each do |page|
page_text = get_page_text page
index = get_transaction_table_index page_text
unless index == -1
lines = page_text[index..-1]
lines.each do |line|
raw_transactions << line.strip.split(/\s\s+/)
end
end
end
return raw_transactions
end

end
Expand Down

0 comments on commit 5bf5010

Please sign in to comment.