Class: CapitalOneStatement
- Inherits:
-
Object
- Object
- CapitalOneStatement
- Defined in:
- lib/parse_capitalone_pdf_statement.rb,
lib/parse_capitalone_pdf_statement/version.rb
Overview
CapitalOneStatement object, with data parsed from a PDF monthly statement.
Defined Under Namespace
Classes: Transaction
Constant Summary collapse
- CARD_NUMBER_REGEX =
/(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/
- DATE_REGEX =
/(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/
- AMOUNT_REGEX =
/\(?\$[\d,]+\.\d\d\)?/
- AMOUNT_ONLY_REGEX =
/^ *#{AMOUNT_REGEX.source} *$/
- FEES_REGEX =
/Total Fees This Period +(#{AMOUNT_REGEX.source})/
- INTEREST_REGEX =
/Total Interest This Period +(#{AMOUNT_REGEX.source})/
- TRANSACTION_REGEX =
/^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/
- VERSION =
"1.1.0"
Instance Attribute Summary collapse
-
#end_date ⇒ Date
readonly
The final day of the monthly statement.
-
#fees ⇒ Array<CapitalOneStatement::Transaction>
readonly
Array of fee transactions.
-
#new_balance ⇒ Float
readonly
The New Balance listed in the statement.
-
#payments ⇒ Array<CapitalOneStatement::Transaction>
readonly
Array of payment transactions.
-
#previous_balance ⇒ Float
readonly
The “Previous Balance” field listed in the statement.
-
#start_date ⇒ Date
readonly
The first day of the monthly statement.
-
#total_fees ⇒ Float
readonly
The “Fees and Interest Charged” field listed in the statement.
-
#total_payments ⇒ Float
readonly
The “Payments and Credits” field listed in the statement.
-
#total_transactions ⇒ Float
readonly
The “Transactions” field listed in the statement.
-
#transactions ⇒ Array<CapitalOneStatement::Transaction>
readonly
Array of charge transactions.
Instance Method Summary collapse
-
#initialize(pdf_path) ⇒ CapitalOneStatement
constructor
A new instance of CapitalOneStatement.
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(pdf_path) ⇒ CapitalOneStatement
Returns a new instance of CapitalOneStatement.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 59 def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end |
Instance Attribute Details
#end_date ⇒ Date (readonly)
Returns the final day of the monthly statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#fees ⇒ Array<CapitalOneStatement::Transaction> (readonly)
Returns array of fee transactions.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#new_balance ⇒ Float (readonly)
Returns the New Balance listed in the statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#payments ⇒ Array<CapitalOneStatement::Transaction> (readonly)
Returns array of payment transactions.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#previous_balance ⇒ Float (readonly)
Returns the “Previous Balance” field listed in the statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#start_date ⇒ Date (readonly)
Returns the first day of the monthly statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#total_fees ⇒ Float (readonly)
Returns the “Fees and Interest Charged” field listed in the statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#total_payments ⇒ Float (readonly)
Returns the “Payments and Credits” field listed in the statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#total_transactions ⇒ Float (readonly)
Returns the “Transactions” field listed in the statement.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
#transactions ⇒ Array<CapitalOneStatement::Transaction> (readonly)
Returns array of charge transactions.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 38 class CapitalOneStatement CARD_NUMBER_REGEX = /(?:TRANSACTIONS|ADJUSTMENTS) FOR [^#]+ #(\d{4})/ DATE_REGEX = /(\w{3})\. (\d\d) - (\w{3})\. (\d\d), (\d{4})/ AMOUNT_REGEX = /\(?\$[\d,]+\.\d\d\)?/ AMOUNT_ONLY_REGEX = /^ *#{AMOUNT_REGEX.source} *$/ FEES_REGEX = /Total Fees This Period +(#{AMOUNT_REGEX.source})/ INTEREST_REGEX = /Total Interest This Period +(#{AMOUNT_REGEX.source})/ TRANSACTION_REGEX = /^ *(\d+) +(\d\d) ([A-Z][A-Z][A-Z]) (.+[^ ]) +(#{ AMOUNT_REGEX.source }) *$/ attr_reader :start_date, :end_date, :previous_balance, :total_payments, :total_fees, :total_transactions, :new_balance, :payments, :transactions def initialize(pdf_path) @card_number = nil @dec_from_prev_year = nil @year = nil @start_date = nil @end_date = nil @previous_balance = nil @new_balance = nil @total_payments = nil @total_transactions = nil @total_fees = nil @payments = [] @transactions = [] @fees = [] parse_from_pdf pdf_path %w{payments transactions fees}.each do |type| trxs = "@#{type}" total = "@total_#{type}" instance_variable_set(trxs, instance_variable_get(trxs).sort_by {|trx| trx[:id]}) check_total( type, instance_variable_get(total), instance_variable_get(trxs).inject(0) {|sum, trx| sum += trx[:amount]} ) end end def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end private def parse_from_pdf(pdf_path) PDF::Reader.new(pdf_path).pages.each_with_index do |page, page_num| if @year.nil? walker = Struct.new(:year, :offset, :start_date, :end_date) do def respond_to?(_) true end def method_missing(name, *args) return unless name =~ /show_text/ if args.any? {|str| str.to_s =~ DATE_REGEX} self.offset = ($1.upcase == 'DEC' && $3.upcase == 'JAN') ? 1 : 0 self.year = $5.to_i self.start_date = Date.parse('%s-%s-%s' % [year - offset, $1, $2]) self.end_date = Date.parse('%s-%s-%s' % [year, $3, $4]) end end end.new page.walk walker @dec_from_prev_year = walker.offset == 1 @year = walker.year @start_date = walker.start_date @end_date = walker.end_date end enum = page.text.split("\n").each loop do current_line = enum.next enum.next until (enum.peek rescue nil) != '' next_line = (enum.peek rescue nil) parse_pdf_line page_num, current_line, next_line end end end def parse_pdf_line(page_num, line, next_line) if line =~ CARD_NUMBER_REGEX @card_number = $1.to_i end if @previous_balance.nil? amount_strs = line.scan AMOUNT_REGEX if amount_strs.size == 5 @previous_balance, @total_payments, @total_fees, @total_transactions, @new_balance = amount_strs.map {|amount| parse_amount(amount)} @total_payments = -@total_payments end end if line =~ FEES_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "CAPITAL ONE MEMBER FEE", $1, parse_amount($1) ) end if line =~ INTEREST_REGEX && $1 != '$0.00' check_parse_state @fees << Transaction.new( @fees.size + 1, @card_number, @end_date, "INTEREST CHARGE:PURCHASES", $1, parse_amount($1) ) end transactions, payments = [(0..78), (80..-1)].map do |index| str = line .to_s[index].to_s next_str = next_line.to_s[index].to_s repair_transaction_line str, next_str end.map do |str| parse_transaction(str) end.compact.partition do |trx| trx[:amount] >= 0 end @transactions += transactions @payments += payments end def repair_transaction_line(line, next_line) if next_line =~ AMOUNT_ONLY_REGEX && !(line =~ AMOUNT_REGEX) line += " #{next_line.strip}" else line end end def parse_transaction(line) return nil unless line =~ TRANSACTION_REGEX && $4 != "CAPITAL ONE MEMBER FEE" check_parse_state year = ($3.upcase == 'DEC' && @dec_from_prev_year) ? @year - 1 : @year date = Date.parse('%s-%s-%s' % [year, $3, $2]) Transaction.new($1.to_i, @card_number, date, $4, $5, parse_amount($5)) end def parse_amount(amount) num = amount.gsub(/[^\d.]/, '').to_f amount.start_with?(?() ? -num : num end def check_parse_state raise "Failed to determine billing cycle dates" if @year.nil? raise "Failed to determine card number" if @card_number.nil? end def check_total(type, expected, actual) return if actual.round(2) == expected.round(2) raise "Calculated %s mismatch %.2f != %.2f" % [ type, actual, expected ] end # CapitalOneStatement::Transaction represents a single credit transaction class CapitalOneStatement::Transaction < Struct.new( :id, :card_number, :date, :description, :amount_str, :amount ) # @!attribute id # @return [Fixnum] transaction id # # @!attribute card_number # @return [Fixnum] the last four digits of the account number # # @!attribute date # @return [Date] the date of the transaction # # @!attribute description # @return [String] the description of the transaction # # @!attribute amount_str # @return [String] the dollar amount string of the transaction # # @!attribute amount # @return [Float] the dollar amount parsed into a Float, negative for payments # @return [String] JSON representation of Transaction def to_json(*args) to_h.to_json(*args) end end end |
Instance Method Details
#to_json(*args) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/parse_capitalone_pdf_statement.rb', line 90 def to_json(*args) { :start_date => @start_date, :end_date => @end_date, :previous_balance => @previous_balance, :total_payments => @total_payments, :total_fees => @total_fees, :total_transactions => @total_transactions, :new_balance => @new_balance, :payments => @payments, :transactions => @transactions, :fees => @fees }.to_json(*args) end |