okane 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/okane.rb +82 -0
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a5560f9b7e28cec7e21de9efef99d0b5f45e97c9314a8d9a8206185019c652d
4
- data.tar.gz: a2adc14b2b5b50440113f0ca80323cb369d04182e9a81c7ad393e12b77a5c471
3
+ metadata.gz: fd83005ad9e8bb7b76c1901a351484e96760946d579891b636f3d065afbf0bea
4
+ data.tar.gz: 57ade3800e666f6c2c847fa5dcf3cd8eca80b30e5d948364bdae8a796ec03c28
5
5
  SHA512:
6
- metadata.gz: 076a4912c679db808a96c3e08e3fc2eb54099aaa80655d28ba7a26d5a6982efb632db1951a295bb250982b27820440dff362935ff77ded3b0f07fe1822100294
7
- data.tar.gz: 0d57ed93bd057e57fdeff15f35baf1d71778da3c129c4c2ea9145bc9d180976a73396f57284b65e724926e7ec1d26d84838e07eee109089821ab39584accbe6b
6
+ metadata.gz: 552410ed9c7ae375fc0c2010ab2942d5400d84b11cd4479ae9e9d2e380577fbf7e20b9c76a392e831d99c64d230a74b2933348705ca27ebc0b63115c883b27b2
7
+ data.tar.gz: 33770d9157f55f9ae8339494167c7fb59420176ab65c0d5506182642d576b75e598d304b39484d310d6bf56c5ad124a1568087c01f982a427f3b6aa2f2124f82
data/lib/okane.rb ADDED
@@ -0,0 +1,82 @@
1
+ module Okane
2
+ ENUMERABLE_TAGS = ["STMTTRN"]
3
+
4
+ class OFX
5
+ def self.parse(content)
6
+ lines = content.split("\n")
7
+
8
+ result = {}
9
+
10
+ tag_stack = []
11
+
12
+ lines.each do |line|
13
+ header_pattern = /(.+):(.+)/
14
+ aggregate_tag_pattern = /<(.+)>/
15
+ aggregate_closing_tag_pattern = /<\/(.+)>/
16
+ attribute_tag_pattern = /<(.+)>(.+)/
17
+ attribute_closing_tag_pattern = /<(.+)>(.+)<\/.+>/
18
+
19
+ line = line.strip
20
+
21
+ if (attribute_closing_tag_pattern.match?(line))
22
+ match = attribute_closing_tag_pattern.match(line)
23
+ tag = match[1]
24
+ value = match[2]
25
+
26
+ target_object = result.dig(*tag_stack)
27
+
28
+ if (target_object.class == Hash)
29
+ target_object[tag] = value.to_s
30
+ else
31
+ target_object.last()[tag] = value.to_s
32
+ end
33
+ elsif (attribute_tag_pattern.match?(line))
34
+ match = attribute_tag_pattern.match(line)
35
+ tag = match[1]
36
+ value = match[2]
37
+
38
+ target_object = result.dig(*tag_stack)
39
+
40
+ if (target_object.class == Hash)
41
+ target_object[tag] = value.to_s
42
+ else
43
+ target_object.last()[tag] = value.to_s
44
+ end
45
+ elsif (header_pattern.match?(line))
46
+ match = header_pattern.match(line)
47
+ result[match[1]] = match[2]
48
+ elsif (aggregate_closing_tag_pattern.match?(line))
49
+ tag_stack.pop
50
+ elsif (aggregate_tag_pattern.match?(line))
51
+ match = aggregate_tag_pattern.match(line)
52
+ tag = match[1]
53
+
54
+ if (tag_stack.count > 0)
55
+ target_object = result.dig(*tag_stack)
56
+
57
+ if (Okane::ENUMERABLE_TAGS.find_index(tag) != nil)
58
+ if (target_object[tag] == nil)
59
+ target_object[tag] = []
60
+ end
61
+
62
+ target_object[tag].append({})
63
+ else
64
+ target_object[tag] = {}
65
+ end
66
+ else
67
+ result[tag] = {}
68
+ end
69
+
70
+ tag_stack.append(tag)
71
+ elsif (line.size == 0)
72
+ # Do nothing
73
+ else
74
+ puts line
75
+ puts "No pattern found"
76
+ end
77
+ end
78
+
79
+ return result
80
+ end
81
+ end
82
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okane
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Vargas
@@ -15,7 +15,8 @@ email:
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
- files: []
18
+ files:
19
+ - lib/okane.rb
19
20
  homepage:
20
21
  licenses: []
21
22
  metadata: {}