dozuki 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ module Dozuki
2
+ module Parsers
3
+ module String
4
+ def self.parse(node)
5
+ node.text.strip
6
+ end
7
+ end
8
+ module Integer
9
+ def self.parse(node)
10
+ string = String.parse(node)
11
+ begin
12
+ Integer(string)
13
+ rescue ArgumentError
14
+ raise InvalidFormat.new(:node => node, :value => string, :format => "integer")
15
+ end
16
+ end
17
+ end
18
+ module Float
19
+ def self.parse(node)
20
+ string = String.parse(node)
21
+ begin
22
+ Float(string)
23
+ rescue ArgumentError
24
+ raise InvalidFormat.new(:node => node, :value => string, :format => "float")
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Dozuki
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,12 +1,7 @@
1
- require 'dozuki/xml/node'
2
- require 'dozuki/xml/node_collection'
3
- require 'dozuki/xml/parser'
4
- require 'dozuki/xml/exceptions'
5
-
6
1
  module Dozuki
7
2
  module XML
8
- def self.parse(string)
9
- Node.new(Nokogiri::XML.parse(string))
3
+ def self.parse(*args)
4
+ Node.new(Nokogiri::XML.parse(*args))
10
5
  end
11
6
  end
12
- end
7
+ end
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+ module Dozuki
3
+ describe NodeCollection do
4
+
5
+ describe "as_node" do
6
+ let(:collection){mock "some_nodes"}
7
+ let(:blk){Proc.new{}}
8
+ let(:node_collection){NodeCollection.new(collection)}
9
+ let(:collection_item){mock "collection item"}
10
+ let(:new_node){mock "new node"}
11
+ before(:each) do
12
+ collection.stub(:each).and_yield(collection_item)
13
+ Node.stub(:new).and_return(new_node)
14
+ end
15
+
16
+ subject{node_collection.as_node(&blk)}
17
+
18
+ it "should iterate through the collection" do
19
+ collection.should_receive(:each)
20
+ subject
21
+ end
22
+ it "should create a new Node with the yielded collection item" do
23
+ Node.should_receive(:new).with(collection_item)
24
+ subject
25
+ end
26
+ it "should call the bloc with the node" do
27
+ blk.should_receive(:call).with(new_node)
28
+ subject
29
+ end
30
+ end
31
+
32
+ describe "as_string" do
33
+ let(:collection){mock "some_nodes"}
34
+ let(:blk){Proc.new{}}
35
+ let(:node_collection){NodeCollection.new(collection)}
36
+ let(:collection_item){mock "collection item"}
37
+ let(:string){"string"}
38
+ before(:each) do
39
+ collection.stub(:each).and_yield(collection_item)
40
+ Dozuki::Parsers::String.stub(:parse).and_return(string)
41
+ end
42
+
43
+ subject{node_collection.as_string(&blk)}
44
+
45
+ it "should iterate through the collection" do
46
+ collection.should_receive(:each)
47
+ subject
48
+ end
49
+ it "should create a new Node with the yielded collection item" do
50
+ Dozuki::Parsers::String.should_receive(:parse).with(collection_item)
51
+ subject
52
+ end
53
+ it "should call the bloc with the node" do
54
+ blk.should_receive(:call).with("string")
55
+ subject
56
+ end
57
+ end
58
+
59
+ describe "as_int" do
60
+ let(:collection){mock "some_nodes"}
61
+ let(:blk){Proc.new{}}
62
+ let(:node_collection){NodeCollection.new(collection)}
63
+ let(:collection_item){mock "collection item"}
64
+ let(:int){39}
65
+ before(:each) do
66
+ collection.stub(:each).and_yield(collection_item)
67
+ Dozuki::Parsers::Integer.stub(:parse).and_return(int)
68
+ end
69
+
70
+ subject{node_collection.as_int(&blk)}
71
+
72
+ it "should iterate through the collection" do
73
+ collection.should_receive(:each)
74
+ subject
75
+ end
76
+ it "should create a new Node with the yielded collection item" do
77
+ Dozuki::Parsers::Integer.should_receive(:parse).with(collection_item)
78
+ subject
79
+ end
80
+ it "should call the bloc with the node" do
81
+ blk.should_receive(:call).with(int)
82
+ subject
83
+ end
84
+ end
85
+
86
+ describe "as_float" do
87
+ let(:collection){mock "some_nodes"}
88
+ let(:blk){Proc.new{}}
89
+ let(:node_collection){NodeCollection.new(collection)}
90
+ let(:collection_item){mock "collection item"}
91
+ let(:float){39.50}
92
+ before(:each) do
93
+ collection.stub(:each).and_yield(collection_item)
94
+ Dozuki::Parsers::Float.stub(:parse).and_return(float)
95
+ end
96
+
97
+ subject{node_collection.as_float(&blk)}
98
+
99
+ it "should iterate through the collection" do
100
+ collection.should_receive(:each)
101
+ subject
102
+ end
103
+ it "should create a new Node with the yielded collection item" do
104
+ Dozuki::Parsers::Float.should_receive(:parse).with(collection_item)
105
+ subject
106
+ end
107
+ it "should call the bloc with the node" do
108
+ blk.should_receive(:call).with(float)
109
+ subject
110
+ end
111
+ end
112
+
113
+ end
114
+ end
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- module Dozuki::XML
3
+ module Dozuki
4
4
  describe Node do
5
-
5
+
6
6
  describe "initialization" do
7
7
  let(:nokogiri_node){mock("nokogiri_node")}
8
8
  subject{Node.new(nokogiri_node)}
9
9
  its(:nokogiri_node){should == nokogiri_node}
10
10
  end
11
-
11
+
12
12
  describe "missing methods" do
13
13
  let(:nokogiri_node){mock("nokogiri node")}
14
14
  subject{Node.new(nokogiri_node)}
@@ -26,8 +26,8 @@ module Dozuki::XML
26
26
  nokogiri_node.should_receive(:blah).with(&blk)
27
27
  subject.blah(&blk)
28
28
  end
29
- end
30
-
29
+ end
30
+
31
31
  describe "responding to nokogiri's methods" do
32
32
  let(:nokogiri_node){mock("nokogiri node")}
33
33
  let(:node){Node.new(nokogiri_node)}
@@ -45,7 +45,7 @@ module Dozuki::XML
45
45
  end
46
46
  end
47
47
  end
48
-
48
+
49
49
  describe "each" do
50
50
  let(:nokogiri_node) {mock("nokogiri_node")}
51
51
  let(:xpath) {"/some/xpath"}
@@ -59,21 +59,21 @@ module Dozuki::XML
59
59
 
60
60
  end
61
61
  subject{node.each(xpath)}
62
-
62
+
63
63
  it "should get the collection from the nokogiri node" do
64
64
  nokogiri_node.should_receive(:xpath).with(xpath)
65
65
  subject
66
66
  end
67
-
67
+
68
68
  it "should create a new NodeCollection with the collection" do
69
69
  NodeCollection.should_receive(:new).with(collection)
70
70
  subject
71
71
  end
72
-
72
+
73
73
  it "should return the collection" do
74
74
  subject.should == node_collection
75
75
  end
76
-
76
+
77
77
  context "where no block is passed" do
78
78
  before(:each) do
79
79
  node_collection.stub(:as_node).and_return(node_collection)
@@ -85,27 +85,27 @@ module Dozuki::XML
85
85
  end
86
86
  end
87
87
  end
88
-
88
+
89
89
  describe "string" do
90
90
  let(:nokogiri_node) { mock("nokogiri_node") }
91
91
  let(:xpath) { "/some/xpath" }
92
92
  let(:node) { Node.new(nokogiri_node) }
93
93
  let(:text_node) { mock "some node" }
94
94
  let(:parsed) { mock "parsed result" }
95
-
95
+
96
96
  before(:each) do
97
97
  nokogiri_node.stub(:xpath).and_return([text_node])
98
- Parser.stub(:to_string).and_return(parsed)
98
+ Dozuki::Parsers::String.stub(:parse).and_return(parsed)
99
99
  end
100
-
100
+
101
101
  subject{ node.string(xpath) }
102
-
102
+
103
103
  it "should get the collection from the nokogiri node" do
104
104
  nokogiri_node.should_receive(:xpath).with(xpath)
105
105
  subject
106
106
  end
107
107
  it "should parse the node to a string" do
108
- Parser.should_receive(:to_string).with(text_node)
108
+ Dozuki::Parsers::String.should_receive(:parse).with(text_node)
109
109
  subject
110
110
  end
111
111
  it "should return the parsed result" do
@@ -116,31 +116,31 @@ module Dozuki::XML
116
116
  nokogiri_node.should_receive(:xpath).and_return([])
117
117
  end
118
118
  it "should raise a NotFound error" do
119
- expect{subject}.to raise_error(NotFound)
119
+ expect{subject}.to raise_error(Dozuki::NotFound)
120
120
  end
121
121
  end
122
122
  end
123
-
123
+
124
124
  describe "int" do
125
125
  let(:nokogiri_node) { mock("nokogiri_node") }
126
126
  let(:xpath) { "/some/xpath" }
127
127
  let(:node) { Node.new(nokogiri_node) }
128
128
  let(:int_node) { mock "some node" }
129
129
  let(:parsed) { mock "parsed result" }
130
-
130
+
131
131
  before(:each) do
132
132
  nokogiri_node.stub(:xpath).and_return([int_node])
133
- Parser.stub(:to_int).and_return(parsed)
133
+ Dozuki::Parsers::Integer.stub(:parse).and_return(parsed)
134
134
  end
135
-
135
+
136
136
  subject{ node.int(xpath) }
137
-
137
+
138
138
  it "should get the collection from the nokogiri node" do
139
139
  nokogiri_node.should_receive(:xpath).with(xpath)
140
140
  subject
141
141
  end
142
142
  it "should parse the node to an int" do
143
- Parser.should_receive(:to_int).with(int_node)
143
+ Dozuki::Parsers::Integer.should_receive(:parse).with(int_node)
144
144
  subject
145
145
  end
146
146
  it "should return the parsed result" do
@@ -151,31 +151,31 @@ module Dozuki::XML
151
151
  nokogiri_node.should_receive(:xpath).and_return([])
152
152
  end
153
153
  it "should raise a NotFound error" do
154
- expect{subject}.to raise_error(NotFound)
154
+ expect{subject}.to raise_error(Dozuki::NotFound)
155
155
  end
156
156
  end
157
157
  end
158
-
158
+
159
159
  describe "float" do
160
160
  let(:nokogiri_node) { mock("nokogiri_node") }
161
161
  let(:xpath) { "/some/xpath" }
162
162
  let(:node) { Node.new(nokogiri_node) }
163
163
  let(:float_node) { mock "some node" }
164
164
  let(:parsed) { mock "parsed result" }
165
-
165
+
166
166
  before(:each) do
167
167
  nokogiri_node.stub(:xpath).and_return([float_node])
168
- Parser.stub(:to_float).and_return(parsed)
168
+ Dozuki::Parsers::Float.stub(:parse).and_return(parsed)
169
169
  end
170
-
170
+
171
171
  subject{ node.float(xpath) }
172
-
172
+
173
173
  it "should get the collection from the nokogiri node" do
174
174
  nokogiri_node.should_receive(:xpath).with(xpath)
175
175
  subject
176
176
  end
177
177
  it "should parse the node to an int" do
178
- Parser.should_receive(:to_float).with(float_node)
178
+ Dozuki::Parsers::Float.should_receive(:parse).with(float_node)
179
179
  subject
180
180
  end
181
181
  it "should return the parsed result" do
@@ -186,23 +186,23 @@ module Dozuki::XML
186
186
  nokogiri_node.should_receive(:xpath).and_return([])
187
187
  end
188
188
  it "should raise a NotFound error" do
189
- expect{subject}.to raise_error(NotFound)
189
+ expect{subject}.to raise_error(Dozuki::NotFound)
190
190
  end
191
191
  end
192
192
  end
193
-
193
+
194
194
  describe "get" do
195
195
  let(:nokogiri_node) { mock("nokogiri_node") }
196
196
  let(:xpath) { "/some/xpath" }
197
197
  let(:node) { Node.new(nokogiri_node) }
198
198
  let(:wanted_node) { mock "some node" }
199
-
199
+
200
200
  before(:each) do
201
201
  nokogiri_node.stub(:xpath).and_return([wanted_node])
202
202
  end
203
-
203
+
204
204
  subject{ node.get(xpath) }
205
-
205
+
206
206
  it "should get the collection from the nokogiri node" do
207
207
  nokogiri_node.should_receive(:xpath).with(xpath)
208
208
  subject
@@ -210,13 +210,13 @@ module Dozuki::XML
210
210
  it "should return the new dozuki node" do
211
211
  subject.nokogiri_node.should == wanted_node
212
212
  end
213
-
213
+
214
214
  context "where the xpath returns no node" do
215
215
  before(:each) do
216
216
  nokogiri_node.should_receive(:xpath).and_return([])
217
217
  end
218
218
  it "should raise a NotFound error" do
219
- expect{subject}.to raise_error(NotFound)
219
+ expect{subject}.to raise_error(Dozuki::NotFound)
220
220
  end
221
221
  end
222
222
  context "where a block is passed" do
@@ -228,7 +228,7 @@ module Dozuki::XML
228
228
  end
229
229
  end
230
230
  end
231
-
231
+
232
232
  describe "exists?" do
233
233
  let(:nokogiri_node) { mock("nokogiri_node") }
234
234
  let(:xpath) { "/some/xpath" }
@@ -252,6 +252,6 @@ module Dozuki::XML
252
252
  end
253
253
  end
254
254
  end
255
-
255
+
256
256
  end
257
- end
257
+ end
@@ -1,12 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Dozuki
4
- module XML
5
- describe Parser do
6
- describe "to_text" do
4
+ module Parsers
5
+
6
+ describe String do
7
+ describe ".parse" do
7
8
  let(:string){"something"}
8
9
  let(:node){mock "node", :text => string}
9
- subject{Parser.to_string(node)}
10
+ subject{String.parse(node)}
10
11
  it "should get the text" do
11
12
  subject.should == "something"
12
13
  end
@@ -17,10 +18,13 @@ module Dozuki
17
18
  end
18
19
  end
19
20
  end
20
- describe "to_int" do
21
+ end
22
+
23
+ describe Integer do
24
+ describe ".parse" do
21
25
  let(:string){"2"}
22
26
  let(:node){mock "node", :text => string}
23
- subject{Parser.to_int(node)}
27
+ subject{Integer.parse(node)}
24
28
  it "should get the integer" do
25
29
  subject.should == 2
26
30
  end
@@ -34,7 +38,7 @@ module Dozuki
34
38
  context "where there is characters in the string" do
35
39
  let(:string){"fjkdsk"}
36
40
  it "should raise an error" do
37
- expect{subject}.to raise_error(InvalidFormat)
41
+ expect{subject}.to raise_error(InvalidFormat, "Cannot parse 'fjkdsk' to integer")
38
42
  end
39
43
  end
40
44
  context "where there are dots" do
@@ -45,34 +49,38 @@ module Dozuki
45
49
  end
46
50
  end
47
51
  end
48
- describe "to_float" do
49
- let(:string){"22.50"}
50
- let(:node){mock "node", :text => string}
51
- subject{Parser.to_float(node)}
52
- it "should get the float" do
53
- subject.should == 22.50
54
- end
55
- context "where the string has whitespace" do
56
- let(:string){"\n\n22.59"}
57
- it "should strip the whitespace" do
58
- subject.should == 22.59
52
+
53
+ describe Float do
54
+ describe ".parse" do
55
+ let(:string){"22.50"}
56
+ let(:node){mock "node", :text => string}
57
+ subject{Float.parse(node)}
58
+ it "should get the float" do
59
+ subject.should == 22.50
59
60
  end
60
- end
61
- context "where the string is badly formatted" do
62
- context "where there is characters in the string" do
63
- let(:string){"fjkdsk"}
64
- it "should raise an error" do
65
- expect{subject}.to raise_error(InvalidFormat)
61
+ context "where the string has whitespace" do
62
+ let(:string){"\n\n22.59"}
63
+ it "should strip the whitespace" do
64
+ subject.should == 22.59
66
65
  end
67
66
  end
68
- context "where there are multiple dots" do
69
- let(:string){"0.0.4"}
70
- it "should raise an error" do
71
- expect{subject}.to raise_error(InvalidFormat)
67
+ context "where the string is badly formatted" do
68
+ context "where there is characters in the string" do
69
+ let(:string){"fjkdsk"}
70
+ it "should raise an error" do
71
+ expect{subject}.to raise_error(InvalidFormat, "Cannot parse 'fjkdsk' to float")
72
+ end
73
+ end
74
+ context "where there are multiple dots" do
75
+ let(:string){"0.0.4"}
76
+ it "should raise an error" do
77
+ expect{subject}.to raise_error(InvalidFormat)
78
+ end
72
79
  end
73
80
  end
74
81
  end
75
82
  end
83
+
76
84
  end
77
85
  end
78
- end
86
+ end