fio_parser 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fio_parser.rb +188 -0
- metadata +46 -0
data/lib/fio_parser.rb
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# fio_parser is simple library for parsing monthly account statements from the Fio Bank CZ. Supports CSV and GPC formats.
|
4
|
+
#
|
5
|
+
# Author: Jaromír "Cervajz" Čevenka
|
6
|
+
# http://www.cervajz.com
|
7
|
+
#
|
8
|
+
# Copyright © 2012 cervajz@cervajz.com
|
9
|
+
#
|
10
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
13
|
+
#
|
14
|
+
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
15
|
+
|
16
|
+
require 'ostruct'
|
17
|
+
require 'date'
|
18
|
+
|
19
|
+
module GPCRowParser
|
20
|
+
def delete_zeros str
|
21
|
+
str.gsub /\A0*/, ''
|
22
|
+
end
|
23
|
+
|
24
|
+
def gpc_bank_code row
|
25
|
+
result = row[71..80][2..5]
|
26
|
+
result.each_char { |ch| return result if (ch != '0') }
|
27
|
+
''
|
28
|
+
end
|
29
|
+
|
30
|
+
def gpc_constant_symbol row
|
31
|
+
result = delete_zeros row[71..80][6..10]
|
32
|
+
result.each_char { |ch| return result if (ch != '0') }
|
33
|
+
''
|
34
|
+
end
|
35
|
+
|
36
|
+
def gpc_specific_symbol row
|
37
|
+
result = row[81..90]
|
38
|
+
result.each_char { |ch| return result if (ch != '0') }
|
39
|
+
''
|
40
|
+
end
|
41
|
+
|
42
|
+
def gpc_total row
|
43
|
+
result = row[48..59].to_i / 100.0
|
44
|
+
result = 0 - result if row[60].to_i == 1
|
45
|
+
result
|
46
|
+
end
|
47
|
+
|
48
|
+
def gpc_date row
|
49
|
+
Date.strptime row[122..127], '%d%m%y'
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class FioParser
|
54
|
+
include GPCRowParser
|
55
|
+
attr_accessor :transactions
|
56
|
+
|
57
|
+
class << self
|
58
|
+
def parse_csv(csv_file_path)
|
59
|
+
return new csv_file_path, true
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse_gpc(gpc_file_path)
|
63
|
+
return new gpc_file_path, false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize file_path, csv=true
|
68
|
+
@transactions = []
|
69
|
+
|
70
|
+
if csv
|
71
|
+
parse_csv file_path
|
72
|
+
else
|
73
|
+
parse_gpc file_path
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def incomes
|
78
|
+
result = []
|
79
|
+
@transactions.each { |t| result.push(t) if t.total > 0.0 }
|
80
|
+
result
|
81
|
+
end
|
82
|
+
|
83
|
+
def total_income
|
84
|
+
result = 0
|
85
|
+
incomes.each { |i| result += i.total }
|
86
|
+
result.round(2)
|
87
|
+
end
|
88
|
+
|
89
|
+
def total_expenses
|
90
|
+
result = 0
|
91
|
+
expenses.each { |i| result += i.total }
|
92
|
+
result.abs.round(2)
|
93
|
+
end
|
94
|
+
|
95
|
+
def expenses
|
96
|
+
result = []
|
97
|
+
@transactions.each { |t| result.push(t) if t.total < 0.0 }
|
98
|
+
result
|
99
|
+
end
|
100
|
+
|
101
|
+
def first
|
102
|
+
@transactions.first
|
103
|
+
end
|
104
|
+
|
105
|
+
def last
|
106
|
+
@transactions.last
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
|
111
|
+
def parse_csv csv_file
|
112
|
+
content = false
|
113
|
+
|
114
|
+
file_rows(csv_file).each do |row|
|
115
|
+
if content && contain_transaction?(row)
|
116
|
+
@transactions.push parse_csv_row row
|
117
|
+
end
|
118
|
+
|
119
|
+
content = row.downcase.include? 'převod;' unless content
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def parse_gpc gpc_file
|
124
|
+
|
125
|
+
file_rows(gpc_file).each do |row|
|
126
|
+
if row[0..2].downcase.include?('075')
|
127
|
+
@transactions.push parse_gpc_row(row)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def parse_csv_row row
|
133
|
+
columns = row.split ';'
|
134
|
+
|
135
|
+
t = OpenStruct.new
|
136
|
+
t.date = Date.strptime columns[1], '%d.%m.%Y'
|
137
|
+
t.total = columns[2].to_s.delete(' ').gsub(',', '.').to_f
|
138
|
+
t.counter_acc = columns[3]
|
139
|
+
t.bank_code = columns[4]
|
140
|
+
t.vs = columns[5]
|
141
|
+
t.type = get_transaction_type columns[6]
|
142
|
+
t.counter_acc_name = columns[7]
|
143
|
+
t.message = columns[8]
|
144
|
+
t.comment = columns[9]
|
145
|
+
|
146
|
+
t
|
147
|
+
end
|
148
|
+
|
149
|
+
def parse_gpc_row row
|
150
|
+
t = OpenStruct.new
|
151
|
+
|
152
|
+
t.date = gpc_date row
|
153
|
+
t.total = gpc_total row
|
154
|
+
t.counter_acc = delete_zeros row[19..34]
|
155
|
+
t.bank_code = gpc_bank_code row
|
156
|
+
t.vs = delete_zeros row[61..70]
|
157
|
+
t.cs = gpc_constant_symbol row
|
158
|
+
t.ss = gpc_specific_symbol row
|
159
|
+
t.type = row[60].to_i
|
160
|
+
t.message = row[97..116]
|
161
|
+
|
162
|
+
t
|
163
|
+
end
|
164
|
+
|
165
|
+
def contain_transaction? row
|
166
|
+
!(row.strip.empty? || row.downcase.include?('suma;'))
|
167
|
+
end
|
168
|
+
|
169
|
+
def get_transaction_type str
|
170
|
+
if ['převodem', 'bezhotovostní', 'připsaný úrok'].any? { |w| str.downcase.include?(w) }
|
171
|
+
return :transfer
|
172
|
+
end
|
173
|
+
|
174
|
+
if ['kartou'].any? { |w| str.include?(w) }
|
175
|
+
return :card
|
176
|
+
end
|
177
|
+
|
178
|
+
:other
|
179
|
+
end
|
180
|
+
|
181
|
+
def file_rows file
|
182
|
+
result = []
|
183
|
+
e_c = Encoding::Converter.new 'cp1250', 'utf-8'
|
184
|
+
File.open(file).each_line { |line| result.push e_c.convert line }
|
185
|
+
result
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fio_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jaromír Červenka
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Gem for parsing monthly statements from the Fio Bank CZ. Supports CSV
|
15
|
+
and GPC formats
|
16
|
+
email: cervajz@cervajz.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/fio_parser.rb
|
22
|
+
homepage: https://github.com/Cervajz/fio_parser
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.24
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Gem for parsing monthly statements from the Fio Bank CZ
|
46
|
+
test_files: []
|