tylerhunt-relief 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tyler Hunt
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,7 @@
1
+ = Relief
2
+
3
+ An XML to Hash Ruby parser DSL.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Tyler Hunt. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "relief"
8
+ gem.summary = %Q{An XML to Hash Ruby parser DSL.}
9
+ gem.email = "tyler@tylerhunt.com"
10
+ gem.homepage = "http://github.com/tylerhunt/relief"
11
+ gem.authors = ["Tyler Hunt"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+
32
+ task :default => :spec
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "relief #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
@@ -0,0 +1,61 @@
1
+ module Relief
2
+ class Element
3
+ attr_reader :name, :options, :children
4
+
5
+ def initialize(name, options, &block)
6
+ @name = name
7
+ @options = options
8
+ @children = {}
9
+
10
+ instance_eval(&block) if block_given?
11
+ end
12
+
13
+ def parse(document)
14
+ @children.inject({}) do |values, child|
15
+ name, element = child
16
+
17
+ values[name] = begin
18
+ target = (document / element.xpath)
19
+
20
+ parse_node = lambda { |target|
21
+ element.children.any? ? element.parse(target) : target.to_s
22
+ }
23
+
24
+ if element.options[:collection]
25
+ target.collect { |child| parse_node.call(child) }
26
+ else
27
+ parse_node.call(target)
28
+ end
29
+ end
30
+
31
+ values
32
+ end
33
+ end
34
+
35
+ def element(name, options={}, &block)
36
+ options[:xpath] ||= name if name =~ %r([/.])
37
+ name = options[:as].to_sym if options.has_key?(:as)
38
+ @children[name] ||= self.class.new(name, options, &block)
39
+ end
40
+
41
+ def elements(name, options={}, &block)
42
+ element(name, options.merge(:collection => true), &block)
43
+ end
44
+
45
+ def attribute(name, options={}, &block)
46
+ element(name, options.merge(:attribute => true), &block)
47
+ end
48
+
49
+ def xpath
50
+ if options.has_key?(:xpath)
51
+ options[:xpath]
52
+ elsif @children.empty?
53
+ attribute = @options[:attribute]
54
+ attribute = name if attribute == true
55
+ !attribute ? "#{name}/text()" : "@#{attribute}"
56
+ else
57
+ name.to_s
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,20 @@
1
+ gem 'nokogiri', '~> 1.2.3'
2
+ require 'nokogiri'
3
+
4
+ module Relief
5
+ class Parser
6
+ attr_reader :root
7
+
8
+ def initialize(name, options={}, &block)
9
+ @root = Element.new(name, options, &block)
10
+ end
11
+
12
+ def parse(document)
13
+ unless document.is_a?(Nokogiri::XML::NodeSet)
14
+ document = Nokogiri::XML(document)
15
+ end
16
+
17
+ @root.parse(document)
18
+ end
19
+ end
20
+ end
data/lib/relief.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Relief
2
+ autoload :Parser, 'lib/relief/parser'
3
+ autoload :Element, 'lib/relief/element'
4
+ end
@@ -0,0 +1,126 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Relief::Parser do
4
+ it "parses elements" do
5
+ parser = Relief::Parser.new(:photo) do
6
+ element :name
7
+ element :url
8
+ end
9
+
10
+ photo = parser.parse(<<-XML)
11
+ <?xml version="1.0" encoding="UTF-8"?>
12
+ <photo>
13
+ <name>Cucumbers</name>
14
+ <url>/photos/cucumbers.jpg</url>
15
+ </photo>
16
+ XML
17
+
18
+ photo.should == { :name => 'Cucumbers', :url => '/photos/cucumbers.jpg' }
19
+ end
20
+
21
+ it "parses collections of elements" do
22
+ parser = Relief::Parser.new(:photos) do
23
+ elements :photo do
24
+ element :name
25
+ element :url
26
+ end
27
+ end
28
+
29
+ photos = parser.parse(<<-XML)
30
+ <?xml version="1.0" encoding="UTF-8"?>
31
+ <photos>
32
+ <photo>
33
+ <name>Cucumbers</name>
34
+ <url>/photos/cucumbers.jpg</url>
35
+ </photo>
36
+ <photo>
37
+ <name>Lemons</name>
38
+ <url>/photos/lemons.jpg</url>
39
+ </photo>
40
+ </photos>
41
+ XML
42
+
43
+ photos.should == {
44
+ :photo => [
45
+ { :name => 'Cucumbers', :url => '/photos/cucumbers.jpg' },
46
+ { :name => 'Lemons', :url => '/photos/lemons.jpg' }
47
+ ]
48
+ }
49
+ end
50
+
51
+ it "parses attributes" do
52
+ parser = Relief::Parser.new(:photos) do
53
+ elements :photo do
54
+ attribute :name
55
+ attribute :url
56
+ end
57
+ end
58
+
59
+ photos = parser.parse(<<-XML)
60
+ <?xml version="1.0" encoding="UTF-8"?>
61
+ <photos>
62
+ <photo name="Cucumbers" url="/photos/cucumbers.jpg" />
63
+ <photo name="Lemons" url="/photos/lemons.jpg" />
64
+ </photos>
65
+ XML
66
+
67
+ photos.should == {
68
+ :photo => [
69
+ { :name => 'Cucumbers', :url => '/photos/cucumbers.jpg' },
70
+ { :name => 'Lemons', :url => '/photos/lemons.jpg' }
71
+ ]
72
+ }
73
+ end
74
+
75
+ it "parses elements by XPath" do
76
+ parser = Relief::Parser.new(:photos) do
77
+ elements '//photo', :as => :photo do
78
+ element 'name/text()', :as => :name
79
+ element 'url/text()', :as => :url
80
+ end
81
+ end
82
+
83
+ photos = parser.parse(<<-XML)
84
+ <?xml version="1.0" encoding="UTF-8"?>
85
+ <photos>
86
+ <photo>
87
+ <name>Cucumbers</name>
88
+ <url>/photos/cucumbers.jpg</url>
89
+ </photo>
90
+ <photo>
91
+ <name>Lemons</name>
92
+ <url>/photos/lemons.jpg</url>
93
+ </photo>
94
+ </photos>
95
+ XML
96
+
97
+ photos.should == {
98
+ :photo => [
99
+ { :name => 'Cucumbers', :url => '/photos/cucumbers.jpg' },
100
+ { :name => 'Lemons', :url => '/photos/lemons.jpg' }
101
+ ]
102
+ }
103
+ end
104
+
105
+ it "parses elements by nested XPath" do
106
+ parser = Relief::Parser.new(:photos) do
107
+ elements '//name/text()', :as => :name
108
+ end
109
+
110
+ photos = parser.parse(<<-XML)
111
+ <?xml version="1.0" encoding="UTF-8"?>
112
+ <photos>
113
+ <photo>
114
+ <name>Cucumbers</name>
115
+ <url>/photos/cucumbers.jpg</url>
116
+ </photo>
117
+ <photo>
118
+ <name>Lemons</name>
119
+ <url>/photos/lemons.jpg</url>
120
+ </photo>
121
+ </photos>
122
+ XML
123
+
124
+ photos.should == { :name => ['Cucumbers', 'Lemons'] }
125
+ end
126
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+
5
+ gem 'rspec', '~> 1.2.4'
6
+ require 'spec'
7
+
8
+ require 'relief'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tylerhunt-relief
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: tyler@tylerhunt.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/relief.rb
31
+ - lib/relief/element.rb
32
+ - lib/relief/parser.rb
33
+ - spec/parser_spec.rb
34
+ - spec/spec_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/tylerhunt/relief
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: An XML to Hash Ruby parser DSL.
61
+ test_files:
62
+ - spec/parser_spec.rb
63
+ - spec/spec_helper.rb