nokogiri_bang_finders 1.0.0 → 1.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3584b7abe3e4df0f0b85120296766ec7e1a24438
4
- data.tar.gz: 9b0b553216740fc6f311a283afa9fbc338423eb8
3
+ metadata.gz: 38f3b45d9436abb86e0308f5eb9edf95baf0fdbc
4
+ data.tar.gz: b84eb8c29d48d8546ddd84bde32ee78901b09095
5
5
  SHA512:
6
- metadata.gz: 268af5bb0f08d50f6daa4d1f78b4af6d51fabf5ea45856d28bf47d8fb36da7e2773a7b54b4cdf881e2164a9d1091b35ea56242d0ef4d45ee36e0d261bcc0706a
7
- data.tar.gz: 41a375c7da3916b7f77a21536d8d38fce36ee54c8ff4ade166c8aea6b334b2eaa7db91686841add7e2d2875f5c1059e420e8c938cc5e51fa728731f7a9db3496
6
+ metadata.gz: 716b0b308507f3598b3cc50ec9a25e680258448f43131922bc8b55c0b759c83749ac6ad270a69c27a4b2eb1552a9675baf25c0c2a2d888c977241ed01fc3bd43
7
+ data.tar.gz: 7ecb8574b7322a8dc6e77f8fa334abd5c1062ad0b2bb92047bde36ca1df3706d8f126f77131a7126a085eea6d31475e824a6e072802ea8834068159df0d178f1
@@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file. Version num
4
4
 
5
5
  ## Unreleased
6
6
 
7
- Nothing
7
+ None
8
+
9
+ ## 1.1.0 - 2015-03-19
10
+
11
+ ### Added
12
+
13
+ Allow user to specify an integer number of characters of context to be given with exceptions, or to request `:all` context, using `Nokogiri::XML::BangFinders.context_length = some_value`
8
14
 
9
15
  ## 1.0.0 - 2014-08-14
10
16
 
data/README.md CHANGED
@@ -25,6 +25,14 @@ Each method just calls its non-bang namesake and, if the result is `nil`, raises
25
25
 
26
26
  This gem is so tiny, you could just copy and paste its code. But if it's convenient, use it. :smile:
27
27
 
28
+ ## Context
29
+
30
+ When an exception is raised, its message includes some context from the document, to show "I was looking for your selector in a document that looks like this". By default, up to 200 characters of context are given.
31
+
32
+ You can specify an integer number of characters of context, like `Nokogiri::XML::BangFinders.context_length = 500`.
33
+
34
+ Or you can specify `:all` to get the whole document in the exception message (though this probably isn't a good idea for production code): `Nokogiri::XML::BangFinders.context_length = :all`
35
+
28
36
  ## Installation
29
37
 
30
38
  $ gem install nokogiri_bang_finders
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -3,6 +3,19 @@ require "nokogiri_bang_finders/version"
3
3
  module Nokogiri
4
4
  module XML
5
5
  module BangFinders
6
+
7
+ def self.context_length
8
+ @context_length ||= 200
9
+ end
10
+
11
+ def self.context_length=(val)
12
+ @context_length = if val.to_s == "all"
13
+ Float::INFINITY
14
+ else
15
+ val.to_i
16
+ end
17
+ end
18
+
6
19
  def at!(*args)
7
20
  node = at(*args); raise NotFound.new(args, self) if node.nil?; node
8
21
  end
@@ -20,11 +33,15 @@ module Nokogiri
20
33
  class NotFound < StandardError
21
34
  attr_reader :message
22
35
  def initialize(needle, haystack)
23
- @message = "#{needle} in \n#{snippet(haystack)}"
36
+ @message = "#{needle} in \n#{snippet(haystack)}\n"
24
37
  end
25
38
  def snippet(haystack)
26
39
  snippet = haystack.to_s
27
- snippet.length > 200 ? "#{snippet[0...200]}..." : snippet
40
+ snippet.length <= context_length ? snippet : "#{snippet[0..(context_length - 1)]}...\n"
41
+ end
42
+
43
+ def context_length
44
+ Nokogiri::XML::BangFinders.context_length
28
45
  end
29
46
  end
30
47
  end
@@ -1,3 +1,3 @@
1
1
  module NokogiriBangFinders
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -21,6 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "nokogiri", ">= 1.0"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.5"
24
- spec.add_development_dependency "rspec", '~> 2.14.1'
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
25
  spec.add_development_dependency "rake"
26
26
  end
@@ -25,7 +25,9 @@ describe Nokogiri::XML::BangFinders do
25
25
  describe "when #at returns nil" do
26
26
 
27
27
  it "raises an exception describing the failed search" do
28
- expect{xml_doc.at!('robots')}.to raise_error(Nokogiri::XML::NotFound, /robots/)
28
+ e = exception_from { xml_doc.at!("robots") }
29
+ expect(e).to be_a(Nokogiri::XML::NotFound)
30
+ expect(e.message).to match(/robots/)
29
31
  end
30
32
 
31
33
  end
@@ -50,7 +52,9 @@ describe Nokogiri::XML::BangFinders do
50
52
  describe "when #at_css returns nil" do
51
53
 
52
54
  it "raises an exception describing the failed search" do
53
- expect{xml_doc.at_css!('robots')}.to raise_error(Nokogiri::XML::NotFound, /robots/)
55
+ e = exception_from { xml_doc.at_css!("robots") }
56
+ expect(e).to be_a(Nokogiri::XML::NotFound)
57
+ expect(e.message).to match(/robots/)
54
58
  end
55
59
 
56
60
  end
@@ -75,7 +79,70 @@ describe Nokogiri::XML::BangFinders do
75
79
  describe "when #at_xpath returns nil" do
76
80
 
77
81
  it "raises an exception describing the failed search" do
78
- expect{xml_doc.at_xpath!('//robots')}.to raise_error(Nokogiri::XML::NotFound, /\/\/robots/)
82
+ e = exception_from { xml_doc.at_xpath!("//robots") }
83
+ expect(e).to be_a(Nokogiri::XML::NotFound)
84
+ expect(e.message).to match(/\/\/robots/)
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ describe "providing context with an exception" do
92
+
93
+ let(:xml_doc) {
94
+ body = "<root><numbers>"
95
+ 1_000.times do |i|
96
+ body << "<number>#{i}</number>"
97
+ end
98
+ body << "</numbers></root>"
99
+ Nokogiri::XML(body)
100
+ }
101
+
102
+ it "does not include the whole document" do
103
+ e = exception_from { xml_doc.at_css!("robots") }
104
+ expect(e.message).not_to include(xml_doc.to_s)
105
+ end
106
+
107
+
108
+ it "includes the first 200 characters of the document by default" do
109
+ e = exception_from { xml_doc.at_css!("robots") }
110
+ expect(e.message ).to include(xml_doc.to_s[0..199])
111
+ expect(e.message).not_to include(xml_doc.to_s[0..200])
112
+ end
113
+
114
+ describe "specifying how much document context to supply with errors" do
115
+
116
+ before :each do
117
+ @original_setting = Nokogiri::XML::BangFinders.context_length
118
+ Nokogiri::XML::BangFinders.context_length = specified_context
119
+ end
120
+
121
+ after :each do
122
+ Nokogiri::XML::BangFinders.context_length = @original_setting
123
+ end
124
+
125
+ describe "when context_length is given as a number" do
126
+
127
+ let(:specified_context) { 10 }
128
+
129
+ it "includes the specified number of characters of context" do
130
+ e = exception_from { xml_doc.at_css!("robots") }
131
+ expect(e.message ).to include(xml_doc.to_s[0..9])
132
+ expect(e.message).not_to include(xml_doc.to_s[0..10])
133
+ end
134
+
135
+ end
136
+
137
+ describe "when context is given as 'all'" do
138
+
139
+ let(:specified_context) { :all }
140
+
141
+ it "includes the entire document" do
142
+ e = exception_from { xml_doc.at_css!("robots") }
143
+ expect(e.message ).to include(xml_doc.to_s)
144
+ end
145
+
79
146
  end
80
147
 
81
148
  end
@@ -3,7 +3,6 @@ require 'nokogiri_bang_finders'
3
3
 
4
4
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
5
5
  RSpec.configure do |config|
6
- config.treat_symbols_as_metadata_keys_with_true_values = true
7
6
  config.run_all_when_everything_filtered = true
8
7
  config.filter_run :focus
9
8
 
@@ -13,3 +12,12 @@ RSpec.configure do |config|
13
12
  # --seed 1234
14
13
  config.order = 'random'
15
14
  end
15
+
16
+ def exception_from
17
+ begin
18
+ yield
19
+ rescue => e
20
+ return e
21
+ end
22
+ fail "Tried to get the exception from the block given, but it didn't raise one"
23
+ end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri_bang_finders
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Long
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-14 00:00:00.000000000 Z
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.5'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.14.1
47
+ version: '3.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.14.1
54
+ version: '3.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Adds XML finders to Nokogiri that raise if nothing is found.
@@ -73,8 +73,8 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
77
- - .rspec
76
+ - ".gitignore"
77
+ - ".rspec"
78
78
  - CHANGELOG.md
79
79
  - Gemfile
80
80
  - LICENSE.txt
@@ -95,17 +95,17 @@ require_paths:
95
95
  - lib
96
96
  required_ruby_version: !ruby/object:Gem::Requirement
97
97
  requirements:
98
- - - '>='
98
+ - - ">="
99
99
  - !ruby/object:Gem::Version
100
100
  version: '0'
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  requirements:
103
- - - '>='
103
+ - - ">="
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
107
  rubyforge_project:
108
- rubygems_version: 2.0.14
108
+ rubygems_version: 2.2.2
109
109
  signing_key:
110
110
  specification_version: 4
111
111
  summary: Adds XML finders to Nokogiri that raise if nothing is found.