fio_parser 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/fio_parser.rb +135 -25
  2. metadata +5 -4
@@ -1,17 +1,27 @@
1
1
  # encoding: UTF-8
2
2
 
3
- # fio_parser is simple library for parsing monthly account statements from the Fio Bank CZ. Supports CSV and GPC formats.
3
+ # fio_parser is simple library for parsing monthly account statements from the Fio Bank CZ.
4
+ # Supports CSV and GPC formats.
5
+ # Support for the transaction email.
4
6
  #
5
7
  # Author: Jaromír "Cervajz" Čevenka
6
8
  # http://www.cervajz.com
7
9
  #
8
10
  # Copyright © 2012 cervajz@cervajz.com
9
11
  #
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:
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13
+ # documentation files (the “Software”), to deal in the Software without restriction, including without limitation
14
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
15
+ # and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11
16
  #
12
- # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
17
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
18
+ # the Software.
13
19
  #
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.
20
+ # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21
+ # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
24
+ # THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
25
 
16
26
  require 'ostruct'
17
27
  require 'date'
@@ -52,25 +62,34 @@ end
52
62
 
53
63
  class FioParser
54
64
  include GPCRowParser
55
- attr_accessor :transactions
65
+ attr_reader :transactions
56
66
 
57
67
  class << self
58
- def parse_csv(csv_file_path)
59
- return new csv_file_path, true
68
+ def parse_csv csv_file_path
69
+ new csv_file_path, :csv
60
70
  end
61
71
 
62
- def parse_gpc(gpc_file_path)
63
- return new gpc_file_path, false
72
+ def parse_gpc gpc_file_path
73
+ new gpc_file_path, :gpc
74
+ end
75
+
76
+ def parse_email email_body
77
+ new email_body, :email
64
78
  end
65
79
  end
66
80
 
67
- def initialize file_path, csv=true
81
+ def initialize file_path, type
68
82
  @transactions = []
69
83
 
70
- if csv
71
- parse_csv file_path
72
- else
73
- parse_gpc file_path
84
+ case type
85
+ when :csv
86
+ parse_csv file_path
87
+ when :gpc
88
+ parse_gpc file_path
89
+ when :email
90
+ parse_email file_path
91
+ else
92
+ Exception.raise 'Unknown input type'
74
93
  end
75
94
  end
76
95
 
@@ -129,17 +148,73 @@ class FioParser
129
148
  end
130
149
  end
131
150
 
151
+ def parse_email email
152
+ if File.exist?(email)
153
+ email_body = file_rows email
154
+ else
155
+ email_body = string_rows email
156
+ end
157
+
158
+ class << self
159
+ attr_reader :account
160
+ attr_reader :total
161
+ attr_reader :vs
162
+ attr_reader :message
163
+ attr_reader :balance
164
+ attr_reader :type
165
+ attr_reader :contra_acc
166
+ attr_reader :contra_acc_bc
167
+ attr_reader :ss
168
+ attr_reader :cs
169
+ end
170
+
171
+ income = true
172
+
173
+ email_body.each_with_index do |line, index|
174
+ out = get_string_from_colon line
175
+
176
+ case index
177
+ when 0 # Account nr.
178
+ income = mail_income? line
179
+ @account = out
180
+ when 1 # Total
181
+ @total = mail_total out, income
182
+ when 2 # Variable symbol
183
+ @vs = out
184
+ when 3 # Messsage
185
+ @message = out
186
+ when 4 # Balance
187
+ @balance = get_total out
188
+ when 5 # Contra account & payment type
189
+ @type = mail_transaction_type out
190
+ if @type == :transfer
191
+ @contra_acc = out.split('/')[0]
192
+ @contra_acc_bc = out.split('/')[1]
193
+ else
194
+ @contra_acc = ''
195
+ @contra_acc_bc = ''
196
+ end
197
+ when 6 # Specific symbol
198
+ @ss = out
199
+ when 7 # Constant symbol
200
+ @cs = out
201
+ else
202
+ #
203
+ end
204
+ end
205
+ end
206
+
132
207
  def parse_csv_row row
133
208
  columns = row.split ';'
134
209
 
135
210
  t = OpenStruct.new
136
211
  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]
212
+ t.total = get_total columns[2]
213
+ t.contra_acc = columns[3]
214
+ t.contra_acc_bc = columns[4]
140
215
  t.vs = columns[5]
141
216
  t.type = get_transaction_type columns[6]
142
- t.counter_acc_name = columns[7]
217
+ t.contra_acc_name = columns[7]
143
218
  t.message = columns[8]
144
219
  t.comment = columns[9]
145
220
 
@@ -151,8 +226,8 @@ class FioParser
151
226
 
152
227
  t.date = gpc_date row
153
228
  t.total = gpc_total row
154
- t.counter_acc = delete_zeros row[19..34]
155
- t.bank_code = gpc_bank_code row
229
+ t.contra_acc = delete_zeros row[19..34]
230
+ t.contra_acc_bc = gpc_bank_code row
156
231
  t.vs = delete_zeros row[61..70]
157
232
  t.cs = gpc_constant_symbol row
158
233
  t.ss = gpc_specific_symbol row
@@ -167,11 +242,11 @@ class FioParser
167
242
  end
168
243
 
169
244
  def get_transaction_type str
170
- if ['převodem', 'bezhotovostní', 'připsaný úrok'].any? { |w| str.downcase.include?(w) }
245
+ if ['převodem', 'prevodem', 'bezhotovostní', 'bezhotovostni', 'připsaný úrok'].any? { |w| str.downcase.include?(w) }
171
246
  return :transfer
172
247
  end
173
248
 
174
- if ['kartou'].any? { |w| str.include?(w) }
249
+ if %w(kartou).any? { |w| str.include?(w) }
175
250
  return :card
176
251
  end
177
252
 
@@ -180,9 +255,44 @@ class FioParser
180
255
 
181
256
  def file_rows file
182
257
  result = []
183
- e_c = Encoding::Converter.new 'cp1250', 'utf-8'
184
- File.open(file).each_line { |line| result.push e_c.convert line }
258
+ File.open(file).each_line { |line| result.push convert_line line }
185
259
  result
186
260
  end
187
- end
188
261
 
262
+ def string_rows str
263
+ result = []
264
+ str.each_line { |line| result.push convert_line line }
265
+ result
266
+ end
267
+
268
+ def convert_line str
269
+ if str.encoding.to_s.downcase == "utf-8"
270
+ str
271
+ else
272
+ e_c = Encoding::Converter.new 'cp1250', 'utf-8'
273
+ e_c.convert str
274
+ end
275
+ end
276
+
277
+ def get_string_from_colon str
278
+ str.sub(/^.*?:/i, '').strip
279
+ end
280
+
281
+ def get_total str
282
+ str.delete(' ').gsub(',', '.').to_f
283
+ end
284
+
285
+ def mail_total str, income
286
+ total = get_total str
287
+ total = 0 - total unless income
288
+ total
289
+ end
290
+
291
+ def mail_income? str
292
+ ['příjem', 'prijem'].any? { |w| str.downcase.include?(w) }
293
+ end
294
+
295
+ def mail_transaction_type str
296
+ str.downcase.include?('kartou') ? :card : :transfer
297
+ end
298
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fio_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,8 +11,8 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Gem for parsing monthly statements from the Fio Bank CZ. Supports CSV
15
- and GPC formats
14
+ description: Gem for parsing monthly statements from the Fio Bank CZ. Supports CSV,
15
+ GPC and email format.
16
16
  email: cervajz@cervajz.com
17
17
  executables: []
18
18
  extensions: []
@@ -42,5 +42,6 @@ rubyforge_project:
42
42
  rubygems_version: 1.8.24
43
43
  signing_key:
44
44
  specification_version: 3
45
- summary: Gem for parsing monthly statements from the Fio Bank CZ
45
+ summary: Gem for parsing monthly statements from the Fio Bank CZ. Parses transaction
46
+ emails too.
46
47
  test_files: []