demolisher 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Geoff Garside
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,48 @@
1
+ = demolisher
2
+
3
+ Works in a similar fashion to Builder but is instead used for extracting information from XML files rather than building them.
4
+
5
+ == Example
6
+
7
+ Given the simple XML example file below
8
+
9
+ <addressbook>
10
+ <person>
11
+ <firstname>Enoch</firstname>
12
+ <lastname>Root</lastname>
13
+ <contact>
14
+ <phone>01234 567 8900</phone>
15
+ <email>enoch@example.com</email>
16
+ </contact>
17
+ <active>YES</active>
18
+ </person>
19
+ <person>
20
+ <firstname>Randy</firstname>
21
+ <lastname>Waterhouse</lastname>
22
+ <contact>
23
+ <phone>01234 567 8901</phone>
24
+ <email>randy@example.com</email>
25
+ </contact>
26
+ <active>NO</active>
27
+ </person>
28
+ </addressbook>
29
+
30
+ we can parse it with
31
+
32
+ xml = Demolisher.demolish('addressbook.xml')
33
+ xml.addressbook do
34
+ xml.person do
35
+ puts "#{xml.firstname} #{xml.lastname}: #{xml.contact.email}"
36
+ puts "this person is active" if xml.active?
37
+ end
38
+ end
39
+
40
+ and we should get the result of
41
+
42
+ Enoch Root: enoch@example.com
43
+ this person is active
44
+ Randy Waterhouse: randy@example.com
45
+
46
+ == Copyright
47
+
48
+ Copyright (c) 2009 Geoff Garside. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "demolisher"
8
+ gem.summary = %Q{Gem for extracting information from XML files, think Builder but backwards}
9
+ gem.email = "geoff@geoffgarside.co.uk"
10
+ gem.homepage = "http://github.com/geoffgarside/demolisher"
11
+ gem.authors = ["Geoff Garside"]
12
+ gem.rubyforge_project = 'demolisher'
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ gem.add_dependency('libxml-ruby', '>= 1.1.3')
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "demolisher #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,51 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{demolisher}
5
+ s.version = "0.3.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Geoff Garside"]
9
+ s.date = %q{2009-06-09}
10
+ s.email = %q{geoff@geoffgarside.co.uk}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "demolisher.gemspec",
23
+ "lib/demolisher.rb",
24
+ "test/demolisher_test.rb",
25
+ "test/test.xml",
26
+ "test/test_helper.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/geoffgarside/demolisher}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubyforge_project = %q{demolisher}
32
+ s.rubygems_version = %q{1.3.4}
33
+ s.summary = %q{Gem for extracting information from XML files, think Builder but backwards}
34
+ s.test_files = [
35
+ "test/demolisher_test.rb",
36
+ "test/test_helper.rb"
37
+ ]
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<libxml-ruby>, [">= 1.1.3"])
45
+ else
46
+ s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<libxml-ruby>, [">= 1.1.3"])
50
+ end
51
+ end
data/lib/demolisher.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'xml'
2
+
3
+ module Demolisher
4
+ # Demolish an XML file or XML::Parser object.
5
+ def self.demolish(file_or_xml_parser)
6
+ file_or_xml_parser = XML::Parser.file(file_or_xml_parser) if file_or_xml_parser.kind_of?(String)
7
+ node = Node.new(file_or_xml_parser.parse, true)
8
+
9
+ yield node if block_given?
10
+ node
11
+ end
12
+ class Node
13
+ # Creates a new Node object. If the node is not the root node then
14
+ # the second argument needs to be false.
15
+ def initialize(xml, is_root = true)
16
+ @nodes = [xml]
17
+ @nodes.unshift(nil) unless is_root
18
+ end
19
+ # Access an attribute of the current node
20
+ # Example:
21
+ # <addressbook>
22
+ # <person rel="friend">
23
+ # <firstname>Steve</firstname>
24
+ # </person>
25
+ # </addressbook>
26
+ #
27
+ # xml.addressbook do
28
+ # xml.person do
29
+ # puts "#{xml.firstname} is a #{xml['rel']}" #=> "Steve is a friend"
30
+ # end
31
+ # end
32
+ #
33
+ def [](attr_name)
34
+ @nodes.last.attributes[attr_name]
35
+ end
36
+ def method_missing(meth, *args, &block) # :nodoc:
37
+ xpath = @nodes.last.find(element_from_symbol(meth))
38
+ return nil if xpath.empty?
39
+
40
+ if block_given?
41
+ xpath.each do |node|
42
+ @nodes.push(node)
43
+ yield
44
+ @nodes.pop
45
+ end
46
+ else
47
+ node = xpath.first
48
+
49
+ if node.find('text()').length == 1
50
+ content = node.find('text()').first.content
51
+ case meth.to_s
52
+ when /\?$/
53
+ !! Regexp.new(/(t(rue)?|y(es)?|1)/i).match(content)
54
+ else
55
+ content
56
+ end
57
+ else
58
+ self.class.new(node, false)
59
+ end
60
+ end
61
+ end
62
+ def to_s # :nodoc:
63
+ @nodes.last.content.strip
64
+ end
65
+ def element_from_symbol(sym) # :nodoc:
66
+ "#{is_root_node? ? '/' : nil}#{sym.to_s.gsub(/[^a-z0-9_]/i, '')}"
67
+ end
68
+ # Indicates if the current node is the root of the XML document
69
+ def is_root_node?
70
+ @nodes.size == 1
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ class DemolisherTest < Test::Unit::TestCase
4
+ context "Demolished XML file" do
5
+ setup do
6
+ @people = Array.new
7
+ Demolisher.demolish(File.dirname(__FILE__) +'/test.xml') do |xml|
8
+ xml.addressbook do
9
+ xml.person do
10
+ @people << {:firstname => xml.firstname, :lastname => xml.lastname,
11
+ :active => xml.active?, :email => xml.contact.email}
12
+ end
13
+ end
14
+ end
15
+ end
16
+ context "first extracted person" do
17
+ setup do
18
+ @person = @people[0]
19
+ end
20
+ should "have extracted firstname" do
21
+ assert_equal 'Enoch', @person[:firstname]
22
+ end
23
+ should "have extracted lastname" do
24
+ assert_equal 'Root', @person[:lastname]
25
+ end
26
+ should "have extracted true active status" do
27
+ assert @person[:active]
28
+ end
29
+ should "have extracted email" do
30
+ assert_equal 'enoch@example.com', @person[:email]
31
+ end
32
+ end
33
+ context "second extracted person" do
34
+ setup do
35
+ @person = @people[1]
36
+ end
37
+ should "have extracted firstname" do
38
+ assert_equal 'Randy', @person[:firstname]
39
+ end
40
+ should "have extracted lastname" do
41
+ assert_equal 'Waterhouse', @person[:lastname]
42
+ end
43
+ should "have extracted false active status" do
44
+ assert !@person[:active]
45
+ end
46
+ should "have extracted email" do
47
+ assert_equal 'randy@example.com', @person[:email]
48
+ end
49
+ end
50
+ end
51
+ end
data/test/test.xml ADDED
@@ -0,0 +1,20 @@
1
+ <addressbook>
2
+ <person>
3
+ <firstname>Enoch</firstname>
4
+ <lastname>Root</lastname>
5
+ <contact>
6
+ <phone>01234 567 8900</phone>
7
+ <email>enoch@example.com</email>
8
+ </contact>
9
+ <active>YES</active>
10
+ </person>
11
+ <person>
12
+ <firstname>Randy</firstname>
13
+ <lastname>Waterhouse</lastname>
14
+ <contact>
15
+ <phone>01234 567 8901</phone>
16
+ <email>randy@example.com</email>
17
+ </contact>
18
+ <active>NO</active>
19
+ </person>
20
+ </addressbook>
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'demolisher'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: demolisher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Geoff Garside
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-09 00:00:00 +01: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:
26
+ email: geoff@geoffgarside.co.uk
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - demolisher.gemspec
42
+ - lib/demolisher.rb
43
+ - test/demolisher_test.rb
44
+ - test/test.xml
45
+ - test/test_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/geoffgarside/demolisher
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project: demolisher
70
+ rubygems_version: 1.3.4
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Gem for extracting information from XML files, think Builder but backwards
74
+ test_files:
75
+ - test/demolisher_test.rb
76
+ - test/test_helper.rb