GitXplorerGitXplorer
n

nokogiri_bang_finders

public
9 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
e225cdce8f738e135a1985a6daa03cbefa4cf179

Bump version for release

nnathanl committed 10 years ago
Unverified
bd0f735376f82b35ca95a4fa7fa7e371f0f2337b

Can request :all context with an exception

nnathanl committed 10 years ago
Unverified
281a191e843f9b2a74806da5756af7da6992f839

Allow specifying a number of lines of context

nnathanl committed 10 years ago
Unverified
a2e5bd0be8a0a2df4287466708145ea3b2fa62c0

Test context inclusion in messages

nnathanl committed 10 years ago
Unverified
cbc35122ac67cceef4c14ada448862f556064a66

Especially explicit exception expectations

nnathanl committed 10 years ago
Unverified
a05d18a2afca1faaaa28a3af528e05f7a8befc92

Update Rspec version

nnathanl committed 10 years ago

README

The README file for this repository.

nokogiri_bang_finders

This gem says "Nokogiri, if you can't find the XML I want, yell about it."

For example:

doc = Nokogiri::XML("<root><aliens><alien><name>Alf</name></alien></aliens></root>")
doc.at('alien').content # => "Alf"

# without nokogiri_bang_finders
doc.at('robot').content # NoMethodError: undefined method `content' for nil:nilclass

# with nokogiri_bang_finders
doc.at!('robot').content # Nokogiri::XML::NotFound: '["robot"] in {snippet of the XML}'

Specifically, this gem adds the following methods to to Nokogiri::XML::Node and Nokogiri::XML::NodeSet:

  • at!
  • at_xpath!
  • at_css!

Each method just calls its non-bang namesake and, if the result is nil, raises Nokogiri::XML::NotFound.

This gem is so tiny, you could just copy and paste its code. But if it's convenient, use it. 😄

Context

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.

You can specify an integer number of characters of context, like Nokogiri::XML::BangFinders.context_length = 500.

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

Installation

$ gem install nokogiri_bang_finders

... or via Bundler.

Requiring

Be sure to require 'nokogiri_bang_finders' after you require 'nokogiri'. It expects Nokogiri::XML::Node and Nokogiri::XML::NodeSet to be defined so that it can add methods to them.

Usage

Use these methods just like you would use the normal Nokogiri versions, but expect an exception if the XML doesn't contain what you're looking for.