relief 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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,50 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = "relief"
11
+ gem.summary = %Q{An XML to hash Ruby parser DSL.}
12
+ gem.email = "tyler@tylerhunt.com"
13
+ gem.homepage = "http://github.com/tylerhunt/relief"
14
+ gem.authors = ["Tyler Hunt"]
15
+
16
+ gem.add_dependency 'nokogiri', '~> 1.2.3'
17
+
18
+ gem.add_development_dependency('jeweler', '~> 0.11.0')
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
22
+ end
23
+
24
+ task :default => :spec
25
+
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ Rake::RDocTask.new do |rdoc|
38
+ if File.exist?('VERSION.yml')
39
+ config = YAML.load(File.read('VERSION.yml'))
40
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "relief #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('LICENSE*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 3
@@ -0,0 +1,60 @@
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, element|
15
+ key = element.options[:as] || element.name
16
+
17
+ values[key] = 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
+ @children << self.class.new(name, options, &block)
38
+ end
39
+
40
+ def elements(name, options={}, &block)
41
+ element(name, options.merge(:collection => true), &block)
42
+ end
43
+
44
+ def attribute(name, options={}, &block)
45
+ element(name, options.merge(:attribute => true), &block)
46
+ end
47
+
48
+ def xpath
49
+ if options.has_key?(:xpath)
50
+ options[:xpath]
51
+ elsif @children.any?
52
+ name.to_s
53
+ else
54
+ attribute = @options[:attribute]
55
+ attribute = name if attribute == true
56
+ !attribute ? "#{name}/text()" : "@#{attribute}"
57
+ end
58
+ end
59
+ end
60
+ 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, 'relief/parser'
3
+ autoload :Element, '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,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relief
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.11.0
34
+ version:
35
+ description:
36
+ email: tyler@tylerhunt.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION.yml
49
+ - lib/relief.rb
50
+ - lib/relief/element.rb
51
+ - lib/relief/parser.rb
52
+ - spec/parser_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/tylerhunt/relief
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.1
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: An XML to hash Ruby parser DSL.
80
+ test_files:
81
+ - spec/parser_spec.rb
82
+ - spec/spec_helper.rb