bliss 0.0.5
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.
- data/.document +5 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +40 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/bliss.gemspec +75 -0
- data/hash.rb +82 -0
- data/http-machine.rb +27 -0
- data/lib/bliss.rb +9 -0
- data/lib/bliss/parser.rb +12 -0
- data/lib/bliss/parser_machine.rb +90 -0
- data/lib/bliss/sax_parser.rb +122 -0
- data/lib/hash_extension.rb +23 -0
- data/test.rb +5 -0
- data/test/helper.rb +18 -0
- data/test/test_bliss.rb +7 -0
- metadata +145 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "nokogiri"
|
7
|
+
gem "eventmachine", ">= 0.12"
|
8
|
+
gem "em-http-request"
|
9
|
+
|
10
|
+
# Add dependencies to develop your gem here.
|
11
|
+
# Include everything needed to run rake, tests, features, etc.
|
12
|
+
group :development do
|
13
|
+
gem "shoulda", ">= 0"
|
14
|
+
gem "bundler", "~> 1.0.0"
|
15
|
+
gem "jeweler", "~> 1.6.4"
|
16
|
+
gem "simplecov"
|
17
|
+
#gem "rcov", ">= 0"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
addressable (2.2.7)
|
5
|
+
cookiejar (0.3.0)
|
6
|
+
em-http-request (1.0.1)
|
7
|
+
addressable (>= 2.2.3)
|
8
|
+
cookiejar
|
9
|
+
em-socksify
|
10
|
+
eventmachine (>= 1.0.0.beta.4)
|
11
|
+
http_parser.rb (>= 0.5.3)
|
12
|
+
em-socksify (0.1.0)
|
13
|
+
eventmachine
|
14
|
+
eventmachine (1.0.0.beta.4)
|
15
|
+
git (1.2.5)
|
16
|
+
http_parser.rb (0.5.3)
|
17
|
+
jeweler (1.6.4)
|
18
|
+
bundler (~> 1.0)
|
19
|
+
git (>= 1.2.5)
|
20
|
+
rake
|
21
|
+
multi_json (1.1.0)
|
22
|
+
nokogiri (1.5.0)
|
23
|
+
rake (0.9.2.2)
|
24
|
+
shoulda (2.11.3)
|
25
|
+
simplecov (0.6.0)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
simplecov-html (~> 0.5.3)
|
28
|
+
simplecov-html (0.5.3)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
bundler (~> 1.0.0)
|
35
|
+
em-http-request
|
36
|
+
eventmachine (>= 0.12)
|
37
|
+
jeweler (~> 1.6.4)
|
38
|
+
nokogiri
|
39
|
+
shoulda
|
40
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Fernando Alonso
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= bliss
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bliss'
|
5
|
+
|
6
|
+
path = 'http://www.yourdomain.com/input.xml'
|
7
|
+
|
8
|
+
parser = Bliss::ParserMachine.new(path, 'output.xml')
|
9
|
+
count = 0
|
10
|
+
p.on_root { |root|
|
11
|
+
puts root
|
12
|
+
}
|
13
|
+
parser.on_tag_open('ad') { |depth|
|
14
|
+
puts depth.inspect
|
15
|
+
}
|
16
|
+
parser.on_tag_close('ad') { |hash|
|
17
|
+
count += 1
|
18
|
+
puts hash.inspect
|
19
|
+
if count == 4
|
20
|
+
parser.close
|
21
|
+
end
|
22
|
+
}
|
23
|
+
|
24
|
+
parser.parse
|
25
|
+
puts "Root: #{parser.root}"
|
26
|
+
|
27
|
+
== Contributing to bliss
|
28
|
+
|
29
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
30
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
31
|
+
* Fork the project
|
32
|
+
* Start a feature/bugfix branch
|
33
|
+
* Commit and push until you are happy with your contribution
|
34
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
35
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
36
|
+
|
37
|
+
== Copyright
|
38
|
+
|
39
|
+
Copyright (c) 2012 Fernando Alonso. See LICENSE.txt for
|
40
|
+
further details.
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "bliss"
|
18
|
+
gem.homepage = "http://github.com/krakatoa/bliss"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{streamed xml parsing tool}
|
21
|
+
gem.description = %Q{streamed xml parsing tool}
|
22
|
+
gem.email = "krakatoa1987@gmail.com"
|
23
|
+
gem.authors = ["Fernando Alonso"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
#require 'rcov/rcovtask'
|
36
|
+
#Rcov::RcovTask.new do |test|
|
37
|
+
# test.libs << 'test'
|
38
|
+
# test.pattern = 'test/**/test_*.rb'
|
39
|
+
# test.verbose = true
|
40
|
+
# test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
#end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "bliss #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.5
|
data/bliss.gemspec
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "bliss"
|
8
|
+
s.version = "0.0.5"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Fernando Alonso"]
|
12
|
+
s.date = "2012-03-01"
|
13
|
+
s.description = "streamed xml parsing tool"
|
14
|
+
s.email = "krakatoa1987@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"bliss.gemspec",
|
28
|
+
"hash.rb",
|
29
|
+
"http-machine.rb",
|
30
|
+
"lib/bliss.rb",
|
31
|
+
"lib/bliss/parser.rb",
|
32
|
+
"lib/bliss/parser_machine.rb",
|
33
|
+
"lib/bliss/sax_parser.rb",
|
34
|
+
"lib/hash_extension.rb",
|
35
|
+
"test.rb",
|
36
|
+
"test/helper.rb",
|
37
|
+
"test/test_bliss.rb"
|
38
|
+
]
|
39
|
+
s.homepage = "http://github.com/krakatoa/bliss"
|
40
|
+
s.licenses = ["MIT"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = "1.8.15"
|
43
|
+
s.summary = "streamed xml parsing tool"
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
|
50
|
+
s.add_runtime_dependency(%q<eventmachine>, [">= 0.12"])
|
51
|
+
s.add_runtime_dependency(%q<em-http-request>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
55
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
58
|
+
s.add_dependency(%q<eventmachine>, [">= 0.12"])
|
59
|
+
s.add_dependency(%q<em-http-request>, [">= 0"])
|
60
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
61
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
63
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
67
|
+
s.add_dependency(%q<eventmachine>, [">= 0.12"])
|
68
|
+
s.add_dependency(%q<em-http-request>, [">= 0"])
|
69
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
71
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
72
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/hash.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
#require 'rubygems'
|
2
|
+
require './lib/hash_extension'
|
3
|
+
|
4
|
+
def start_element(hash, depth)
|
5
|
+
current = hash.pair_at_chain(depth)
|
6
|
+
value_at = hash.value_at_chain(depth)
|
7
|
+
if current.is_a? Hash
|
8
|
+
if value_at.is_a? NilClass
|
9
|
+
puts 'nil'
|
10
|
+
current[depth.last] = {}
|
11
|
+
elsif value_at.is_a? Hash
|
12
|
+
puts 'hash'
|
13
|
+
current[depth.last] = [current[depth.last], {}]
|
14
|
+
current = hash.pair_at_chain(depth)
|
15
|
+
end
|
16
|
+
elsif current.is_a? Array
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
hash = {'ads' => {'pictures' => {}}}
|
21
|
+
depth = ['ads', 'pictures', 'picture']
|
22
|
+
puts "Hash: #{hash.inspect}"
|
23
|
+
puts "Depth: #{depth.inspect}"
|
24
|
+
start_element(hash, depth)
|
25
|
+
puts "#{hash.inspect}"
|
26
|
+
#puts hash.pair_at_chain(depth).inspect
|
27
|
+
#puts hash.value_at_chain(depth).inspect
|
28
|
+
puts "---\n"
|
29
|
+
#=> pair_at_chain == {}
|
30
|
+
#=> value_at_chain == nil
|
31
|
+
|
32
|
+
hash = {'ads' => {'pictures' => {'picture' => {}}}}
|
33
|
+
depth = ['ads', 'pictures', 'picture']
|
34
|
+
puts "Hash: #{hash.inspect}"
|
35
|
+
puts "Depth: #{depth.inspect}"
|
36
|
+
puts hash.pair_at_chain(depth).inspect
|
37
|
+
puts hash.value_at_chain(depth).inspect
|
38
|
+
puts "---\n"
|
39
|
+
#=> pair_at_chain == {'picture' => {}}
|
40
|
+
#=> value_at_chain == {}
|
41
|
+
|
42
|
+
hash = {'ads' => {'pictures' => {'picture' => [{'picture_url' => {}}, {}]}}}
|
43
|
+
depth = ['ads', 'pictures', 'picture']
|
44
|
+
puts "Hash: #{hash.inspect}"
|
45
|
+
puts "Depth: #{depth.inspect}"
|
46
|
+
puts hash.pair_at_chain(depth).inspect
|
47
|
+
puts hash.value_at_chain(depth).inspect
|
48
|
+
puts "---\n"
|
49
|
+
#=> pair_at_chain == {'picture' => {}}
|
50
|
+
#=> value_at_chain == {}
|
51
|
+
|
52
|
+
hash = {'ads' => {'pictures' => {'picture' => [{'picture_url' => {}}, {}]}}}
|
53
|
+
depth = ['ads', 'pictures', 'picture']
|
54
|
+
puts "Hash: #{hash.inspect}"
|
55
|
+
puts "Depth: #{depth.inspect}"
|
56
|
+
puts hash.pair_at_chain(depth).inspect
|
57
|
+
puts hash.value_at_chain(depth).inspect
|
58
|
+
puts "---\n"
|
59
|
+
#=> pair_at_chain == {'picture' => {}}
|
60
|
+
#=> value_at_chain == {}
|
61
|
+
|
62
|
+
hash = {'ads' => {'pictures' => {'picture' => [{'picture_url' => {}}, {}]}}}
|
63
|
+
depth = ['ads', 'pictures', 'picture', 'picture_url']
|
64
|
+
puts "Hash: #{hash.inspect}"
|
65
|
+
puts "Depth: #{depth.inspect}"
|
66
|
+
puts hash.pair_at_chain(depth).inspect
|
67
|
+
puts hash.value_at_chain(depth).inspect
|
68
|
+
puts "---\n"
|
69
|
+
#=> pair_at_chain == [{'picture_url' => {}}, {'picture_url' => {}}]
|
70
|
+
#=> value_at_chain == {}
|
71
|
+
|
72
|
+
|
73
|
+
hash = {'ads' => {'pictures' => {'picture' => [{'picture_url' => {}}, {'picture_url' => {}}]}}}
|
74
|
+
depth = ['ads', 'pictures', 'picture', 'picture_url']
|
75
|
+
puts "Hash: #{hash.inspect}"
|
76
|
+
puts "Depth: #{depth.inspect}"
|
77
|
+
puts hash.pair_at_chain(depth).inspect
|
78
|
+
puts hash.value_at_chain(depth).inspect
|
79
|
+
puts "---\n"
|
80
|
+
#=> pair_at_chain == [{'picture_url' => {}}, {'picture_url' => {}}]
|
81
|
+
#=> value_at_chain == {}
|
82
|
+
|
data/http-machine.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'em-http-request'
|
4
|
+
|
5
|
+
@bytes = 0
|
6
|
+
@io_read, @io_write = IO.pipe
|
7
|
+
|
8
|
+
EM.run do
|
9
|
+
#url = 'http://www.universobit.com.ar/AvisosUniversobit/trovit/AvisosUniversobit_1.xml'
|
10
|
+
#url = 'http://www.aestrenar.com.ar/backend/rssAestrenar.xml'
|
11
|
+
url = 'http://procarnet.es/feed/sumavisos/sumavisos.xml'
|
12
|
+
|
13
|
+
http = EM::HttpRequest.new(url).get
|
14
|
+
http.stream { |chunk|
|
15
|
+
if @bytes > 10000
|
16
|
+
@io_write << "\n"
|
17
|
+
EM.stop
|
18
|
+
else
|
19
|
+
@io_write << chunk
|
20
|
+
@bytes += chunk.length
|
21
|
+
end
|
22
|
+
}
|
23
|
+
http.callback { EM.stop }
|
24
|
+
end
|
25
|
+
|
26
|
+
puts @io_read.gets
|
27
|
+
puts @bytes
|
data/lib/bliss.rb
ADDED
data/lib/bliss/parser.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
module Bliss
|
2
|
+
class ParserMachine
|
3
|
+
def initialize(path, filepath=nil)
|
4
|
+
@path = path
|
5
|
+
|
6
|
+
@sax_parser = Bliss::SaxParser.new
|
7
|
+
|
8
|
+
@parser = Nokogiri::XML::SAX::PushParser.new(@sax_parser)
|
9
|
+
|
10
|
+
if filepath
|
11
|
+
@file = File.new(filepath, 'w')
|
12
|
+
end
|
13
|
+
|
14
|
+
@root = nil
|
15
|
+
@nodes = nil
|
16
|
+
|
17
|
+
on_root {}
|
18
|
+
end
|
19
|
+
|
20
|
+
def on_root(&block)
|
21
|
+
return false if not block.is_a? Proc
|
22
|
+
@sax_parser.on_root { |root|
|
23
|
+
@root = root
|
24
|
+
block.call(root)
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def on_tag_open(element, &block)
|
29
|
+
return false if block.arity != 1
|
30
|
+
@sax_parser.on_tag_open(element, block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def on_tag_close(element, &block)
|
34
|
+
@sax_parser.on_tag_close(element, block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def root
|
38
|
+
@root
|
39
|
+
end
|
40
|
+
|
41
|
+
def close
|
42
|
+
@sax_parser.close
|
43
|
+
end
|
44
|
+
|
45
|
+
def parse
|
46
|
+
@bytes = 0
|
47
|
+
|
48
|
+
EM.run do
|
49
|
+
http = EM::HttpRequest.new(@path).get
|
50
|
+
http.stream { |chunk|
|
51
|
+
chunk.force_encoding('UTF-8')
|
52
|
+
|
53
|
+
@parser << chunk
|
54
|
+
|
55
|
+
@bytes += chunk.length
|
56
|
+
|
57
|
+
if not @sax_parser.is_closed?
|
58
|
+
if @file
|
59
|
+
@file << chunk
|
60
|
+
end
|
61
|
+
else
|
62
|
+
if @file
|
63
|
+
last_index = chunk.index('</ad>') + 4
|
64
|
+
begin
|
65
|
+
@file << chunk[0..last_index]
|
66
|
+
@file << "</#{self.root}>"
|
67
|
+
ensure
|
68
|
+
@file.close
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
EM.stop
|
73
|
+
end
|
74
|
+
}
|
75
|
+
http.callback {
|
76
|
+
if @file
|
77
|
+
@file.close
|
78
|
+
end
|
79
|
+
EM.stop
|
80
|
+
}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#require 'stringio'
|
87
|
+
#str = StringIO.new
|
88
|
+
#z = Zlib::GzipWriter.new(str)
|
89
|
+
#z.write(txt)
|
90
|
+
#z.close
|
@@ -0,0 +1,122 @@
|
|
1
|
+
module Bliss
|
2
|
+
class SaxParser < Nokogiri::XML::SAX::Document
|
3
|
+
def initialize
|
4
|
+
@depth = []
|
5
|
+
# @settings = {} # downcased
|
6
|
+
|
7
|
+
@root = nil
|
8
|
+
@nodes = {}
|
9
|
+
@current_node = {}
|
10
|
+
|
11
|
+
@on_root = nil
|
12
|
+
@on_tag_open = {}
|
13
|
+
@on_tag_close = {}
|
14
|
+
|
15
|
+
@closed = false
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def on_root(&block)
|
20
|
+
@on_root = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_tag_open(element, block)
|
24
|
+
@on_tag_open.merge!({element => block})
|
25
|
+
end
|
26
|
+
|
27
|
+
def on_tag_close(element, block)
|
28
|
+
@on_tag_close.merge!({element => block})
|
29
|
+
end
|
30
|
+
|
31
|
+
def close
|
32
|
+
@closed = true
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_closed?
|
36
|
+
@closed
|
37
|
+
end
|
38
|
+
|
39
|
+
def start_element(element, attributes)
|
40
|
+
# element_transformation
|
41
|
+
|
42
|
+
if @root == nil
|
43
|
+
@root = element
|
44
|
+
if @on_root.is_a? Proc
|
45
|
+
@on_root.call(@root)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
@depth.push(element) if @depth.last != element
|
50
|
+
|
51
|
+
if @on_tag_open.has_key? element
|
52
|
+
@on_tag_open[element].call(@depth)
|
53
|
+
end
|
54
|
+
|
55
|
+
current = @nodes.pair_at_chain(@depth)
|
56
|
+
|
57
|
+
value_at = @nodes.value_at_chain(@depth)
|
58
|
+
|
59
|
+
if current.is_a? Hash
|
60
|
+
if value_at.is_a? NilClass
|
61
|
+
current[element] = {}
|
62
|
+
elsif value_at.is_a? Hash
|
63
|
+
if current[element].is_a? Array
|
64
|
+
current[element].concat [{}]
|
65
|
+
else
|
66
|
+
current[element] = [current[element], {}]
|
67
|
+
#current = @nodes.pair_at_chain(@depth)
|
68
|
+
end
|
69
|
+
elsif value_at.is_a? Array
|
70
|
+
#puts @depth.inspect
|
71
|
+
#puts current[element].inspect
|
72
|
+
#puts current[element].inspect
|
73
|
+
end
|
74
|
+
elsif current.is_a? Array
|
75
|
+
end
|
76
|
+
|
77
|
+
@current_content = ''
|
78
|
+
end
|
79
|
+
|
80
|
+
def characters(string)
|
81
|
+
concat_content(string)
|
82
|
+
end
|
83
|
+
|
84
|
+
def cdata_block(string)
|
85
|
+
concat_content(string)
|
86
|
+
end
|
87
|
+
|
88
|
+
def end_element(element, attributes=[])
|
89
|
+
# element_transformation
|
90
|
+
|
91
|
+
current = @nodes.pair_at_chain(@depth)
|
92
|
+
value_at = @nodes.value_at_chain(@depth)
|
93
|
+
|
94
|
+
if value_at.is_a? Hash
|
95
|
+
current[element] = @current_content if @current_content.size > 0
|
96
|
+
elsif value_at.is_a? NilClass
|
97
|
+
if current.is_a? Array
|
98
|
+
current = current.last
|
99
|
+
current[element] = @current_content if @current_content.size > 0
|
100
|
+
end
|
101
|
+
end
|
102
|
+
@current_content = ''
|
103
|
+
|
104
|
+
if @on_tag_close.has_key? element
|
105
|
+
@on_tag_close[element].call(value_at)
|
106
|
+
end
|
107
|
+
|
108
|
+
@depth.pop if @depth.last == element
|
109
|
+
end
|
110
|
+
|
111
|
+
def concat_content(string)
|
112
|
+
string.strip!
|
113
|
+
if string
|
114
|
+
@current_content << string
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def end_document
|
119
|
+
puts @nodes.inspect
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Hash
|
2
|
+
def value_at_chain(chain)
|
3
|
+
current = self
|
4
|
+
chain.each do |key|
|
5
|
+
if current.is_a? Hash and current.has_key? key
|
6
|
+
current = current[key]
|
7
|
+
if current.is_a? Array
|
8
|
+
current = current.last
|
9
|
+
end
|
10
|
+
else
|
11
|
+
current = nil
|
12
|
+
break
|
13
|
+
end
|
14
|
+
end
|
15
|
+
return current
|
16
|
+
end
|
17
|
+
|
18
|
+
def pair_at_chain(chain)
|
19
|
+
chain = chain.dup
|
20
|
+
chain.pop
|
21
|
+
return self.value_at_chain(chain)
|
22
|
+
end
|
23
|
+
end
|
data/test.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'bliss'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
data/test/test_bliss.rb
ADDED
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bliss
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fernando Alonso
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: &10621740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *10621740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: eventmachine
|
27
|
+
requirement: &10620860 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.12'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *10620860
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: em-http-request
|
38
|
+
requirement: &10619800 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *10619800
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: shoulda
|
49
|
+
requirement: &10617960 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *10617960
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: bundler
|
60
|
+
requirement: &10616320 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.0.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *10616320
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jeweler
|
71
|
+
requirement: &10615260 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.6.4
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *10615260
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: simplecov
|
82
|
+
requirement: &10725500 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *10725500
|
91
|
+
description: streamed xml parsing tool
|
92
|
+
email: krakatoa1987@gmail.com
|
93
|
+
executables: []
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files:
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.rdoc
|
98
|
+
files:
|
99
|
+
- .document
|
100
|
+
- Gemfile
|
101
|
+
- Gemfile.lock
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.rdoc
|
104
|
+
- Rakefile
|
105
|
+
- VERSION
|
106
|
+
- bliss.gemspec
|
107
|
+
- hash.rb
|
108
|
+
- http-machine.rb
|
109
|
+
- lib/bliss.rb
|
110
|
+
- lib/bliss/parser.rb
|
111
|
+
- lib/bliss/parser_machine.rb
|
112
|
+
- lib/bliss/sax_parser.rb
|
113
|
+
- lib/hash_extension.rb
|
114
|
+
- test.rb
|
115
|
+
- test/helper.rb
|
116
|
+
- test/test_bliss.rb
|
117
|
+
homepage: http://github.com/krakatoa/bliss
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
none: false
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
hash: -3470161500999733584
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.8.15
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: streamed xml parsing tool
|
145
|
+
test_files: []
|