nori 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nori might be problematic. Click here for more details.

@@ -1,3 +1,17 @@
1
+ == 0.2.1 (2011-05-15)
2
+
3
+ * Fix: Changed XML attributes converted to Hash keys to be prefixed with an @-sign.
4
+ This avoids problems with attributes and child nodes having the same name.
5
+
6
+ <multiRef id="id1">
7
+ <approved xsi:type="xsd:boolean">true</approved>
8
+ <id xsi:type="xsd:long">76737</id>
9
+ </multiRef>
10
+
11
+ is now translated to:
12
+
13
+ { "multiRef" => { "@id" => "id1", "id" => "76737", "approved" => "true" } }
14
+
1
15
  == 0.2.0 (2011-04-30)
2
16
 
3
17
  * Removed JSON from the original Crack basis
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  Nori ![http://travis-ci.org/rubiii/nori](http://travis-ci.org/rubiii/nori.png)
2
2
  ====
3
3
 
4
- Really simple XML parsing ripped from Crack which ripped it from Merb.
5
- Nori was created to bypass the stale development of Crack, improve its XML parser
4
+ Really simple XML parsing ripped from Crack which ripped it from Merb.
5
+ Nori was created to bypass the stale development of Crack, improve its XML parser
6
6
  and fix certain issues.
7
7
 
8
8
  ``` ruby
@@ -10,12 +10,12 @@ Nori.parse("<tag>This is the contents</tag>")
10
10
  # => { 'tag' => 'This is the contents' }
11
11
  ```
12
12
 
13
- Nori supports pluggable parsers and ships with both REXML and Nokogiri implementations.
13
+ Nori supports pluggable parsers and ships with both REXML and Nokogiri implementations.
14
14
  It defaults to REXML, but you can change it to use Nokogiri via:
15
15
 
16
16
  ``` ruby
17
17
  Nori.parser = :nokogiri
18
18
  ```
19
19
 
20
- Make sure Nokogiri is in your LOAD_PATH when parsing XML, because Nori tries to load it
20
+ Make sure Nokogiri is in your LOAD_PATH when parsing XML, because Nori tries to load it
21
21
  when it's needed.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ task :benchmark do
9
9
  end
10
10
 
11
11
  RSpec::Core::RakeTask.new do |t|
12
- t.rspec_opts = %w(-fd -c)
12
+ t.rspec_opts = %w(-c)
13
13
  end
14
14
 
15
15
  task :default => :spec
@@ -1,5 +1,5 @@
1
1
  module Nori
2
2
 
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
 
5
5
  end
@@ -65,6 +65,13 @@ module Nori
65
65
 
66
66
  attr_accessor :name, :attributes, :children, :type
67
67
 
68
+ def prefixed_attributes
69
+ attributes.inject({}) do |memo, (key, value)|
70
+ memo["@#{key}"] = value
71
+ memo
72
+ end
73
+ end
74
+
68
75
  def add_node(node)
69
76
  @text = true if node.is_a? String
70
77
  @children << node
@@ -83,8 +90,12 @@ module Nori
83
90
 
84
91
  if @text
85
92
  t = typecast_value( unnormalize_xml_entities( inner_html ) )
86
- t.class.send(:attr_accessor, :attributes)
87
- t.attributes = attributes
93
+ if t.is_a?(String)
94
+ class << t
95
+ attr_accessor :attributes
96
+ end
97
+ t.attributes = attributes
98
+ end
88
99
  return { name => t }
89
100
  else
90
101
  #change repeating groups into an array
@@ -111,7 +122,7 @@ module Nori
111
122
  out.merge!( k => v.map{|e| e.to_hash[k]})
112
123
  end
113
124
  end
114
- out.merge! attributes unless attributes.empty?
125
+ out.merge! prefixed_attributes unless attributes.empty?
115
126
  out = out.empty? ? nil : out
116
127
  end
117
128
 
@@ -25,7 +25,7 @@ describe Nori do
25
25
 
26
26
  it "should transform a simple tag with attributes" do
27
27
  xml = "<tag attr1='1' attr2='2'></tag>"
28
- hash = { 'tag' => { 'attr1' => '1', 'attr2' => '2' } }
28
+ hash = { 'tag' => { '@attr1' => '1', '@attr2' => '2' } }
29
29
  parse(xml).should == hash
30
30
  end
31
31
 
@@ -42,11 +42,11 @@ describe Nori do
42
42
  hash = {
43
43
  'opt' => {
44
44
  'user' => [{
45
- 'login' => 'grep',
46
- 'fullname' => 'Gary R Epstein'
45
+ '@login' => 'grep',
46
+ '@fullname' => 'Gary R Epstein'
47
47
  },{
48
- 'login' => 'stty',
49
- 'fullname' => 'Simon T Tyson'
48
+ '@login' => 'stty',
49
+ '@fullname' => 'Simon T Tyson'
50
50
  }]
51
51
  }
52
52
  }
@@ -66,8 +66,8 @@ describe Nori do
66
66
  hash = {
67
67
  'opt' => {
68
68
  'user' => {
69
- 'login' => 'grep',
70
- 'fullname' => 'Gary R Epstein'
69
+ '@login' => 'grep',
70
+ '@fullname' => 'Gary R Epstein'
71
71
  }
72
72
  }
73
73
  }
@@ -75,6 +75,17 @@ describe Nori do
75
75
  parse(xml).should == hash
76
76
  end
77
77
 
78
+ it "should prefix attributes with an @-sign to avoid problems with overwritten values" do
79
+ xml =<<-XML
80
+ <multiRef id="id1">
81
+ <approved>true</approved>
82
+ <id>76737</id>
83
+ </multiRef>
84
+ XML
85
+
86
+ parse(xml)["multiRef"].should == { "approved" => "true", "@id" => "id1", "id" => "76737" }
87
+ end
88
+
78
89
  context "Parsing xml with text and attributes" do
79
90
  before do
80
91
  xml =<<-XML
@@ -104,6 +115,16 @@ describe Nori do
104
115
  it "default attributes to empty hash if not present" do
105
116
  @data['opt']['user'][1].attributes.should == {}
106
117
  end
118
+
119
+ it "add 'attributes' accessor methods to parsed instances of String" do
120
+ @data['opt']['user'][0].should respond_to(:attributes)
121
+ @data['opt']['user'][0].should respond_to(:attributes=)
122
+ end
123
+
124
+ it "not add 'attributes' accessor methods to all instances of String" do
125
+ "some-string".should_not respond_to(:attributes)
126
+ "some-string".should_not respond_to(:attributes=)
127
+ end
107
128
  end
108
129
 
109
130
  it "should typecast an integer" do
@@ -148,26 +169,26 @@ describe Nori do
148
169
  end
149
170
 
150
171
  it "should unescape XML entities in attributes" do
151
- xml_entities.each do |k,v|
152
- xml = "<tag attr='Some content #{v}'></tag>"
153
- parse(xml)['tag']['attr'].should =~ Regexp.new(k)
172
+ xml_entities.each do |key, value|
173
+ xml = "<tag attr='Some content #{value}'></tag>"
174
+ parse(xml)['tag']['@attr'].should =~ Regexp.new(key)
154
175
  end
155
176
  end
156
177
 
157
178
  it "should undasherize keys as tags" do
158
179
  xml = "<tag-1>Stuff</tag-1>"
159
- parse(xml).keys.should include( 'tag_1' )
180
+ parse(xml).keys.should include('tag_1')
160
181
  end
161
182
 
162
183
  it "should undasherize keys as attributes" do
163
184
  xml = "<tag1 attr-1='1'></tag1>"
164
- parse(xml)['tag1'].keys.should include( 'attr_1')
185
+ parse(xml)['tag1'].keys.should include('@attr_1')
165
186
  end
166
187
 
167
188
  it "should undasherize keys as tags and attributes" do
168
189
  xml = "<tag-1 attr-1='1'></tag-1>"
169
- parse(xml).keys.should include( 'tag_1' )
170
- parse(xml)['tag_1'].keys.should include( 'attr_1')
190
+ parse(xml).keys.should include('tag_1')
191
+ parse(xml)['tag_1'].keys.should include('@attr_1')
171
192
  end
172
193
 
173
194
  it "should render nested content correctly" do
@@ -203,7 +224,7 @@ describe Nori do
203
224
 
204
225
  hash = {
205
226
  "user" => {
206
- "gender" => "m",
227
+ "@gender" => "m",
207
228
  "age" => 35,
208
229
  "name" => "Home Simpson",
209
230
  "dob" => Date.parse('1988-01-01'),
@@ -347,14 +368,14 @@ describe Nori do
347
368
  EOT
348
369
 
349
370
  expected_topic_hash = {
350
- 'id' => "175756086",
351
- 'owner' => "55569174@N00",
352
- 'secret' => "0279bf37a1",
353
- 'server' => "76",
354
- 'title' => "Colored Pencil PhotoBooth Fun",
355
- 'ispublic' => "1",
356
- 'isfriend' => "0",
357
- 'isfamily' => "0",
371
+ '@id' => "175756086",
372
+ '@owner' => "55569174@N00",
373
+ '@secret' => "0279bf37a1",
374
+ '@server' => "76",
375
+ '@title' => "Colored Pencil PhotoBooth Fun",
376
+ '@ispublic' => "1",
377
+ '@isfriend' => "0",
378
+ '@isfamily' => "0",
358
379
  }
359
380
 
360
381
  parse(topic_xml)["rsp"]["photos"]["photo"].each do |k, v|
@@ -471,7 +492,7 @@ describe Nori do
471
492
 
472
493
  expected_product_hash = {
473
494
  'weight' => 0.5,
474
- 'image' => {'type' => 'ProductImage', 'filename' => 'image.gif' },
495
+ 'image' => {'@type' => 'ProductImage', 'filename' => 'image.gif' },
475
496
  }
476
497
 
477
498
  parse(product_xml)["product"].should == expected_product_hash
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nori
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Harrington
@@ -17,8 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-04-30 00:00:00 +02:00
21
- default_executable:
20
+ date: 2011-05-15 00:00:00 Z
22
21
  dependencies:
23
22
  - !ruby/object:Gem::Dependency
24
23
  name: nokogiri
@@ -102,7 +101,6 @@ files:
102
101
  - spec/nori/nori_spec.rb
103
102
  - spec/nori/parser_spec.rb
104
103
  - spec/spec_helper.rb
105
- has_rdoc: true
106
104
  homepage: http://github.com/rubiii/nori
107
105
  licenses: []
108
106
 
@@ -132,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
130
  requirements: []
133
131
 
134
132
  rubyforge_project: nori
135
- rubygems_version: 1.4.2
133
+ rubygems_version: 1.7.2
136
134
  signing_key:
137
135
  specification_version: 3
138
136
  summary: XML to Hash translator