wannabeparser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ require 'rubygems'
2
+ require 'libxml'
3
+
4
+ module WannaBeParser
5
+
6
+ def self.included(base)
7
+ base.instance_variable_set("@elements", {})
8
+ base.instance_variable_set("@tag", {})
9
+ base.instance_variable_set("@process", {})
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def tag(tag)
15
+ @tag = tag
16
+ end
17
+
18
+ def element(o)
19
+ @elements.merge!(o)
20
+ end
21
+
22
+ def elements
23
+ @elements ||= {}
24
+ end
25
+
26
+ def process(func)
27
+ @process = func
28
+ end
29
+
30
+ def parse(filepath)
31
+ @counter = 0
32
+ @flow = LibXML::XML::Reader.document(document(filepath))
33
+
34
+ raise 'I/O error or no such file' if @flow.nil?
35
+
36
+ while @flow.read do
37
+ parse_tag(@flow.expand) if @flow.node_type == LibXML::XML::Reader::TYPE_ELEMENT && @flow.name == @tag
38
+ end
39
+
40
+ @counter
41
+ end
42
+
43
+ private
44
+
45
+ def parse_tag(node)
46
+ @basepath = node.path
47
+ @counter = @counter + 1
48
+ @process.call(parse_node(node))
49
+ end
50
+
51
+ def parse_node(node)
52
+ obj = {}
53
+ node.each_element { |n|
54
+ if (n.node_type == LibXML::XML::Reader::TYPE_ELEMENT)
55
+ key = @elements[n.name]||n.name
56
+ cLength, cFirst = n.children.length, n.first
57
+ value = cFirst ? cLength == 1 && cFirst.text? ? cFirst.content : parse_node(n) : nil
58
+
59
+
60
+ unless key.nil? or value.nil?
61
+ case obj[key]
62
+ when nil
63
+ obj[key] = value
64
+ when Array
65
+ obj[key].push(value)
66
+ else
67
+ obj[key] = [ obj[key], value ]
68
+ end
69
+ end
70
+ end
71
+ }
72
+ obj
73
+ end
74
+
75
+ def document(filepath)
76
+ LibXML::XML::Document.file(filepath)
77
+ end
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wannabeparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pasha Puzikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-21 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: libxml-ruby
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.3
24
+ version:
25
+ description: Library for parsing xml to ruby hashes by input, selecting only specified tag contents
26
+ email: heydiplo@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - .gitignore
35
+ - Manifest
36
+ - Rakefile
37
+ - VERSION
38
+ - examples/twitter.rb
39
+ - examples/twitter.xml
40
+ - lib/wannabeparser.rb
41
+ - README.markdown
42
+ has_rdoc: true
43
+ homepage: http://github.com/heydiplo/wannabeparser
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Library for input-based xml to hash parsing
70
+ test_files:
71
+ - examples/twitter.rb