dynarex-parser 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynarex-parser.rb +31 -7
- metadata +1 -1
data/lib/dynarex-parser.rb
CHANGED
@@ -2,15 +2,39 @@
|
|
2
2
|
|
3
3
|
# file: dynarex-parser.rb
|
4
4
|
|
5
|
-
require '
|
5
|
+
require 'rexleparser'
|
6
|
+
|
7
|
+
class DynarexParser
|
6
8
|
|
7
|
-
class DynarexParser < RecordxParser
|
8
|
-
|
9
9
|
def initialize(s)
|
10
10
|
@a = parse(s)
|
11
11
|
end
|
12
|
-
|
13
|
-
def
|
14
|
-
|
15
|
-
|
12
|
+
|
13
|
+
def parse(s)
|
14
|
+
|
15
|
+
s.instance_eval{
|
16
|
+
def fetch_node(name)
|
17
|
+
self.slice(((self =~ /<#{name}>/) + name.length + 2) .. \
|
18
|
+
(self =~ /<\/#{name}>/) - 1) if self[/<#{name}>/]
|
19
|
+
end
|
20
|
+
}
|
21
|
+
|
22
|
+
root_name = s[/<(\w+)/,1]
|
23
|
+
|
24
|
+
summary = RexleParser.new("<summary>#{s.fetch_node(:summary)}</summary>").to_a
|
25
|
+
|
26
|
+
raw_records = s.fetch_node(:records)
|
27
|
+
records = nil
|
28
|
+
|
29
|
+
if raw_records then
|
30
|
+
node_name = raw_records[/<(\w+)/,1]
|
31
|
+
records = raw_records.strip.split(/(?=<#{node_name}[^>]*>)/).map {|x| RexleParser.new(x).to_a}
|
32
|
+
end
|
33
|
+
|
34
|
+
[root_name, "", {}, [*summary], ['records', "",{}, *records]]
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_a()
|
38
|
+
@a
|
39
|
+
end
|
16
40
|
end
|