dozuki 0.3.0 → 0.4.0

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/README.markdown CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A Nokogiri wrapper that simplifies commonly occurring tasks.
4
4
 
5
- [![Build Status](http://travis-ci.org/jamesalmond/dozuki.png)](http://travis-ci.org/JamesAlmond/dozuki)
5
+ [![Build Status](http://travis-ci.org/jamesalmond/dozuki.png)](http://travis-ci.org/jamesalmond/dozuki)
6
6
 
7
7
  ## What does it do?
8
8
 
@@ -0,0 +1,43 @@
1
+ Feature: Getting booleans from the document
2
+ In order to provide simpler way of getting booleans from a node
3
+ As a traverser
4
+ I want to access nodes using the boolean method and an xpath
5
+
6
+ Background:
7
+ Given I have parsed the XML:
8
+ """
9
+ <root>
10
+ <name>St. George's Arms</name>
11
+ <open>1</open>
12
+ <licensed>
13
+ 0
14
+ </licensed>
15
+ <clean>
16
+ true
17
+ </clean>
18
+ <worth_visiting>false</worth_visiting>
19
+ </root>
20
+ """
21
+
22
+ Scenario: getting the boolean of a 1 single node
23
+ When I call "boolean('/root/open')" on the document
24
+ Then the result should be true
25
+
26
+ Scenario: getting the boolean of a 0 single node with whitespace
27
+ When I call "boolean('/root/licensed')" on the document
28
+ Then the result should be false
29
+
30
+ Scenario: getting the boolean of a true single node with whitespace
31
+ When I call "boolean('/root/clean')" on the document
32
+ Then the result should be true
33
+
34
+ Scenario: getting the boolean of a false single node with whitespace
35
+ When I call "boolean('/root/worth_visiting')" on the document
36
+ Then the result should be false
37
+
38
+ Scenario: getting a non-existent node
39
+ When I call "float('//something/missing')" on the document
40
+ Then it should raise a "NotFound" error
41
+ And the error should have the xpath "//something/missing"
42
+ And the error should have a stored node
43
+
@@ -24,6 +24,10 @@ Feature: Iterating through nodes
24
24
  <day>2011-05-03</day>
25
25
  <day>2013-07-09</day>
26
26
  </available>
27
+ <open>
28
+ <day>true</day>
29
+ <day>0</day>
30
+ </open>
27
31
  </root>
28
32
  """
29
33
 
@@ -51,3 +55,8 @@ Feature: Iterating through nodes
51
55
  When I call "each('/root/available/day').as_date" on the document and collect the results
52
56
  Then the results should contain 03/05/2011
53
57
  And the results should contain 09/07/2013
58
+
59
+ Scenario: using each to traverse a document and getting the boolean elements
60
+ When I call "each('/root/open/day').as_boolean" on the document and collect the results
61
+ Then the results should contain true
62
+ And the results should contain false
@@ -13,4 +13,11 @@ end
13
13
  Then /^the results should contain (\d+)\/(\d+)\/(\d+)$/ do |day, month, year|
14
14
  @results.should include(Date.civil(year.to_i, month.to_i, day.to_i))
15
15
  end
16
+ Then /^the results should contain true$/ do
17
+ @results.should include true
18
+ end
19
+
20
+ Then /^the results should contain false$/ do
21
+ @results.should include false
22
+ end
16
23
 
data/lib/dozuki/node.rb CHANGED
@@ -31,10 +31,15 @@ module Dozuki
31
31
  def float(xpath)
32
32
  Parsers::Float.parse(get_first_node(xpath))
33
33
  end
34
+
34
35
  def date(xpath)
35
36
  Parsers::Date.parse(get_first_node(xpath))
36
37
  end
37
38
 
39
+ def boolean(xpath)
40
+ Parsers::Boolean.parse(get_first_node(xpath))
41
+ end
42
+
38
43
  def get(xpath)
39
44
  node = Node.new(get_first_node(xpath))
40
45
  yield node if block_given?
@@ -11,18 +11,28 @@ module Dozuki
11
11
  end
12
12
 
13
13
  def as_string(&blk)
14
- collection.each{|item| blk.call(Parsers::String.parse(item))}
14
+ each_as(Parsers::String, &blk)
15
15
  end
16
16
 
17
17
  def as_int(&blk)
18
- collection.each{|item| blk.call(Parsers::Integer.parse(item))}
18
+ each_as(Parsers::Integer, &blk)
19
19
  end
20
20
 
21
21
  def as_float(&blk)
22
- collection.each{|item| blk.call(Parsers::Float.parse(item))}
22
+ each_as(Parsers::Float, &blk)
23
23
  end
24
+
24
25
  def as_date(&blk)
25
- collection.each{|item| blk.call(Parsers::Date.parse(item))}
26
+ each_as(Parsers::Date, &blk)
27
+ end
28
+
29
+ def as_boolean(&blk)
30
+ each_as(Parsers::Boolean, &blk)
26
31
  end
32
+
33
+ private
34
+ def each_as(parser, &blk)
35
+ collection.each{|item| blk.call(parser.parse(item))}
36
+ end
27
37
  end
28
38
  end
@@ -35,5 +35,13 @@ module Dozuki
35
35
  end
36
36
  end
37
37
  end
38
+ module Boolean
39
+ def self.parse(node)
40
+ true_values.include? String.parse(node)
41
+ end
42
+ def self.true_values
43
+ ['1', 'true']
44
+ end
45
+ end
38
46
  end
39
47
  end
@@ -1,3 +1,3 @@
1
1
  module Dozuki
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -85,7 +85,7 @@ module Dozuki
85
85
  @block_called_with.should == a_float
86
86
  end
87
87
  end
88
-
88
+
89
89
  describe "as_date" do
90
90
 
91
91
  let(:a_date){Date.civil(2012, 2,5)}
@@ -105,6 +105,25 @@ module Dozuki
105
105
  @block_called_with.should == a_date
106
106
  end
107
107
  end
108
+ describe "as_boolean" do
109
+
110
+ let(:a_boolean){true}
111
+
112
+ before(:each) do
113
+ Dozuki::Parsers::Boolean.stub(:parse).and_return(a_boolean)
114
+ end
115
+
116
+ subject{node_collection.as_boolean{|arg| @block_called_with = arg}}
117
+
118
+ it "should parse the node to a boolean" do
119
+ Dozuki::Parsers::Boolean.should_receive(:parse).with(collection_item).and_return(a_boolean)
120
+ subject
121
+ end
122
+ it "should call the block with the boolean" do
123
+ subject
124
+ @block_called_with.should == a_boolean
125
+ end
126
+ end
108
127
 
109
128
  end
110
129
  end
@@ -226,6 +226,41 @@ module Dozuki
226
226
  end
227
227
  end
228
228
 
229
+ describe "boolean" do
230
+ let(:nokogiri_node) { mock("nokogiri_node") }
231
+ let(:xpath) { "/some/xpath" }
232
+ let(:node) { Node.new(nokogiri_node) }
233
+ let(:boolean_node) { mock "some node" }
234
+ let(:parsed) { mock "parsed result" }
235
+
236
+ before(:each) do
237
+ nokogiri_node.stub(:xpath).and_return([boolean_node])
238
+ Dozuki::Parsers::Boolean.stub(:parse).and_return(parsed)
239
+ end
240
+
241
+ subject{ node.boolean(xpath) }
242
+
243
+ it "should get the collection from the nokogiri node" do
244
+ nokogiri_node.should_receive(:xpath).with(xpath).and_return([boolean_node])
245
+ subject
246
+ end
247
+ it "should parse the node to a date" do
248
+ Dozuki::Parsers::Boolean.should_receive(:parse).with(boolean_node).and_return(parsed)
249
+ subject
250
+ end
251
+ it "should return the parsed result" do
252
+ subject.should == parsed
253
+ end
254
+ context "where the xpath returns no node" do
255
+ before(:each) do
256
+ nokogiri_node.should_receive(:xpath).and_return([])
257
+ end
258
+ it "should raise a NotFound error" do
259
+ expect{subject}.to raise_error(Dozuki::NotFound)
260
+ end
261
+ end
262
+ end
263
+
229
264
  describe "get" do
230
265
  let(:nokogiri_node) { mock("nokogiri_node") }
231
266
  let(:xpath) { "/some/xpath" }
@@ -101,5 +101,38 @@ module Dozuki
101
101
  end
102
102
  end
103
103
  end
104
+
105
+ describe Boolean do
106
+ let(:node) {mock :node, :text => string}
107
+ subject{ Boolean.parse(node)}
108
+ context "where string is 0" do
109
+ let(:string){"0"}
110
+ it{should be_false}
111
+ context "and there is whitespace" do
112
+ let(:string){"\n0"}
113
+ it{should be_false}
114
+ end
115
+ end
116
+ context "where string is 1" do
117
+ let(:string){"1"}
118
+ it{should be_true}
119
+ context "and there is whitespace" do
120
+ let(:string){"\n1"}
121
+ it{should be_true}
122
+ end
123
+ end
124
+ context "where string is true" do
125
+ let(:string){ "true" }
126
+ it {should be_true}
127
+ context "and there is whitespace" do
128
+ let(:string){"\ntrue"}
129
+ it{should be_true}
130
+ end
131
+ end
132
+ context "where string is false" do
133
+ let(:string){ "false"}
134
+ it{ should be_false }
135
+ end
136
+ end
104
137
  end
105
138
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: dozuki
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Almond
@@ -10,11 +10,12 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-09 00:00:00 +01:00
13
+ date: 2011-05-10 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: nokogiri
18
+ prerelease: false
18
19
  requirement: &id001 !ruby/object:Gem::Requirement
19
20
  none: false
20
21
  requirements:
@@ -22,10 +23,10 @@ dependencies:
22
23
  - !ruby/object:Gem::Version
23
24
  version: "0"
24
25
  type: :runtime
25
- prerelease: false
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
+ prerelease: false
29
30
  requirement: &id002 !ruby/object:Gem::Requirement
30
31
  none: false
31
32
  requirements:
@@ -33,10 +34,10 @@ dependencies:
33
34
  - !ruby/object:Gem::Version
34
35
  version: "0"
35
36
  type: :development
36
- prerelease: false
37
37
  version_requirements: *id002
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: cucumber
40
+ prerelease: false
40
41
  requirement: &id003 !ruby/object:Gem::Requirement
41
42
  none: false
42
43
  requirements:
@@ -44,10 +45,10 @@ dependencies:
44
45
  - !ruby/object:Gem::Version
45
46
  version: "0"
46
47
  type: :development
47
- prerelease: false
48
48
  version_requirements: *id003
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: autotest
51
+ prerelease: false
51
52
  requirement: &id004 !ruby/object:Gem::Requirement
52
53
  none: false
53
54
  requirements:
@@ -55,10 +56,10 @@ dependencies:
55
56
  - !ruby/object:Gem::Version
56
57
  version: "0"
57
58
  type: :development
58
- prerelease: false
59
59
  version_requirements: *id004
60
60
  - !ruby/object:Gem::Dependency
61
61
  name: rake
62
+ prerelease: false
62
63
  requirement: &id005 !ruby/object:Gem::Requirement
63
64
  none: false
64
65
  requirements:
@@ -66,7 +67,6 @@ dependencies:
66
67
  - !ruby/object:Gem::Version
67
68
  version: "0"
68
69
  type: :development
69
- prerelease: false
70
70
  version_requirements: *id005
71
71
  description: A simple way of extracting various elements from an Nokogiri document using XPaths
72
72
  email:
@@ -87,6 +87,7 @@ files:
87
87
  - README.markdown
88
88
  - Rakefile
89
89
  - dozuki.gemspec
90
+ - features/boolean_accessor.feature
90
91
  - features/date_accessor.feature
91
92
  - features/each_accessor.feature
92
93
  - features/exists_accessor.feature
@@ -128,18 +129,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
129
  requirements:
129
130
  - - ">="
130
131
  - !ruby/object:Gem::Version
131
- hash: -990796854414031921
132
- segments:
133
- - 0
134
132
  version: "0"
135
133
  required_rubygems_version: !ruby/object:Gem::Requirement
136
134
  none: false
137
135
  requirements:
138
136
  - - ">="
139
137
  - !ruby/object:Gem::Version
140
- hash: -990796854414031921
141
- segments:
142
- - 0
143
138
  version: "0"
144
139
  requirements: []
145
140
 
@@ -150,6 +145,7 @@ specification_version: 3
150
145
  summary: A syntactic sugar wrapper for Nokogiri
151
146
  test_files:
152
147
  - .autotest
148
+ - features/boolean_accessor.feature
153
149
  - features/date_accessor.feature
154
150
  - features/each_accessor.feature
155
151
  - features/exists_accessor.feature