eggnog 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/eggnog.gemspec +31 -0
- data/eggnog.jpg +0 -0
- data/lib/eggnog/node_decorator.rb +77 -0
- data/lib/eggnog/version.rb +3 -0
- data/lib/eggnog.rb +6 -0
- data/lib/node.rb +5 -0
- data/spec/eggnog/node_decorator_spec.rb +33 -0
- data/spec/node_spec.rb +16 -0
- data/spec/spec_helper.rb +14 -0
- metadata +111 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.3@eggnog
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ryan Closner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
#Eggnog
|
2
|
+
|
3
|
+
[![travis](https://secure.travis-ci.org/rclosner/eggnog.png)](http://travis-ci.org/rclosner/eggnog)
|
4
|
+
|
5
|
+
<img src="https://github.com/rclosner/eggnog/raw/master/eggnog.jpg" width="200px" />
|
6
|
+
|
7
|
+
Eggnog is a Nokogiri XML Node Class mixin that implements Node#to_hash
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem install eggnog
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
|
18
|
+
# XML
|
19
|
+
|
20
|
+
|
21
|
+
```
|
22
|
+
<root>
|
23
|
+
<foo bar='baz'>Some text value</foo>
|
24
|
+
</root>
|
25
|
+
```
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
node = Nokogiri::XML(xml)
|
29
|
+
```
|
30
|
+
|
31
|
+
# Default Behavior
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
node.to_hash
|
35
|
+
```
|
36
|
+
|
37
|
+
returns:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
{
|
41
|
+
"root" => {
|
42
|
+
"foo" => "Some text value"
|
43
|
+
}
|
44
|
+
}
|
45
|
+
```
|
46
|
+
|
47
|
+
# With Options
|
48
|
+
|
49
|
+
Preserve XML attributes:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
node.to_hash(:preserve_attributes => true)
|
53
|
+
```
|
54
|
+
returns:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
{
|
58
|
+
"root" => {
|
59
|
+
"foo" => {
|
60
|
+
"__content__" => "Some text value",
|
61
|
+
"bar" => "baz"
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
```
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/eggnog.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/eggnog/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ryan Closner"]
|
6
|
+
gem.email = ["ryan@ryanclosner.com"]
|
7
|
+
gem.description = %q{Nokogiri Mixin - Converts Nodes to a Hash}
|
8
|
+
gem.summary = %q{Nokogiri Mixin - Converts Nodes to a Hash}
|
9
|
+
gem.homepage = "http://github.com/rclosner/eggnog"
|
10
|
+
|
11
|
+
runtime_dependencies = {
|
12
|
+
"nokogiri" => "~> 1.5.5"
|
13
|
+
}
|
14
|
+
|
15
|
+
runtime_dependencies.each {|lib, version| gem.add_runtime_dependency(lib, version) }
|
16
|
+
|
17
|
+
development_dependencies = {
|
18
|
+
"rake" => "~> 0.9.2",
|
19
|
+
"rspec" => "~> 2.7.0"
|
20
|
+
}
|
21
|
+
|
22
|
+
development_dependencies.each {|lib, version| gem.add_development_dependency(lib, version) }
|
23
|
+
|
24
|
+
|
25
|
+
gem.files = `git ls-files`.split($\)
|
26
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
27
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
28
|
+
gem.name = "eggnog"
|
29
|
+
gem.require_paths = ["lib"]
|
30
|
+
gem.version = Eggnog::VERSION
|
31
|
+
end
|
data/eggnog.jpg
ADDED
Binary file
|
@@ -0,0 +1,77 @@
|
|
1
|
+
class Eggnog::NodeDecorator < Struct.new(:node, :options)
|
2
|
+
|
3
|
+
CONTENT_ROOT = "__content__".freeze
|
4
|
+
|
5
|
+
def self.to_hash(node, options = {})
|
6
|
+
new(node, options).to_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_hash(parent_hash = {})
|
10
|
+
insert_into_parent_hash(parent_hash, collect_nodes)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def insert_into_parent_hash(hash, content)
|
16
|
+
case hash[name]
|
17
|
+
when NilClass then hash[name] = content
|
18
|
+
when Hash then hash[name] = [hash[name], content]
|
19
|
+
when Array then hash[name] << content
|
20
|
+
end
|
21
|
+
hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def collect_nodes
|
25
|
+
case node
|
26
|
+
when Nokogiri::XML::Document
|
27
|
+
self.class.to_hash(node.root, options)
|
28
|
+
else
|
29
|
+
if preserve_attributes?
|
30
|
+
output = collect_attributes
|
31
|
+
|
32
|
+
if output.empty?
|
33
|
+
output = collect_children
|
34
|
+
else
|
35
|
+
output[CONTENT_ROOT] = collect_children
|
36
|
+
end
|
37
|
+
|
38
|
+
output
|
39
|
+
else
|
40
|
+
collect_children
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def collect_attributes
|
46
|
+
({}).tap do |output|
|
47
|
+
attribute_nodes.each {|a| output[a.name] = a.value }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def collect_children
|
52
|
+
if element_children.empty?
|
53
|
+
self.content
|
54
|
+
else
|
55
|
+
output = {}
|
56
|
+
|
57
|
+
element_children.each do |child|
|
58
|
+
child.to_hash(output)
|
59
|
+
end
|
60
|
+
|
61
|
+
output
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def element_children
|
66
|
+
@element_children ||= node.element_children.map {|c| self.class.new(c, options)}
|
67
|
+
end
|
68
|
+
|
69
|
+
def preserve_attributes?
|
70
|
+
!!options.fetch(:preserve_attributes) { false }
|
71
|
+
end
|
72
|
+
|
73
|
+
def method_missing(m, *args, &block)
|
74
|
+
node.send(m, *args, &block)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/lib/eggnog.rb
ADDED
data/lib/node.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Eggnog::NodeDecorator do
|
4
|
+
subject { described_class.new(node, options) }
|
5
|
+
|
6
|
+
let(:node) { Nokogiri::XML(xml).root }
|
7
|
+
let(:xml) { "<foo boo='baz'>bar</foo>"}
|
8
|
+
let(:options) { {} }
|
9
|
+
|
10
|
+
describe "#to_hash" do
|
11
|
+
it "returns a Hash" do
|
12
|
+
subject.to_hash.should be_a(Hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "converts xml node into a hash" do
|
16
|
+
subject.to_hash.should eql({"foo" => "bar"})
|
17
|
+
end
|
18
|
+
|
19
|
+
context "preserve_attributes => true" do
|
20
|
+
let(:options) { { :preserve_attributes => true } }
|
21
|
+
|
22
|
+
it "preserves xml attributes" do
|
23
|
+
subject.to_hash.should eql({"foo" => {"__content__" => "bar", "boo" => "baz" } })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".to_hash" do
|
29
|
+
it "returns a hash" do
|
30
|
+
described_class.to_hash(node, options).should be_a(Hash)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/node_spec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Nokogiri::XML::Node do
|
4
|
+
subject { Nokogiri::XML(xml) }
|
5
|
+
|
6
|
+
let(:xml) {"<root><foo>bar</foo></root>"}
|
7
|
+
|
8
|
+
describe "#to_hash" do
|
9
|
+
let(:options) { {} }
|
10
|
+
|
11
|
+
it "delegates to NodeDecorator" do
|
12
|
+
Eggnog::NodeDecorator.should_receive(:to_hash).with(subject, options)
|
13
|
+
subject.to_hash(options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "rspec"
|
4
|
+
|
5
|
+
require "nokogiri"
|
6
|
+
require File.expand_path('../../lib/eggnog', __FILE__)
|
7
|
+
|
8
|
+
RSpec.configure do |c|
|
9
|
+
c.treat_symbols_as_metadata_keys_with_true_values = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def fixture_file(filename)
|
13
|
+
File.read(File.dirname(__FILE__) + "/fixtures/#{filename}")
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eggnog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Closner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.5.5
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.9.2
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.9.2
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.7.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.7.0
|
62
|
+
description: Nokogiri Mixin - Converts Nodes to a Hash
|
63
|
+
email:
|
64
|
+
- ryan@ryanclosner.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rvmrc
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- eggnog.gemspec
|
76
|
+
- eggnog.jpg
|
77
|
+
- lib/eggnog.rb
|
78
|
+
- lib/eggnog/node_decorator.rb
|
79
|
+
- lib/eggnog/version.rb
|
80
|
+
- lib/node.rb
|
81
|
+
- spec/eggnog/node_decorator_spec.rb
|
82
|
+
- spec/node_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
homepage: http://github.com/rclosner/eggnog
|
85
|
+
licenses: []
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.21
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Nokogiri Mixin - Converts Nodes to a Hash
|
108
|
+
test_files:
|
109
|
+
- spec/eggnog/node_decorator_spec.rb
|
110
|
+
- spec/node_spec.rb
|
111
|
+
- spec/spec_helper.rb
|