relief 0.0.3 → 0.0.4
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/VERSION.yml +1 -1
- data/lib/relief/element.rb +14 -2
- data/lib/relief/parser.rb +2 -2
- data/spec/parser_spec.rb +51 -0
- metadata +2 -2
data/VERSION.yml
CHANGED
data/lib/relief/element.rb
CHANGED
@@ -2,7 +2,7 @@ module Relief
|
|
2
2
|
class Element
|
3
3
|
attr_reader :name, :options, :children
|
4
4
|
|
5
|
-
def initialize(name, options, &block)
|
5
|
+
def initialize(name=nil, options={}, &block)
|
6
6
|
@name = name
|
7
7
|
@options = options
|
8
8
|
@children = []
|
@@ -13,12 +13,24 @@ module Relief
|
|
13
13
|
def parse(document)
|
14
14
|
@children.inject({}) do |values, element|
|
15
15
|
key = element.options[:as] || element.name
|
16
|
+
type = element.options[:type]
|
16
17
|
|
17
18
|
values[key] = begin
|
18
19
|
target = (document / element.xpath)
|
19
20
|
|
20
21
|
parse_node = lambda { |target|
|
21
|
-
element.children.any?
|
22
|
+
if element.children.any?
|
23
|
+
element.parse(target)
|
24
|
+
else
|
25
|
+
value = target.to_s
|
26
|
+
|
27
|
+
if type == Integer then value.to_i
|
28
|
+
elsif type == Float then value.to_f
|
29
|
+
elsif type == Date then Date.parse(value)
|
30
|
+
elsif type.is_a?(Parser) then type.parse(document)
|
31
|
+
else value
|
32
|
+
end
|
33
|
+
end
|
22
34
|
}
|
23
35
|
|
24
36
|
if element.options[:collection]
|
data/lib/relief/parser.rb
CHANGED
@@ -5,13 +5,13 @@ module Relief
|
|
5
5
|
class Parser
|
6
6
|
attr_reader :root
|
7
7
|
|
8
|
-
def initialize(name, options={}, &block)
|
8
|
+
def initialize(name=nil, options={}, &block)
|
9
9
|
@root = Element.new(name, options, &block)
|
10
10
|
end
|
11
11
|
|
12
12
|
def parse(document)
|
13
13
|
unless document.is_a?(Nokogiri::XML::NodeSet)
|
14
|
-
document = Nokogiri::XML(document)
|
14
|
+
document = Nokogiri::XML(document.to_s)
|
15
15
|
end
|
16
16
|
|
17
17
|
@root.parse(document)
|
data/spec/parser_spec.rb
CHANGED
@@ -123,4 +123,55 @@ describe Relief::Parser do
|
|
123
123
|
|
124
124
|
photos.should == { :name => ['Cucumbers', 'Lemons'] }
|
125
125
|
end
|
126
|
+
|
127
|
+
it "parses elements with type casting" do
|
128
|
+
parser = Relief::Parser.new(:photo) do
|
129
|
+
element :id, :type => Integer
|
130
|
+
element :rating, :type => Float
|
131
|
+
element :published, :type => Date
|
132
|
+
end
|
133
|
+
|
134
|
+
photo = parser.parse(<<-XML)
|
135
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
136
|
+
<photo>
|
137
|
+
<id>86634</id>
|
138
|
+
<rating>3.5</rating>
|
139
|
+
<published>2009-05-08T18:23:26-07:00</url>
|
140
|
+
</photo>
|
141
|
+
XML
|
142
|
+
|
143
|
+
photo.should == {
|
144
|
+
:id => 86634,
|
145
|
+
:rating => 3.5,
|
146
|
+
:published => Date.parse('2009-05-08T18:23:26-07:00')
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
it "parses elements with custom type casting" do
|
151
|
+
author = Relief::Parser.new do
|
152
|
+
element :name
|
153
|
+
element :email
|
154
|
+
end
|
155
|
+
|
156
|
+
parser = Relief::Parser.new(:photo) do
|
157
|
+
element :author, :type => author
|
158
|
+
end
|
159
|
+
|
160
|
+
photo = parser.parse(<<-XML)
|
161
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
162
|
+
<photo>
|
163
|
+
<author>
|
164
|
+
<name>Jennifer Stone</name>
|
165
|
+
<email>jstone@example.com</email>
|
166
|
+
</author>
|
167
|
+
</photo>
|
168
|
+
XML
|
169
|
+
|
170
|
+
photo.should == {
|
171
|
+
:author => {
|
172
|
+
:name => 'Jennifer Stone',
|
173
|
+
:email => 'jstone@example.com'
|
174
|
+
}
|
175
|
+
}
|
176
|
+
end
|
126
177
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: relief
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Hunt
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-12 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|