client_bank_exchange 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/client_bank_exchange.rb +82 -78
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92621e68a03565d0b587d1c7b954298ffbf58f16
4
- data.tar.gz: 0b7f97f49af52dbdae699e10b9c0dd61df105902
3
+ metadata.gz: 6bfdafd757b6310d7af6584f01678d8d6d532884
4
+ data.tar.gz: 60354c097156b3a6d33e4737f523fe34974df8ef
5
5
  SHA512:
6
- metadata.gz: 89d08d70a5fa54e68f77b04ac4e8bc40d9748bc304d97cb1d4248c867593b9ae3ea1cb28cc513a6afa9d285ca264a96cb79da679c29bf93a82fd2f6ef96efba1
7
- data.tar.gz: 9f369100b8cecbc1281d4845164288026f752217f75e555a5b71d6150bac2193559dee823559645bc369298973f13da5d7c0388c13ae054d0c9cc1d4fa5c5b0c
6
+ metadata.gz: 3895e088af950f8c205708977d413ca2d6b6f34728a50f813b3b56a92f392b35be5a55af7b716ed409a49e6edf014ae9fc2fa4f4668f1b685b5e3b3d27d3bf00
7
+ data.tar.gz: a730f4d40994af695ea4d480ee00cd29ff5260ef3f2084c188a81dc14664785b14f59fdf905eb9b50f74f9393ebca257344c5a1274d6368398d57b9ba02cc57e
@@ -1,87 +1,91 @@
1
1
  require 'date'
2
2
  require 'time'
3
+ require 'bigdecimal'
4
+ require 'bigdecimal/util'
3
5
 
4
6
  module ClientBankExchange
5
- def self.parse_file path
6
- self.parse File.read(path)
7
- end
8
-
9
- def self.parse content
10
- result = {
11
- errors: [],
12
- general: {},
13
- remainings: {},
14
- filters: {},
15
- documents: []
16
- }
17
-
18
- if content.start_with? '1CClientBankExchange'
19
- # parse general info (key=value)
20
- [
21
- :ВерсияФормата, :Кодировка,
22
- :Отправитель, :Получатель,
23
- :ДатаСоздания, :ВремяСоздания
24
- ].each do |key|
25
- /#{key}=(.*)/.match(content) do |match|
26
- result[:general][key] = match[1]
7
+ class << self
8
+ def parse_file path
9
+ parse File.read(path)
10
+ end
11
+
12
+ def parse content
13
+ result = {
14
+ errors: [],
15
+ general: {},
16
+ remainings: {},
17
+ filters: {},
18
+ documents: []
19
+ }
20
+
21
+ if content.start_with? '1CClientBankExchange'
22
+ # parse general info (key=value)
23
+ [
24
+ :ВерсияФормата, :Кодировка,
25
+ :Отправитель, :Получатель,
26
+ :ДатаСоздания, :ВремяСоздания
27
+ ].each do |key|
28
+ /#{key}=(.*)/.match(content) do |match|
29
+ result[:general][key] = match[1]
30
+ end
27
31
  end
32
+
33
+ hash_value_to_date result[:general], :ДатаСоздания
34
+ hash_value_to_time result[:general], :ВремяСоздания
35
+
36
+ # parse remainings
37
+ /СекцияРасчСчет([\s\S]*?)\sКонецРасчСчет/.match(content) do |match|
38
+ # remainings properties (key=value)
39
+ match[1].scan(/(.*)=(.*)/) { |k, v| result[:remainings][k.to_sym] = v }
40
+
41
+ # normalize
42
+ hash_value_to_date result[:remainings], :ДатаНачала
43
+ hash_value_to_date result[:remainings], :ДатаКонца
44
+ hash_value_to_d result[:remainings], :НачальныйОстаток
45
+ hash_value_to_d result[:remainings], :ВсегоПоступило
46
+ hash_value_to_d result[:remainings], :ВсегоСписано
47
+ hash_value_to_d result[:remainings], :КонечныйОстаток
48
+ end
49
+
50
+ # parse documents
51
+ regexp_document = /СекцияДокумент=(.*)\s([\s\S]*?)\sКонецДокумента/
52
+ result[:documents] = content.scan(regexp_document).map do |doc|
53
+ # document type
54
+ document = { СекцияДокумент: doc[0] }
55
+
56
+ # document properties (key=value)
57
+ doc[1].scan(/(.*)=(.*)/) { |k, v| document[k.to_sym] = v }
58
+
59
+ # normalize
60
+ hash_value_to_i document, :Номер
61
+ hash_value_to_date document, :Дата
62
+ hash_value_to_d document, :Сумма
63
+
64
+ document
65
+ end
66
+ else
67
+ result[:errors] << 'Wrong format: 1CClientBankExchange not found'
28
68
  end
29
-
30
- hash_value_to_date result[:general], :ДатаСоздания
31
- hash_value_to_time result[:general], :ВремяСоздания
32
-
33
- # parse remainings
34
- /СекцияРасчСчет([\s\S]*?)\sКонецРасчСчет/.match(content) do |match|
35
- # remainings properties (key=value)
36
- match[1].scan(/(.*)=(.*)/) { |k, v| result[:remainings][k.to_sym] = v }
37
-
38
- # normalize
39
- hash_value_to_date result[:remainings], :ДатаНачала
40
- hash_value_to_date result[:remainings], :ДатаКонца
41
- hash_value_to_f result[:remainings], :НачальныйОстаток
42
- hash_value_to_f result[:remainings], :ВсегоПоступило
43
- hash_value_to_f result[:remainings], :ВсегоСписано
44
- hash_value_to_f result[:remainings], :КонечныйОстаток
45
- end
46
-
47
- # parse documents
48
- regexp_document = /СекцияДокумент=(.*)\s([\s\S]*?)\sКонецДокумента/
49
- result[:documents] = content.scan(regexp_document).map do |doc|
50
- # document type
51
- document = { СекцияДокумент: doc[0] }
52
-
53
- # document properties (key=value)
54
- doc[1].scan(/(.*)=(.*)/) { |k, v| document[k.to_sym] = v }
55
-
56
- # normalize
57
- hash_value_to_i document, :Номер
58
- hash_value_to_date document, :Дата
59
- hash_value_to_f document, :Сумма
60
-
61
- document
62
- end
63
- else
64
- result[:errors] << 'Wrong format: 1CClientBankExchange not found'
69
+
70
+ result
71
+ end
72
+
73
+ private
74
+
75
+ def hash_value_to_date hash, key
76
+ hash[key] = Date.parse(hash[key]) if hash[key]
77
+ end
78
+
79
+ def hash_value_to_time hash, key
80
+ hash[key] = Time.parse(hash[key]) if hash[key]
81
+ end
82
+
83
+ def hash_value_to_i hash, key
84
+ hash[key] = hash[key].to_i if hash[key]
85
+ end
86
+
87
+ def hash_value_to_d hash, key
88
+ hash[key] = hash[key].to_d if hash[key]
65
89
  end
66
-
67
- result
68
- end
69
-
70
- private
71
-
72
- def self.hash_value_to_date hash, key
73
- hash[key] = Date.parse(hash[key]) if hash[key]
74
- end
75
-
76
- def self.hash_value_to_time hash, key
77
- hash[key] = Time.parse(hash[key]) if hash[key]
78
- end
79
-
80
- def self.hash_value_to_i hash, key
81
- hash[key] = hash[key].to_i if hash[key]
82
- end
83
-
84
- def self.hash_value_to_f hash, key
85
- hash[key] = hash[key].to_f if hash[key]
86
90
  end
87
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: client_bank_exchange
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Zharikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Format specification: http://v8.1c.ru/edi/edi_stnd/100/101.htm'
14
14
  email: andrey@zharikov.pro