class BillNye::BillNye

Public Class Methods

new() click to toggle source
# File lib/bill-nye.rb, line 13
def initialize()
    @REGEX_PURCHASE = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>Card Purchase[a-zA-Z ]+)(?<usage_date>[0-9]{2}\/[0-9]{2}) (?<source>.+)(?<amount>[-,0-9]+.[0-9]{2}) +(?<balance>[-,0-9]+\.[0-9]{2})/
    @REX_TRANSFER      = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>[a-zA-Z .0-9#:]+) +(?<amount>[-,0-9,]+.[0-9]{2}) +(?<balance>[-,0-9,]+.[0-9]{2})/
            @REX_PUR_RECUR        = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>Recurring Card Purchase) (?<usage_date>[0-9]{2}\/[0-9]{2}) (?<source>.+) *(?<amount>[-,0-9]+.[0-9]{2}) +(?<balance>[-,0-9]+\.[0-9]{2})/
            @REX_PUR_RETURN = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>Purchase Return) +(?<usage_date>[0-9]{2}\/[0-9]{2}) (?<source>.+)  (?<amount>[-,0-9]*.[0-9]{2}) +(?<balance>[-,0-9]+\.[0-9]{2})/
            @REX_ATM_WITH         = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>Non-Chase ATM Withdraw) +(?<usage_date>[0-9]{2}\/[0-9]{2}) (?<source>.+)  (?<amount>[-,0-9]+.[0-9]{2}) +(?<balance>[-,0-9]+.[0-9]{2})/
            @REX_ATM_FEE  = / *(?<process_date>[0-9]{2}\/[0-9]{2}) +(?<type>.*Fee.*) +(?<amount>[-,0-9]*.[0-9]{2}) +(?<balance>[-,0-9,]*.[0-9]{2})/
            @REX_CREDIT   = / *(?<process_date>[0-9]{2}\/[0-9]{2}) + (?<source>.+ )(?<amount>[-,0-9]+\.[0-9]{2})/
            @REX_SAVINGS  = / +SAVINGS SUMMARY/

            @log = BNLogger.log
end

Public Instance Methods

credit_default(match) click to toggle source
# File lib/bill-nye.rb, line 77
def credit_default(match)
        processDate = match[:process_date].strip
        type          = "Credit"
        source                = match[:source].strip
        amount                = match[:amount].strip
        return Credit.new(processDate, type, source, amount)
end
debit_atm_with(match) click to toggle source
# File lib/bill-nye.rb, line 141
def debit_atm_with(match)
        processDate = match[:process_date].strip
        source                = match[:type].strip
        amount                = match[:amount].strip
        type          = match[:type].strip
        usageDate     = processDate
        balance               = match[:balance].strip
        return Debit.new(processDate, type, usageDate, source, amount, balance)
end
debit_default(match) click to toggle source
# File lib/bill-nye.rb, line 121
def debit_default(match)
        processDate = match[:process_date].strip
        source                = match[:source].strip
        amount                = match[:amount].strip
        type          = match[:type].strip
        usageDate     = match[:usage_date].strip
        balance               = match[:balance].strip
        return Debit.new(processDate, type, usageDate, source, amount, balance)
end
debit_transfer_or_fee(match) click to toggle source
# File lib/bill-nye.rb, line 131
def debit_transfer_or_fee(match)
        processDate = match[:process_date].strip
        type          = match[:type].strip
        usageDate     = processDate
        source                = ""
        amount                = match[:amount].strip
        balance               = match[:balance].strip
        return Debit.new(processDate, type, usageDate, source, amount, balance)
end
parse_credit(line) click to toggle source
# File lib/bill-nye.rb, line 67
def parse_credit(line)
        _credit = nil
        if !@REX_CREDIT.match(line).nil?
                _match       = @REX_CREDIT.match(line)
                _credit = credit_default(_match)
                @log.info{ "Match CREDIT: #{_credit}" }
        end
        return _credit
end
parse_debit(line) click to toggle source
# File lib/bill-nye.rb, line 85
def parse_debit(line)
        _debit = nil
        begin
               if !@REGEX_PURCHASE.match(line).nil?
                       _match    = @REGEX_PURCHASE.match(line)
                       _debit = debit_default(_match)
                      @log.info{ "Match PURCHASE: #{_debit}" }
               elsif !@REX_PUR_RECUR.match(line).nil?
                       _match    = @REX_PUR_RECUR.match(line)
                       _debit = debit_default(_match)
                      @log.info{ "Match PURCHASE RECURRING: #{_debit}" }
               elsif !@REX_PUR_RETURN.match(line).nil?
                       _match    = @REX_PUR_RETURN.match(line)
                       _debit = debit_default(_match)
                      @log.info{ "Match PURCHASE RETURN: #{_debit}" }
               elsif !@REX_ATM_WITH.match(line).nil?
                       _match    = @REX_ATM_WITH.match(line)
                       _debit = debit_atm_with(_match)
                      @log.info{ "Match ATM WITH: #{_debit}" }
               elsif !@REX_ATM_FEE.match(line).nil?
                       _match    = @REX_ATM_FEE.match(line)
                       _debit = debit_transfer_or_fee(_match)
                      @log.info{ "ATM FEE: #{_debit}" }
               elsif !@REX_TRANSFER.match(line).nil?
                       _match    = @REX_TRANSFER.match(line)
                       _debit = debit_transfer_or_fee(_match)
                      @log.info{ "Match TRANSFER: #{_debit}" }
              else
                      @log.error{ "NO-MATCH on: #{_debit}" }
               end
       rescue => e
               @log.error{"Error #{e} on: #{_debit}"}
       end
       return _debit
end
parse_pdf(pdf, type) click to toggle source
# File lib/bill-nye.rb, line 39
def parse_pdf(pdf, type)
        _xactions = []
        begin
                `pdftotext -layout #{pdf}`
                _match               = /(?<filename>.+).pdf/.match(pdf)
                txt          = "#{_match[:filename]}.txt"
        rescue => e
                p "Error #{e}: Failed to convert #{pdf} to text."
        end
        
        File.open("#{txt}", "r") do |file_handle|
                file_handle.each_line do |line|
                        case type
                        when 1
                                _xaction = parse_debit(line)
                        when 2
                                _xaction = parse_credit(line)
                        end

                        if !_xaction.nil?
                                _xactions << _xaction
                        end
                end
        end

        return _xactions
end
parse_pdfs(dir, type) click to toggle source
# File lib/bill-nye.rb, line 26
def parse_pdfs(dir, type)
        _xaction_list = []
        Dir.foreach(dir) do |pdf|
                begin
                        _xactions = parse_pdf("#{dir}#{pdf}", type)
                        _xaction_list << _xactions
                rescue
                        p "Failed to convert #{pdf} to text."
                end
        end
        return _xaction_list
end